10

I am using EditText's to accept an OTP, where user has focus on next EditText once he enters a digit to a field and so. It works fine on all devices. But on devices running android OS P i.e. API 28, requestFocus() does not work, and user is not able to enter digits to consecutive EditTexts as focus doesn't move automatically.

Here is the code - by default all EditText's are disable to prevent from opening system keyboard. I am using my own CustomKeybaord to accept numbers. However it works except Android P.

mEtCode1.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            Log.d("BEFORE_", charSequence.toString());
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            hideError(charSequence.toString());

            if (!charSequence.toString().isEmpty()) {
                mEtCode2.requestFocus();
                mEtCode1.setBackground(getResources().getDrawable(R.drawable.verify_code_edit_text_background));
                mEtCode2.setBackground(getResources().getDrawable(R.drawable.verify_code_edit_text_background));
                mEtCode1.setEnabled(false);
            }

        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });

Please help me with this

Thank you, in advance

Abdul Rehman Yawar Khan
  • 1,088
  • 3
  • 17
  • 40
Yogesh Katkar
  • 369
  • 1
  • 4
  • 16

4 Answers4

17

I had the same issue with my OTP screen on devices with Android P sdk. The problem was that i set the height and width of the editText to 0dp, which is focus disabling in android P, as described in Android Developer page in the Android P change log: android-9.0-changes-28#ui-changes

Views with 0 area (either a width or a height is 0) are no longer focusable.

Hanoch Moreno
  • 593
  • 7
  • 9
2

This is issue with Android P. And what worked for me is the following code block, So sharing here:

enterOtpTextFrame.postDelayed(Runnable {
            enterOtpTextFrame.requestFocus()
        }, 100
        )

we require to call requestFocus with postDelayed with some small time amount. In my case it is 100 millisecond.

Ankur Chaudhary
  • 2,709
  • 3
  • 19
  • 30
1

Here is the official documentation for requestFocus method. It states there that it only works if the desired view for which you want to have focus is enabled, has size, is visible, is focusable and is FocusableInTouchMode.

I had exact same issue. I was using databinding to set the enable state of my EditText. I realised that requestFocus was not working because databinding, due to some unknown reasons, was not enabling my textview in time.

Here is my code:

/*
    setMyEditTextEnabled is my method to which my view is binded i.e.
    android:enabled="@{vm.myEditTextEnabled, default=false}"
    This worked for all version except Android P because it has
    some timing issues with API 28 (not sure what)
 */
        //binding.getVm().setMyEditTextEnabled(true);
/*
    So to make it work, I am enabling my EditText directly and
    it works for all versions.
 */
        binding.myEditText.setEnabled(true);
        binding.myEditText.requestFocus();

Also, as mentioned in following post: EditText requestFocus not working Do set focusable and focusableInTouchMode to true as well.

In short, my point is to make sure that your edit text is fulfilling all requirements as mentioned in official doc in order to requestFocus to work.

Abdul Rehman Yawar Khan
  • 1,088
  • 3
  • 17
  • 40
0

Encountered a similar problem when popping an alert dialog. In my case, postDelayed'ing a focus request or forcing the soft keyboard to pop up didn't work. Even if I could manage to pop up the keyboard, the focus stayed on an EditText in the main activity; needless to say I have tried clearing its focus and even disabling it. However, popping the alertDialog delayed did the trick:

final AlertDialog alertDialog = alertDialogBuilder.create(); //a builder of your own
final Runnable r = new Runnable() {
    public void run() {
    alertDialog.show();
  }
};
editTextOnMainActivity.postDelayed(r, 100);
Dharman
  • 30,962
  • 25
  • 85
  • 135