How to specify digits and imeOptions together in XML of Android Studio?

732 views Asked by At

In Android EditText I found a strange issue. I have put android:imeOptions="actionNext" and it worked correctly but when I specified digits as android:digits="abcdefghijklmnopqrstuvwxyz " I haven't Next option in the edit text. Why does it happen and what should I do to have imeOptions with digits?!

3

There are 3 answers

1
Amit Kundu On

Use android:inputType="number" and android:imeOptions="actionNext". This will work. If you want something different change the inputType (numberDecimal/number/numberPassword).

3
Muhammad El-Sayed On

I understand that you want to write android:digits="abcdefghijklmnopqrstuvwxyz " to Prevent user to type invalid characters .. if I Right , you have to use android:inputType=" attribute Or make ValidationClass.Java To filter user income ..

[+] For more information about android:inputType=" follow this this .

[+] For more information about android:imeOptions follow this Explanation with Video

0
ricindigus On

It's very simple. The part about restricting to letters only, don't do it for XML. Otherwise by code: "InputFilters", otherwise imeoptions and digits will not work together.

For example:(Work for TextInputEditText or EditText is the same)

1.XML: (here your imeOptions)

<com.google.android.material.textfield.TextInputEditText
                        android:id="@+id/etOwner"
                        android:layout_width="match_parent"
                        android:layout_height="@dimen/size_48"
                        android:maxLength="40"
                        android:imeOptions="actionNext"
                        android:hint="@string/hint_owner"/>

2.But, in code: Will create an input filter for the text input.

class InputLettersFilter.kt

class InputLettersFilter: InputFilter {
    override fun filter(
        cs: CharSequence,
        start: Int,
        end: Int,
        spanned: Spanned?,
        dStart: Int,
        dEnd: Int
    ): CharSequence? {
        var filtered = ""
        for (i in start until end) {
            val character = cs[i]
            if (Character.isWhitespace(character) || Character.isLetter(character)) {
                filtered += character
            }
        }
        return filtered
    }
}
  1. And lastly you add it to the input in your fragment , activity, etc.

    mBinding.etOwner.apply { filters = arrayOf(InputLettersFilter())}

digits and ImeOptions do not work together if digits refers to letters "ABC....abc..."