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.
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);
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"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With