How to Default to Numeric Keyboard on Edittext Without Forcing Numeric Input

How do I default to numeric keyboard on EditText without forcing numeric input?

Add the following line of code, and it will do the trick :)

editText.setRawInputType(Configuration.KEYBOARD_QWERTY);

This will show the the numeric keypad first, but also allows you to enter free text.

More information here.

Open a numeric keyboard without forcing the EditText to be numeric only

Not sure if this is the best solution, but I use android:inputType="phone".

Android EditText force numeric keyboard but allow non-numeric chars

This will keep it numeric but will allow the $ character, add any other special chars you need to the android:digits attribute

<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:digits="0123456789,$">

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

TextInputEditText to show numeric keyboard but also allow /

Removing android:inputType and android:digits from the xml and also adding the below to the activity's onCreate() seems to do the trick.

input.setKeyListener(DigitsKeyListener.getInstance("0123456789./"));

However it's not completely clear to me that this should or will always work - see note below from the docs on DigitsKeyListener...

As for all implementations of KeyListener, this class is only concerned with hardware keyboards. Software input methods have no obligation to trigger the methods in this class.

Source: https://developer.android.com/reference/android/text/method/DigitsKeyListener



Related Topics



Leave a reply



Submit