How to Set Edittext to Only Accept Numeric Values in Android

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:

Sample Image

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



Leave a reply



Submit