154

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

Lucifer
  • 29,392
  • 25
  • 90
  • 143
Chiwai Chan
  • 4,716
  • 4
  • 30
  • 33
  • 4
    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. – AlanKley Jun 09 '13 at 13:52
  • Check my answer on this question http://stackoverflow.com/questions/39593324/cannot-resolve-symbol-showsoftinput/39593871#39593871 – Android Help Sep 20 '16 at 12:22

13 Answers13

275

You can configure an inputType for your EditText:

<EditText android:inputType="number" ... />
Josef Pfleger
  • 74,165
  • 16
  • 97
  • 99
  • 6
    Also note that you need to target Android 1.5 to use this (it's not available in 1.1) – Wilka Aug 09 '09 at 19:47
  • 4
    The problem is that you can't switch back. Using "number|text" does not work either. It seems if you want to show a numeric keyboard, you can't allow it switch back to text. –  Aug 18 '11 at 20:18
  • I agree with Brian..if the first field is alpha and next field is numeric, I cannot go to the next field using the 'Next' button! – Ahmed Faisal Mar 21 '12 at 17:57
  • Whether you can go back or not depends on the phone I think. Mine allows it. – Martin Clemens Bloch Feb 24 '14 at 11:43
  • I want to change keybord type number and I want paste text in edittext. How to do? – ibyanik Nov 30 '18 at 06:29
114

To do it in a Java file:

EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
Dominic
  • 1,141
  • 1
  • 7
  • 2
56

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);
Matthijs
  • 1,315
  • 12
  • 12
  • 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. – Martin Erlic Dec 30 '15 at 02:54
19
<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ...
    android:inputType="number|phone"/> 

will show the large number pad as dialer.

John
  • 3,769
  • 6
  • 30
  • 49
Nguyen Minh Binh
  • 23,891
  • 30
  • 115
  • 165
10

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" />
    
msrd0
  • 7,816
  • 9
  • 47
  • 82
Nirav Ranpara
  • 13,753
  • 3
  • 39
  • 54
8

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

android:inputType="numberDecimal"
macio.Jun
  • 9,647
  • 1
  • 45
  • 41
5

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)

Pir Fahim Shah
  • 10,505
  • 1
  • 82
  • 81
4
editText.setRawInputType(InputType.TYPE_CLASS_NUMBER);
Aliya
  • 1,168
  • 12
  • 17
  • 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! – lauksas Sep 29 '18 at 19:36
  • 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. – Alex Suzuki Sep 23 '19 at 15:27
4

Use the below code in java file

editText.setRawInputType(Configuration.KEYBOARD_QWERTY);

Darshuu
  • 501
  • 5
  • 9
4

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”));
Akhila
  • 3,235
  • 1
  • 14
  • 30
2

More input types and details found here on google website

sai Gorantla
  • 179
  • 9
0

Define this in your xml code

android:inputType="number"
Pirate X
  • 3,023
  • 5
  • 33
  • 60
fulgen
  • 181
  • 2
  • 2
  • 12
0
EditText number1 = (EditText) layout.findViewById(R.id.edittext); 
number1.setInputType(InputType.TYPE_CLASS_NUMBER);
Shiv Buyya
  • 3,770
  • 2
  • 30
  • 25
  • 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. – Shiv Buyya Apr 23 '18 at 13:08