0

I have a custom keyboard and don't want to show the Android softkeyboard. this can be achieved by the following code (How to hide Android soft keyboard on EditText):

editText.setInputType(InputType.TYPE_NULL);

However the EditText should only allow digits. this can be achieved by:

editText.setInputType(InputType.TYPE_CLASS_NUMBER);

I can't seem to find a way to combine both functionalities. setting the inputtype to TYPE_NULL will allow non-numeric characters when using hardware keyboards and setting the inputtype to TYPE_CLASS_NUMBER causes the soft keyboard to pop up.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Wirling
  • 4,810
  • 3
  • 48
  • 78

2 Answers2

2

The answer from airowe guided me in the right direction. This solution didn't seem to work for Android 2.3.X devices (the soft keyboard would still pop up). So I tweaked it a bit. I ended up using the following code:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); //hide keyboard

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
{
   editText.setInputType(InputType.TYPE_CLASS_NUMBER);
}
else
{
   editText.setRawInputType(InputType.TYPE_NULL);
}

This would probably allow pre-honeycomb tablets to enter text. However I think this is negligible.

Wirling
  • 4,810
  • 3
  • 48
  • 78
1

Instead of

editText.setInputType(InputType.TYPE_NULL);

use

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(fieldController.getFieldComponent().getWindowToken(), 0); //hide keyboard

to hide the keyboard.

Then, you can set the input type to what you want.

i.e.

editText.setInputType(InputType.TYPE_CLASS_NUMBER);
airowe
  • 794
  • 2
  • 9
  • 29