Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you set the EditText keyboard to only consist of numbers on Android?

I want my EditText to display a keyboard that ONLY has numbers visible, no other characters.

I have tested with all available inputs and it doesn't work. I searched for a way to get a keyboard that only has numbers but I have only seen references to:

android: inputType = "numberPassword" 

But I want the numbers to be visible, like this: (numberPassword)

enter image description here

I have tried with:

android:digits="0123456789" android:inputType="phone" 

and

android:inputType="number" 

but it appears like this:

enter image description here

like image 298
Jorge B. Avatar asked Dec 11 '12 09:12

Jorge B.


People also ask

How do I get my android keyboard to only show numbers?

The keyboard actually has a full number pad layout too; unlike some keyboards, it's available in any app. To get the number pad, and tap the special character key in the lower left corner of the keyboard.

How can add only numbers in EditText in android?

You can use android:inputType="number" in the XML file. You can specify other values such as numberDecimal as well. Also, you might additionally want to use android:singleLine="true" for a single line Edittext .

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

Using phone inputType EditText has an attribute called android:inputType . Providing android:inputType with value phone can display numbered keyboard when the EditText gets focus. Create an Android Project and replace layout (activity_main. xml) and Kotlin file (MainActivity.

How do I get the number to edit text?

Use android:inputType="number" to force it to be numeric. Convert the resulting string into an integer (e.g., Integer. parseInt(myEditText. getText().


2 Answers

After several tries, I got it! I'm setting the keyboard values programmatically like this:

myEditText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD); 

Or if you want you can edit the XML like so:

android: inputType = "numberPassword" 

Both configs will display password bullets, so we need to create a custom ClickableSpan class:

private class NumericKeyBoardTransformationMethod extends PasswordTransformationMethod {     @Override     public CharSequence getTransformation(CharSequence source, View view) {         return source;     } } 

Finally we need to implement it on the EditText in order to display the characters typed.

myEditText.setTransformationMethod(new NumericKeyBoardTransformationMethod()); 

This is how my keyboard looks like now:

like image 121
Mauricio Sartori Avatar answered Sep 20 '22 00:09

Mauricio Sartori


android:inputType="number" or android:inputType="phone". You can keep this. You will get the keyboard containing numbers. For further details on different types of keyboard, check this link.

I think it is possible only if you create your own soft keyboard. Or try this android:inputType="number|textVisiblePassword. But it still shows other characters. Besides you can keep android:digits="0123456789" to allow only numbers in your edittext. Or if you still want the same as in image, try combining two or more features with | separator and check your luck, but as per my knowledge you have to create your own keypad to get exactly like that..

like image 38
Kanth Avatar answered Sep 18 '22 00:09

Kanth