I have an EditText box and I want the default keyboard that comes up when it is selected to be the numeric keypad, since most of the time users will be entering numbers. However, I do want to allow users to enter alphabetic characters too, if they need to. Using android:inputType="number"
restricts input to numeric digits only. What options do I have?

- 6,851
- 3
- 49
- 78

- 889
- 1
- 6
- 3
-
3Hi have you got the solution ?If yes then please share it.I have to implement the same – Sneha Bansal Nov 15 '14 at 09:51
-
Do you want user to start with numeric only and then after more than 1 digit he/she can allow to enter alphanumeric? – Dhrumil Shah - dhuma1981 Feb 05 '15 at 09:08
-
this have an answer [here](http://stackoverflow.com/a/8776491/1529129) – Rahul Tiwari Sep 25 '15 at 14:01
-
possible duplicate. question answered in this [Similar Post](http://stackoverflow.com/questions/11134873/android-display-numeric-keypad-on-button-click) – hrk_er Oct 15 '15 at 07:50
19 Answers
Define your EditText in your xml this way:
<EditText
android:id="@+id/txtOppCodigoCliente"
android:inputType="textVisiblePassword"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxLength="11"
android:hint="@string/strCodigoCliente"/>
output: android:inputType="textVisiblePassword"
A row of numbers and the rest of letters.

- 311
- 2
- 4
-
3
-
It depends on the keyboard. In my case does not work with the last versions of Google Keyboard and Nougat. – fireb86 Jun 29 '17 at 07:56
-
-
this._editText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); this._editText.setTypeface(Typeface.DEFAULT); works for me – xevser Jul 05 '18 at 05:41
-
@fireb86 it does work with the latest version of Gboard. And its working perfectly fine on Oreo. – Tim John Sep 20 '18 at 11:53
I would suggest a more Robust solution:
Present the user with the ability to choose the input type:
NEEDED FIXES
-better handling of showing/hiding the input selection buttons
-probably remove the suggestions when the input type is letter
Here is the activity:
package mobi.sherif.numberstexttextview;
import android.os.Bundle;
import android.app.Activity;
import android.text.InputType;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText mEdit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEdit = (EditText) findViewById(R.id.input);
mEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean focused) {
findViewById(R.id.llmenu).setVisibility(focused?View.VISIBLE:View.GONE);
}
});
}
public void onNumbers(View v) {
mEdit.setInputType(InputType.TYPE_CLASS_NUMBER);
}
public void onLetters(View v) {
mEdit.setInputType(InputType.TYPE_CLASS_TEXT);
}
}
Here is the activity's xml : activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:id="@+id/scrollview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignWithParentIfMissing="true"
android:layout_above="@+id/llmenu" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" >
<requestFocus />
</EditText>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="@+id/llmenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#000000"
android:padding="0dp" >
<Button
android:id="@+id/buttonnumbers"
android:layout_width="0dp"
android:onClick="onNumbers"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Numbers" />
<Button
android:id="@+id/buttonletters"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="onLetters"
android:text="Letters" />
</LinearLayout>
</RelativeLayout>

- 45,786
- 16
- 89
- 106
-
If number is one of the inputTypes and the keyboard is in fullscreen mode (landscape), letters get hidden. When switching back to the TEXT inputType, they appear again. – OneWorld Mar 25 '15 at 08:02
Here is what you are looking for:
Define your EditText
in your xml this way:
<EditText
android:id="@+id/etTest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/hint">
</EditText>
And call following in your onCreate()
:
etTest = (EditText) findViewById(R.id.etTest);
etTest.setRawInputType(InputType.TYPE_CLASS_NUMBER);
// Note the "Raw"
Whenever you click into your EditText
now, you can enter digits AND letters! (Digit keyboard is shown first)

- 3,857
- 1
- 25
- 28
-
4For me on a Nexus 7 it showed the numeric pad but it had no way of showing the normal keyboard. – Rob Oct 15 '14 at 09:42
-
Looked promising, but if number is one of the inputTypes and the keyboard is in fullscreen mode (landscape), letters get hidden. – OneWorld Mar 25 '15 at 08:03
Unfortunately this is not possible, because only one inputType can be set for an EditText. The following will just show alphabatic characters on buttons.
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text|number" />
However, what I can suggest you is, set your input type to numeric, and just add a small switchKeyboard button on your form close to the numpad. And set its onClick
editText1.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
so that users can switch to alphabetic characters, too.

