Search code examples
androidandroid-keypad

Best way to detect editText inputType in Android?


I developed a simple keyboard.

It's a T9 keyboard that is conceived specifically for phones with an hardware keyboard.

When I want to write something into a standard EditText I have no problem, but when i try to write numbers inside an ediText with android:inputType="phone" or android:inputType="number" the keyboard still tries to write letters.

My question is: What is the best and simplest way to detect if the editText I have focus on is Number-only, Text-only, etc. from my custom Keyboard? How can I detect the inputType from my Keyboard?

I hope someone of you already solved this problem in the past and can help...thank you in advance!


Solution

  • Try something like this:

    if(txt.getInputType()==InputType.TYPE_CLASS_PHONE){
    //do something
    }
    

    Also check this question: Android: Determine active input method from code it describes using the InputMethodManager

    **

    Another Approach:

    **

    when developing your custom keyboard: as described here http://developer.android.com/guide/topics/text/creating-input-method.html#IMEUI

    you extends InputMethodService which provides the method onStartInputView(EditorInfo info, boolean restarting)

    onStartInputView has the EditorInfo argument which has the inputType field that you can know the current edit text input type from