Search code examples
androidscrollviewhinttextinputlayout

Android TextInputLayout with EditText in ScrollView hides hint on focus


if I focus a EditText within a ScrollView this happens (thats fine): enter image description here

Then, I focus the EditText below and refocus EditText from picture one, then this happens: enter image description here

The hint is not shown or badly (depending on scrollview).

Part of the solution: The Scrollview sees that I want to focus the EditText but not the surrounded android.support.design.widget.TextInputLayout element. Therefore it does not care if the hint is being shown.

How to fix? My XML (basically a scrollview with some linearlayouts containing android.support.design.widget.TextInputLayout with EditTexts):

<ScrollView
            android:layout_width="match_parent"
            android:fadeScrollbars="false"
            android:layout_height="wrap_content">

            <LinearLayout
                android:layout_width="match_parent"
                android:orientation="vertical"
                android:layout_height="match_parent">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:orientation="horizontal"
                    android:focusable="true"
                    android:focusableInTouchMode="true"
                    android:weightSum="9"
                    android:descendantFocusability="beforeDescendants"
                    android:layout_height="match_parent">

                    <TextView
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:text="1"
                        android:gravity="center"
                        android:layout_height="match_parent" />

                    <android.support.design.widget.TextInputLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="4">

                        <EditText
                            android:layout_width="match_parent"
                            android:layout_weight="1"
                            android:maxLines="1"
                            android:hint="hint1.1"
                            android:inputType="numberDecimal"
                            android:layout_height="match_parent" />

                    </android.support.design.widget.TextInputLayout>

                    <android.support.design.widget.TextInputLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="4">

                        <EditText
                            android:layout_width="match_parent"
                            android:maxLines="1"
                            android:inputType="number"
                            android:hint="hint1.2"
                            android:layout_height="match_parent" />


                    </android.support.design.widget.TextInputLayout>

                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:orientation="horizontal"
                    android:weightSum="9"
                    android:layout_height="match_parent">

                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:text="2"
                        android:gravity="center"/>

                    <android.support.design.widget.TextInputLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="4">

                        <EditText
                            android:layout_width="match_parent"
                            android:layout_weight="1"
                            android:maxLines="1"
                            android:hint="hint2.1"
                            android:inputType="numberDecimal"
                            android:layout_height="match_parent" />

                    </android.support.design.widget.TextInputLayout>

                    <android.support.design.widget.TextInputLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="4">

                        <EditText
                            android:layout_width="match_parent"
                            android:layout_weight="1"
                            android:maxLines="1"
                            android:hint="hint2.2"
                            android:inputType="number"
                            android:layout_height="match_parent" />

                    </android.support.design.widget.TextInputLayout>

                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:orientation="horizontal"
                    android:weightSum="9"
                    android:layout_height="match_parent">

                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:text="3"/>

                    <android.support.design.widget.TextInputLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="4">

                        <EditText
                            android:layout_width="match_parent"
                            android:hint="hint3.1"
                            android:maxLines="1"
                            android:inputType="numberDecimal"
                            android:layout_height="match_parent" />

                    </android.support.design.widget.TextInputLayout>

                    <android.support.design.widget.TextInputLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="4">

                        <EditText
                            android:layout_width="match_parent"
                            android:layout_weight="1"
                            android:hint="hint3.2"
                            android:maxLines="1"
                            android:inputType="number"
                            android:layout_height="match_parent" />

                    </android.support.design.widget.TextInputLayout>

                </LinearLayout>

            </LinearLayout>

        </ScrollView>

Solution

  • Use this code to scroll to proper position of your edittext view,

    your_scrollview.post(new Runnable() { 
    @Override
     public void run() { 
           your_scrollview.smoothScrollTo(0, your_EditBox.getBottom());
     } 
    });