4

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, ...)

Anael
  • 417
  • 1
  • 6
  • 18
  • it's interesting that you say this issue only occurs on a specific device, so i guess this isn't a duplicate and something people should consider when reading the question – a_local_nobody Feb 10 '21 at 10:53
  • @a_local_nobody Indeed, there's thousands of post asking this question. I am an experienced Android developer and have done this a million times. My question is really specific to this device (and/or other Samsung devices?). – Anael Feb 10 '21 at 12:55

2 Answers2

4

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"
Hasan
  • 519
  • 1
  • 6
  • 17
1

It seems to work well with this configuration:

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

That's a good enough workaround for me.

Anael
  • 417
  • 1
  • 6
  • 18