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

  1. Runtime set

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

    <EditText
        ...
        android:inputType="number" />
    
Share:
177,567

Related videos on Youtube

Chiwai Chan
Author by

Chiwai Chan

AWS &amp; Security

Updated on May 30, 2020

Comments

  • Chiwai Chan
    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
      AlanKley almost 11 years
      The 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
    Martin Erlic over 8 years
    Is 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
    Shiv Buyya about 6 years
    If 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
    lauksas over 5 years
    If 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
    cyroxis over 5 years
    You should add commentary explaining your code and why it is the solution
  • Alex Suzuki
    Alex Suzuki over 4 years
    This 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
    Chego over 3 years
    you have the best answer!! Thanks