Chiwai Chan
Chiwai Chan

Reputation: 4866

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

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

Upvotes: 155

Views: 190605

Answers (13)

Akhila
Akhila

Reputation: 3242

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”));

Upvotes: 4

Shiv Buyya
Shiv Buyya

Reputation: 4130

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

Upvotes: 0

Darshuu
Darshuu

Reputation: 511

Use the below code in java file

editText.setRawInputType(Configuration.KEYBOARD_QWERTY);

Upvotes: 4

fulgen
fulgen

Reputation: 191

Define this in your xml code

android:inputType="number"

Upvotes: 1

Matthijs
Matthijs

Reputation: 1375

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);

Upvotes: 56

Nirav Ranpara
Nirav Ranpara

Reputation: 13785

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" />
    

Upvotes: 10

macio.Jun
macio.Jun

Reputation: 9895

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

android:inputType="numberDecimal"

Upvotes: 8

sai Gorantla
sai Gorantla

Reputation: 179

More input types and details found here on google website

Upvotes: 2

Nguyen  Minh Binh
Nguyen Minh Binh

Reputation: 24423

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ...
    android:inputType="number|phone"/> 

will show the large number pad as dialer.

Upvotes: 19

Aliya
Aliya

Reputation: 1178

editText.setRawInputType(InputType.TYPE_CLASS_NUMBER);

Upvotes: 4

Pir Fahim Shah
Pir Fahim Shah

Reputation: 10623

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)

Upvotes: 5

Dominic
Dominic

Reputation: 1141

To do it in a Java file:

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

Upvotes: 114

Josef Pfleger
Josef Pfleger

Reputation: 74507

You can configure an inputType for your EditText:

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

Upvotes: 277

Related Questions