How do I show the number keyboard on an EditText in android?
177,567
Solution 1
You can configure an inputType
for your EditText
:
<EditText android:inputType="number" ... />
Solution 2
To do it in a Java file:
EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
Solution 3
I found this implementation useful, shows a better keyboard and limits input chars.
<EditText
android:inputType="phone"
android:digits="1234567890"
...
/>
Additionally, you could use android:maxLength
to limit the max amount of numbers.
To do this programmatically:
editText.setInputType(InputType.TYPE_CLASS_PHONE);
KeyListener keyListener = DigitsKeyListener.getInstance("1234567890");
editText.setKeyListener(keyListener);
Solution 4
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
...
android:inputType="number|phone"/>
will show the large number pad as dialer.
Solution 5
You can do it 2 ways
Runtime set
EditText input = new EditText(this); input.setInputType(InputType.TYPE_CLASS_NUMBER);
Using XML
<EditText ... android:inputType="number" />
Related videos on Youtube
Comments
-
Chiwai Chan almost 4 years
I just basically want to switch to the number pad mode as soon a certain EditText has the focus.
-
AlanKley almost 11 yearsThe question does not specifically state a desire to change the allowed input type. It simply asks how to change the keyboard mode which is what I would like to do without changing the input type. All answers explain how to change the input type.
-
-
Martin Erlic over 8 yearsIs there any way to do this programmatically? Dying to get a limited number keyboard for my EditText but it's set up in such a way it has to be generated in the activity.
-
Shiv Buyya about 6 yearsIf you have EditText number1 created in layout xml code, then if you want to set Number Keyboard to number1 EditText, you can do this way using Java Code.
-
lauksas over 5 yearsIf you are manually formatting a text (like for currency) and want to control just the keyboard, that's the choice. Won't filter your chars. Thanks!
-
cyroxis over 5 yearsYou should add commentary explaining your code and why it is the solution
-
Alex Suzuki over 4 yearsThis worked like a charm for me. I want to display a textual placeholder value when a field has not been edited yet, but still restrict the input to numbers and display a restricted keyboard.
-
Chego over 3 yearsyou have the best answer!! Thanks