How do you set the EditText keyboard to only consist of numbers on Android?
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:
Android EditText - Input with numbers only
It seems to work well with this configuration:
android:digits="0123456789"
android:inputType="phone"
That's a good enough workaround for me.
How do you set EditText to only accept numeric values in Android?
Add android:inputType="number"
as an XML attribute.
How to force EditText to accept only numbers?
Use android:inputType="number"
in your layout XML
Android: Edittext that only allows a digit from 1-9
Ther have two ways for your question
1) in your XML fayl
<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="123456789"
android:inputType="number"
android:maxLength="1"/>
2) in your Activity class
mEdit.setInputType(InputType.TYPE_CLASS_NUMBER );
mEdit.setKeyListener(DigitsKeyListener.getInstance("0123456789"));
mEdit.setSingleLine(true);
Display keyboard for numers when inputType set to 'text'
You can use this but you need to handle invalid input yourself or add TextWatcher
to check for input while typing
android:inputType="number"
android:digits="0123456789,."
How to force EditText to accept only numbers?
Use android:inputType="number"
in your layout XML
Related Topics
Android: How to Periodically Send Location to a Server
Multi Flavor App Based on Multi Flavor Library in Android Gradle
Customize Android Facebook Login Button
Set Inputtype for an Edittext Programmatically
Transitive Dependencies for Local Aar Library
Android - Local Image in Webview
Android Get Image from Gallery into Imageview
Sqliteopenhelper Problem with Fully Qualified Db Path Name
Remove Listview Items in Android
Is a Relativelayout More Expensive Than a Linearlayout
How to Fix: "Hax Is Not Working and Emulator Runs in Emulation Mode"
Adding Table Rows Dynamically in Android
Android: Bitmapfactory.Decodestream() Out of Memory with a 400Kb File with 2Mb Free Heap
How to Show Icons in Overflow Menu in Actionbar
How to Use 3G Connection in Android Application Instead of Wi-Fi