Search code examples
androidandroid-layoutandroid-softkeyboardandroid-textinputlayoutandroid-textinputedittext

Android TextInputLayout writing focus issue when height is bigger than the screen


I have a TextInputLayout like this:

<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/textInputLayout"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="200dp">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/textInputLayoutEdit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="200dp"
            android:gravity="top" />
</com.google.android.material.textfield.TextInputLayout>

This TextInputLayout's height is dynamic, as in, the height will grow when more text is being written. So in a case where the height is becoming bigger than the screen, and scrolling would be needed in order to see the first line of the TextInputLayout, there is an issue with writing focusing. When writing all the way at top, the screen focus will go all the way to the bottom while the cursor is still at top. This prevents me from seeing what is being written at top because the screen is being scrolled to the bottom of the TextInputLayout. And sometimes the focus goes back to the top, almost like a glitchy flickering.

Writing at bottom works as normal, since bottom screen focus is always prioritized. Writing at top when the TextInputLayout's height has not exceeded the screen yet, also works fine since there is no need to scroll.

Is there a property I need to set in TextInputLayout, in order to make the screen focus where I am writing?

UPDATE 1:

It seems to be related to the keyboard always being right below the bottom of the TextInputLayout. So when typing, the keyboard will always want to be at bottom, and writing at top will just pull the screen focus all the way to bottom where the keyboard is being placed.

A way to prevent the keyboard from being fixed under the TextInputLayout is to set the following in the Activity (AndroidManifest.xml):

android:windowSoftInputMode="adjustPan"

This will prevent the need for the keyboard to force up the layout upwards so that it can fit in. This will solve my problem of being "pulled down all the way down", when writing, even though I am writing all the way at top and the bottom is not visible due to the big height. But... fixing the keyboard like this will prevent me from seeing what I am writing at bottom, since the keyboard is blocking what I am writing. So ideally, I still want the keyboard to automatically make space at bottom for the keyboard, and at the same time I don't want the keyboard to pull the screen focus all the way down.

UPDATE 2 (SOLVED):

I managed to solve this and the solution can be seen in my answer below.

TL;DR, add the following in TextInputEditText:

app:textInputLayoutFocusedRectEnabled="false"

Solution

  • After playing around with many of the TextInputLayout's properties, I managed to find the solution. The key is to set app:textInputLayoutFocusedRectEnabled to false, in the TextInputEditText:

    app:textInputLayoutFocusedRectEnabled="false"
    

    This will make it possible to achieve what I wanted with my TextInputLayout, which was:

    • Be able to write text in a TextInputLayout that grows based on input length.
    • Be able to write anywhere in the input field (in cases where the text is very long), and not be dragged down all the way to the bottom, because the keyboard is trying to be placed below the TextInputLayout. (Or it can also look like it is "glitching", by pulling the screen to the middle of the TextInputLayout or all the way to the bottom, from the place you are writing at. Like if the screen is scrolling up and down very fast for every character you type.)
    • Be able to have the keyboard at any position throughout the entire TextInputLayout, as well as right below the TextInputLayout, as if the keyboard is pushing up the entire layout.

    Hopefully this Answer to my Question will be able to help others who look for the specific problem I ran into!