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:
How do you set EditText to only accept numeric values in Android?
Add android:inputType="number"
as an XML attribute.
How to set only integer numbers for EditText android (without comma)
Try below attribute in your xml:
android:inputType="numberSigned"
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 to force EditText to accept only numbers?
Use android:inputType="number"
in your layout XML
how to set only numeric value for EditTextPreference in android?
EditTextPreference widgets should take the same attributes as a regular EditText, so use:
android:inputType="number"
Or more specifically use:
android:inputType="numberDecimal"
android:digits="0123456789"
since you want to limit the input to a port number only.
How to use an editText only for numbers
The type in XML is EditText
not Number
so use Editext
as
EditText editText = (EditText) v.findViewById(R.id.editText);
For Number use inputType
android:inputType="number"
e.g
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
You can also use android:digits
to use specific digit(s)
How to force EditText to accept only numbers?
Use android:inputType="number"
in your layout XML
Related Topics
No Shadow by Default on Toolbar
Facebook Key Hash Does Not Match Any Stored Key Hashes
How to Place Fragment Arrylist Scrollabletabsactivity
How to Set Up Android Sdk for Command Line Development on Linux
Getrunningtasks Doesn't Work in Android L
Android How to Create My Own Activity and Extend It
Edittext with Number Keypad by Default, But Allowing Alphabetic Characters
How to Sign an Already Compiled APK
Windowsoftinputmode="Adjustresize" Not Working with Translucent Action/Navbar
How to Set Edittext to Only Accept Numeric Values in Android
Misbehavior When Trying to Store a String Set Using Sharedpreferences
Getview Returning Null When Fragment Has Been Created from an Activity
Best Way to Add Activity to an Android Project in Eclipse
Broadcast Receiver Within a Service
Reading an Image File into Bitmap from Sdcard, Why am I Getting a Nullpointerexception