Material-components-android: TextInputEditText losing focus

Created on 5 Aug 2019  路  8Comments  路  Source: material-components/material-components-android

Description: TextInputEditText where the inputType="numberDecimal" inside ReyclerView item losing focus after keyboard popup so user can not enter any number value.

Expected behavior: User can enter numbers without any problem
https://youtu.be/qt21EWerrLI

Source code:
<com.google.android.material.textfield.TextInputLayout android:id="@+id/itemDeliveredOrder_til_amount" style="@style/DeliveredOrderTextInputLayoutStyle" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginEnd="16dp" app:layout_constraintEnd_toStartOf="@+id/guideline3" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText android:id="@+id/itemDeliveredOrder_tie_amount" style="@style/DeliveredOrderTextInputLayoutStyle" android:layout_width="match_parent" android:imeOptions="actionDone" android:layout_height="wrap_content" android:hint="@string/amount" android:inputType="numberDecimal" /> </com.google.android.material.textfield.TextInputLayout>

Android API version: Android API version here

Material Library version: 1.1.0-alpha08 & 1.1.0-alpha09

Device: Galaxy Note 9 (SM-N960F/DS)

To help us triage faster, please check to make sure you are using the latest version of the library.

bug

Most helpful comment

This is still happening when com.google.android.material.textfield.TextInputLayout used inside viewpager2 based on recyclerview.
Its not only inputType number, but for all inputTypes on first focus and when focus change from one TextInputEditText to another TextInputEditText with different inputTypes, then the focus is lost when keyboard shows up.

"com.google.android.material:material:1.2.0-alpha01"
"androidx.viewpager2:viewpager2:1.0.0"

All 8 comments

This is still happening when com.google.android.material.textfield.TextInputLayout used inside viewpager2 based on recyclerview.
Its not only inputType number, but for all inputTypes on first focus and when focus change from one TextInputEditText to another TextInputEditText with different inputTypes, then the focus is lost when keyboard shows up.

"com.google.android.material:material:1.2.0-alpha01"
"androidx.viewpager2:viewpager2:1.0.0"

I think this is due to the fact that the request focus that happens on the text input field will trigger a re-layout of the recyclerview, which in term triggers a scroll event with 0 dx.
In this the ViewPager2 assumes with the comment:
// onScrolled while IDLE means RV has just been populated after an adapter has been set.
// Contract requires us to fire onPageSelected as well.
Which is of course bs. So this triggers the onPageSelected which in turn calls this magical OnPageChangeCallback:
// Prevents focus from remaining on a no-longer visible page
final OnPageChangeCallback focusClearer = new OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
clearFocus();
if (hasFocus()) { // if clear focus did not succeed
mRecyclerView.requestFocus(View.FOCUS_FORWARD);
}
}
};

So maybe there is a way to remove this focus listener, or change the internal scroll listener to not trigger in this way,
Wouldn't it make more sense to use registerAdapterDataObserver on the adapter of the recyclerview and then trigger the onPageChanged from there?

Faced the same issue. Upgrading

'androidx.constraintlayout:constraintlayout:2.0.0-beta6'
'com.google.android.material:material:1.2.0-alpha06'

fixed the problem.

This becouse code use
View.searchFocus() and it use parent.searchFocus(this, direction). But parent is FrameLayout
if i use parent.parent.searchFocus its work ok!

this hack ))

            val a: TextInputLayout = view.parent.parent as TextInputLayout
                if (a.editText?.imeOptions != IME_ACTION_NEXT && a.editText?.isSingleLine == true)
                    a.editText?.imeOptions = IME_ACTION_NEXT

                if (a.focusSearch(view, ViewGroup.FOCUS_FORWARD).id == -1)
                    a.focusSearch(view, ViewGroup.FOCUS_FORWARD).id = ViewCompat.generateViewId()

                a.editText?.nextFocusForwardId = a.focusSearch(view, ViewGroup.FOCUS_FORWARD).id

This is still happening when com.google.android.material.textfield.TextInputLayout used inside viewpager2 based on recyclerview.
Its not only inputType number, but for all inputTypes on first focus and when focus change from one TextInputEditText to another TextInputEditText with different inputTypes, then the focus is lost when keyboard shows up.

"com.google.android.material:material:1.2.0-alpha01"
"androidx.viewpager2:viewpager2:1.0.0"

I can confirm this problem, I have three pages all with constraintlayout, one has just some labels, the second is a form of TextInputLayouts, the third contains RecyclerView.
'com.google.android.material:material:1.3.0-alpha01'
'androidx.viewpager2:viewpager2:1.1.0-alpha01'
'androidx.constraintlayout:constraintlayout:2.0.0-beta8'

After a page change (by user input or programatically), the first focus (again user input, or programatically, no matter the delay after page change), causes onPageSelected call of pager's internal listener that clears this focus.
I can workaround this for the focus requested from code by calling another posted requestFocus right after the first one, however this cancels the animation and it doesn't look good. This also solves the user input case, since I do it in the onResume of the page fragment and all the user interaction happens after this point.

Hey @devqmr, @ChairfaceChippendale, @AlmightyCZ

Any chance someone is able to provide a small sample that reproduces this in order to help us debug and work on a fix? It's extremely helpful when we don't have to set up a project and things for each issue we receive!

Thanks!

Hey @hunterstich it can be replicated using my old sample, that I made for a different ViewPager2 bug report. I just updated the libraries to current version. https://github.com/AlmightyCZ/ViewPager2TextInputGravity
When I start the app, switch to the FORM tab and then try to focus editor, it just opens the keyboard, but the editor still appears as unfocused and input from the keyboard is ignored. When you then try to focus editor again it works fine. But if you then change to another tab and back to FORM tab, the whole thing happens again. (this time you can even see the animation of editText getting focus immediately followed of loosing focus animation.
This described behaviour is for Android 6 tablet. When I run on Android 10 phone, it's similar, but in this case the input from the keyboard is not ignored and causes editText to regain focus.

This issue is a general issue and not specific to the material components. (s. https://github.com/android/views-widgets-samples/issues/107)

If you have a small number of pages there is the workaround to increase the offscreenPageLimit of the ViewPager2 (thanks to @FEvgenSON https://github.com/android/views-widgets-samples/issues/107#issuecomment-726809766)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TdevM picture TdevM  路  3Comments

ahmaducg picture ahmaducg  路  3Comments

sepehr-alipour picture sepehr-alipour  路  3Comments

danielwilson1702 picture danielwilson1702  路  3Comments

JavierSegoviaCordoba picture JavierSegoviaCordoba  路  3Comments