Anael
Anael

Reputation: 470

Android EditText - Input with numbers only

I created an EditText that only allows numbers as an input. It's a very simple EditText with nothing special:

<EditText
        android:id="@+id/editTextAmount"
        android:layout_marginTop="10dp"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:background="@color/colorWhite"
        android:inputType="text"
        android:digits="0123456789"
        />

I tried it on a few devices and it's working as expected, I can enter only numbers. The issue arise when using a Samsung Galaxy Note S10+ (with a Samsung keyboard, I think the issue comes from there). With that device when I focus the EditText the keyboard opens (with only number visible) but when I type something in the field nothing happens. I can press any key from the keyboard (enter key, numbers, ...) nothing changes.

I tried to change the input type programmatically in the activity, I tried to fiddle with the inputType (numberDecimal, numberSigned, both, ...) in any case it is simply not working. The interesting thing is that if I change the inputType to "text" and remove the digits restriction then it is working perfectly.

My question is: How can I limit the EditText input to numbers with a Samsung Galaxy Note S10+?

EDIT: Just to make it perfectly clear, I am asking this for a specific device! I already tried:

android:inputType="number"

And

editText.setInputType(InputType.TYPE_CLASS_NUMBER);

And any variation of those (numberDecimal, numberSigned, ...)

Upvotes: 4

Views: 6231

Answers (2)

Anael
Anael

Reputation: 470

It seems to work well with this configuration:

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

That's a good enough workaround for me.

Upvotes: 1

Hasan
Hasan

Reputation: 520

in Java class set input type like this

editText.setInputType(InputType.TYPE_CLASS_NUMBER);

or if you want to do that from layout then set

android:inputType="number"

Upvotes: 4

Related Questions