How do I show the number keyboard on an EditText in android?

AndroidKeyboardAndroid Edittext

Android Problem Overview


I just basically want to switch to the number pad mode as soon a certain EditText has the focus.

Android Solutions


Solution 1 - Android

You can configure an inputType for your EditText:

<EditText android:inputType="number" ... />

Solution 2 - Android

To do it in a Java file:

EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);

Solution 3 - Android

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 - Android

<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 - Android

You can do it 2 ways

  1. Runtime set

    EditText input = new EditText(this);
    input.setInputType(InputType.TYPE_CLASS_NUMBER);
    
  2. Using XML

    <EditText
        ...
        android:inputType="number" />
    

Solution 6 - Android

If you need fractional number, then this is the answer for you:

android:inputType="numberDecimal"

Solution 7 - Android

If you are using your EditText in Dialog or creating dynamically and You can't set it from xml then this example will help you in setting that type of key board while you are using dynamic Edit Text etc

myEditTxt.setInputType(InputType.TYPE_CLASS_NUMBER);

where myEditTxt is the dynamic EDIT TEXT object(name)

Solution 8 - Android

editText.setRawInputType(InputType.TYPE_CLASS_NUMBER);

Solution 9 - Android

Use the below code in java file

> editText.setRawInputType(Configuration.KEYBOARD_QWERTY);

Solution 10 - Android

Below code will only allow numbers "0123456789”, even if you accidentally type other than "0123456789”, edit text will not accept.

    EditText number1 = (EditText) layout.findViewById(R.id.edittext); 
    number1.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_CLASS_PHONE);
    number1.setKeyListener(DigitsKeyListener.getInstance("0123456789”));

Solution 11 - Android

More input types and details found here on google website

Solution 12 - Android

Define this in your xml code

android:inputType="number"

Solution 13 - Android

EditText number1 = (EditText) layout.findViewById(R.id.edittext); 
number1.setInputType(InputType.TYPE_CLASS_NUMBER);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChiwai ChanView Question on Stackoverflow
Solution 1 - AndroidJosef PflegerView Answer on Stackoverflow
Solution 2 - AndroidDominicView Answer on Stackoverflow
Solution 3 - AndroidMatthijsView Answer on Stackoverflow
Solution 4 - AndroidNguyen Minh BinhView Answer on Stackoverflow
Solution 5 - AndroidNirav RanparaView Answer on Stackoverflow
Solution 6 - Androidmacio.JunView Answer on Stackoverflow
Solution 7 - AndroidPir Fahim ShahView Answer on Stackoverflow
Solution 8 - AndroidAliyaView Answer on Stackoverflow
Solution 9 - AndroidDarshuuView Answer on Stackoverflow
Solution 10 - AndroidAkhila MadariView Answer on Stackoverflow
Solution 11 - Androidsai GorantlaView Answer on Stackoverflow
Solution 12 - AndroidfulgenView Answer on Stackoverflow
Solution 13 - AndroidShiv BuyyaView Answer on Stackoverflow