Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

requestFocus() for EditText not working on devices running android P (API 28)

Tags:

android

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

like image 424
Yogesh Katkar Avatar asked Aug 29 '18 09:08

Yogesh Katkar


3 Answers

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.

like image 78
Hanoch Moreno Avatar answered Oct 05 '22 22:10

Hanoch Moreno


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.

like image 29
Ankur Chaudhary Avatar answered Oct 05 '22 22:10

Ankur Chaudhary


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.

like image 28
Abdul Rehman Yawar Khan Avatar answered Oct 06 '22 00:10

Abdul Rehman Yawar Khan