Search code examples
androidkeyboardandroid-softkeyboard

Numeric Soft Keyboard on Android on empty Activity


I want to call a soft keyboard with numbers, but I don’t understand how to do this. I can call just a pogram keyboard, but how can I make it so that there are only numbers?

in all examples they usually use editText with inputType="number", but I have an empty activity on which I need to call the soft keyboard with numbers.


Solution

  • The simple answer is, you can't without setting an input type.

    You can display the keyboard any time you like by calling:

    InputMethodManager imm = (InputMethodManager)   getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    

    But in order to set it to set the keyboard to numeric, you need to set the input type, as shown below:

    myEditText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
    

    or in xml

    android:inputType = "numberPassword"