Search code examples
androidmaterial-components-androidandroid-textinputlayoutandroid-nestedscrollviewandroid-textinputedittext

Android: bottom of TextInputEditText always scrolling to top of softkeyboard


I'm using a TextInputEditText inside a TextInputLayout, which is inside a NestedScrollView:

<androidx.core.widget.NestedScrollView
    android:id="@+id/nsv"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/toolbar">

    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:orientation="vertical">

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/textField"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@{hint}"
            android:padding="@dimen/padding_items"
            app:startIconContentDescription="@{iconDescription}"
            app:startIconDrawable="@{icon}">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/et"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:importantForAutofill="no"
                android:text="@={text}" />
        </com.google.android.material.textfield.TextInputLayout>
    </LinearLayout>
</androidx.core.widget.NestedScrollView>

When I enter a long text, so that the TextInputEditText takes more vertical space than is available above the softkeyboard, the upper part of the TextInputEditText scrolls out of view. This is perfectly fine.

But if I manually scroll up the nestedScrollView so that I can edit the first words of the TextInputEditText, it scrolls back all the time and this happens whenever I click (= set the cursor) into the TextInputEditText or enter a letter.

The bottom edge of the TextInputEditText seems to align with the top edge of the softkeyboard every time I interact with it, which is why the top edge scrolls out of view even when the cursor is placed at the very beginning.

This makes it impossible to edit the beginning of the long string, because you have to scroll up after each letter and see what happens, or you have to type blindly.

How can I work around this?

Steps to reproduce:

  1. enter long text
  2. scroll to the beginning of the text
  3. place cursor at the beginning of the TextInputEditText
  4. scroll again to the beginning of the text so that cursor is visible: cursor at the beginning of the TextInputEditText
  5. type letters: cursor no longer visible, because scrolled out of view

Solution

  • I was stumped by this for a long time, too. The key is to add the following to your TextInputEditText

    app:textInputLayoutFocusedRectEnabled="false"
    

    as described here: Android TextInputLayout writing focus issue when height is bigger than the screen