Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Edittext that only allows a digit from 1-9 [duplicate]

I am looking for a way so that users can only input a number between 1-9 with edittext. No exception should be displayed. It should not be possible to type in more than one digit.

like image 893
lob Avatar asked Jun 05 '17 10:06

lob


2 Answers

Ther have two ways for your question 1) in your XML fayl

<EditText
  android:id="@+id/edittext"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:digits="123456789"
  android:inputType="number"
  android:maxLength="1"/>

2) in your Activity class

    mEdit.setInputType(InputType.TYPE_CLASS_NUMBER );
    mEdit.setKeyListener(DigitsKeyListener.getInstance("0123456789"));
    mEdit.setSingleLine(true);
like image 183
Faxriddin Abdullayev Avatar answered Oct 13 '22 01:10

Faxriddin Abdullayev


1. Use attribute android:inputType="number" to take input only number 0-9

2. Use attribute android:digits="123456789" to take only 1-9

3. Use attribute android:maxLength="1" to allow user to input only a single digit.

Try this:

<EditText
    android:id="@+id/edittext"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:digits="123456789"
    android:maxLength="1"/>
like image 42
Ferdous Ahamed Avatar answered Oct 13 '22 01:10

Ferdous Ahamed