- 1,350
- 2
- 24
- 56
-
Reason for that lies in the [documentation](http://developer.android.com/reference/android/text/InputType.html) Take a close look at the constant names. You only can combine one class with one variation and one flag. Like `TYPE_CLASS_NUMBER | TYPE_NUMBER_FLAG_SIGNED`. **But you never can mix classes like `number` and `text`** – OneWorld Mar 24 '15 at 07:38
-
the documentation certainly does not state that classes cannot be mixed. there's no lint check complaining about it. and there's even a specific keyboard when combining eg `number|text` (see above) which implies you could enter letters via long press, doesn't actually work. looks rather buggy to me, or incomplete at least. – masc3d Oct 20 '17 at 10:41
-
@masc3d - agreed doc doesn't say that where it should. Is mentioned under [InputType](https://developer.android.com/reference/android/text/InputType), in description of `TYPE_MASK_CLASS`: Mask of bits that determine the overall class of text being given. supported classes are: TYPE_CLASS_TEXT, TYPE_CLASS_NUMBER, TYPE_CLASS_PHONE, TYPE_CLASS_DATETIME. IME authors: If the class is not one you understand, assume TYPE_CLASS_TEXT with NO variation or flags." – ToolmakerSteve Feb 14 '19 at 19:41
Basically, EditText inputtype is used to set the input type in your EditText.
The possible values for the android:inputtype
are:
- text
- textCapCharacters
- textCapWords
- textCapSentences
- textAutoCorrect
- textAutoComplete
- textMultiLine
- textImeMultiLine
- textNoSuggestions
- textUri
- textEmailAddress
- textEmailSubject
- textShortMessage
- textLongMessage
- textPersonName
- textPostalAddress
- textPassword
- textVisiblePassword
- textWebEditText
- textFilter
- textPhonetic
- number
- numberSigned
- numberDecimal
- phone
- datetime
- date
- time
then you can use any value as showing in example:
<EditText android:id="@+id/EditText01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:inputType="number"
android:text="1212223"/>

- 127,700
- 71
- 241
- 295
-
3Thanks! I couldn't even find the Google Android documentation, listing all of this! – gimmegimme Feb 11 '17 at 17:23
Asus tablet has that feature, they have customized the default keyboard. And the keyboard contains the numbers and alphabets only. I think your requirement solution is too to customize the default keyboard

- 8,281
- 9
- 37
- 50
You should not use any inputType filter in this case, just add the below line in your EditText Property
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 "
Now only these characters will be allowed to enter in the EditText. The complete code of your edittext will be like this:
<EditText
android:id="@+id/etTest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 "
android:hint="@string/hint">

- 6,627
- 2
- 58
- 83
You can follow Paresh Mayani answer.
And in advice you can use android:inputType="textPhonetic"
for the inputType Text and Number based on user selection.
Hope you will got my point.

- 23,386
- 35
- 116
- 188
In OnCreate in Activity do this:
t = (EditText) findViewById(R.id.test);
t.setKeyListener(new KeyListener() {
@Override
public boolean onKeyUp(View view, Editable text, int keyCode, KeyEvent event) {
return false;
}
@Override
public boolean onKeyOther(View view, Editable text, KeyEvent event) {
return false;
}
@Override
public boolean onKeyDown(View view, Editable text, int keyCode, KeyEvent event) {
if (keyCode == 67) { // Handle backspace button =)
if (text.length() > 0) {
t.setText(text.subSequence(0, text.length() - 1));
t.setSelection(text.length()-1);
}
}
return false;
}
@Override
public int getInputType() {
return InputType.TYPE_CLASS_NUMBER;
}
@Override
public void clearMetaKeyState(View view, Editable content, int states) {
}
});
In this case you type is number
and android will show numeric keyboard, but you can input whatever you want.
Also you can override other methods as you wish.

- 3,761
- 3
- 31
- 55
-
Did you try it in landscape, when the keyboard is in fullscreen mode? – OneWorld Mar 25 '15 at 08:06
Here is how I achieved it...
android:inputType="textPhonetic|numberDecimal" android:digits="/0123456789.abcdefghijklmnopqrstuvwxyz"
Only characters specified by digits will be allowed.
Please note that the keyboard shown is that of the dialer, not the typical keyboard. For me, it achieved what I needed to entirely within the XML.

- 49
- 1
- 3
-
2Don't mix input classes. They always will result in number. See http://stackoverflow.com/questions/3544214/edittext-with-number-keypad-by-default-but-allowing-alphabetic-characters/28694835#comment46661555_14476449 – OneWorld Mar 25 '15 at 08:09
-
Thanks slamn, I needed to use only whole numbers and "-"/minus operator – gimmegimme Feb 11 '17 at 17:22
Programmatically it is possible with little bit of tweak to the usual flow. First you have to set editText as:
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
Then you have to listen for keyevent. On pressing of pound set the InputType again to InputType.TYPE_CLASS_TEXT. This should work as it works for me.
editText.setOnKeyListener(new View.OnKeyListener()
{
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
Log.d("KeyBoard", "Keyboard Test Key Hit");
switch (keyCode)
{
KeyEvent.KEYCODE_POUND:
if(editText.setInputType(InputType.TYPE_CLASS_TEXT);
{
editText.setInputType(InputType.TYPE_CLASS_TEXT);
return true;
}
}
}
}

- 4,603
- 3
- 39
- 59

- 560
- 4
- 17
-
is it a java code? Also, some keyboards, when in TYPE_CLASS_NUMBER, don't have a KEYCODE_POUND button. – nutella_eater Aug 13 '21 at 09:07
Why not just have a toggle button that either:
- Sets the input type to allow only numerical values
- Sets the input type to allow the use of the QWERTY keyboard
- Or set other input types (if needed)
That way, you can set the numeric keypad to default, then the user can change it by pressing a button.

- 103
- 1
- 13
This is not possible given current default Android input types across various phones.
There are many possible hacks that work on some keyboards but not on others, like the common method of setting the raw input type that is commonly included in answers:
editText.setRawInputType(InputType.TYPE_CLASS_NUMBER)
Best advice I could find is to use the textPostalAddress
input type in your XML:
android:inputType="textPostalAddress"
Unfortunately this doesn't do what we want it to do yet, but maybe it will in the future. The only advantage of this is that the meta information on your edit box will reflect what you intend.

- 29,432
- 22
- 140
- 255
You can try setting---
editText.setRawInputType(Configuration.KEYBOARD_QWERTY);
for more description
In your code for textView, remove the "text" in android:inputType.
It should be,
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number" />

- 1,073
- 6
- 15
- 26
-
This is not solving his problem. He wants to allow letters and numbers with numbers keyboard – OneWorld Mar 25 '15 at 08:05
You can do this programatically:
// Numbers
mEditText.setInputType(InputType.TYPE_CLASS_NUMBER);
// Text
mEditText.setInputType(InputType.TYPE_CLASS_TEXT);
setInputType(int type) Set the type of the content with a constant as defined for inputType.
As Saurav says, the best way is changing between QWERTY Keyboard and Numeric Keyborad with a simple listener:
input.setInputType(InputType.TYPE_CLASS_TEXT);
input.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode) {
case 24: // Volumen UP
input.setInputType(InputType.TYPE_CLASS_NUMBER);
break;
case 25: // Volumen DOWN
input.setInputType(InputType.TYPE_CLASS_TEXT);
break;
}
return true;
}
});

- 2,282
- 8
- 26
- 34

- 3,382
- 1
- 26
- 26
I try all the method but not perfect. So I have another idea. Work perfect for all keyboard.
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reset_password);
editText.setRawInputType(InputType.TYPE_CLASS_NUMBER);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks whether a hardware keyboard is available
if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
if (editText.isFocused()) {
editText.setInputType(InputType.TYPE_CLASS_TEXT);
}
} else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show();
if (editText.getInputType() == InputType.TYPE_CLASS_TEXT) {
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
}
}
}

- 236
- 1
- 7
In your xml layout for EditText add this parameter:
android:inputType="number|text"
This will allow the user to enter both text and number and contains the logic in the xml rather than having code in your activity or fragment.

- 548
- 2
- 8
-
3Don't mix input classes. They always will result in number. See http://stackoverflow.com/questions/3544214/edittext-with-number-keypad-by-default-but-allowing-alphabetic-characters/28694835#comment46661555_14476449 – OneWorld Mar 25 '15 at 08:08