0

So I have an activity with some EditTexts and ChipsInputs, I want these to scroll to the very top when active so my filterable list has room to show up. It works perfectly with the following structure. The problem is that now I need to convert this into a fragment, but being in a fragment it does not present the same behaviour. How to achieve the same result in a fragment?

This works in an activity:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/background1"
        android:orientation="vertical">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:paddingHorizontal="5dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                tools:targetApi="o">

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_marginHorizontal="12dp"
                    android:layout_marginVertical="5dp"
                    android:background="@color/black" />

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

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="10dp"
                        android:layout_marginLeft="10dp"
                        android:fontFamily="@font/quattrocento"
                        android:text="@string/about_me"
                        android:textColor="@color/textColor2"
                        android:textSize="20sp" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@color/white"
                        android:orientation="vertical">

                        <EditText
                            android:id="@+id/description"
                            android:layout_width="match_parent"
                            android:layout_height="70dp"
                            android:autofillHints="no"
                            android:background="@color/white"
                            android:inputType="text"
                            android:maxLength="500"
                            tools:ignore="LabelFor" />

                        <TextView
                            android:id="@+id/caracCounter"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="end"
                            android:fontFamily="@font/quattrocento"
                            android:text="500"
                            android:textColor="@color/black"
                            android:textSize="16sp" />
                    </LinearLayout>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="10dp"
                    android:gravity="center"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:fontFamily="@font/quattrocento"
                        android:gravity="center"
                        android:text="@string/interest1"
                        android:textColor="@color/textColor2"
                        android:textSize="20sp" />

                    <Spinner
                        android:id="@+id/categories1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginBottom="10dp"
                        android:background="@color/colorPrimary"
                        android:gravity="center" />

                    <com.pchmn.materialchips.ChipsInput
                        android:id="@+id/chipsInput1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@color/white"
                        android:elevation="3dp"
                        app:chip_backgroundColor="@color/colorPrimary"
                        app:chip_deletable="true"
                        app:chip_deleteIconColor="@color/black"
                        app:chip_hasAvatarIcon="false"
                        app:chip_labelColor="@color/black"
                        app:filterable_list_backgroundColor="@color/background1"
                        app:filterable_list_textColor="@color/colorPrimary"
                        app:hint="#"
                        app:maxRows="3"/>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="10dp"
                    android:gravity="center"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:fontFamily="@font/quattrocento"
                        android:gravity="center"
                        android:text="@string/interest2"
                        android:textColor="@color/textColor2"
                        android:textSize="20sp" />

                    <Spinner
                        android:id="@+id/categories2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginBottom="10dp"
                        android:background="@color/colorPrimary"
                        android:gravity="center" />

                    <com.pchmn.materialchips.ChipsInput
                        android:id="@+id/chipsInput2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="15dp"
                        android:background="@color/white"
                        android:elevation="3dp"
                        app:chip_backgroundColor="@color/colorPrimary"
                        app:chip_deletable="true"
                        app:chip_deleteIconColor="@color/black"
                        app:chip_hasAvatarIcon="false"
                        app:chip_labelColor="@color/black"
                        app:filterable_list_backgroundColor="@color/background1"
                        app:filterable_list_textColor="@color/colorPrimary"
                        app:hint="#" />
                </LinearLayout>

                <Button
                    android:id="@+id/btLogout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:layout_marginBottom="5dp"
                    android:background="@color/colorPrimary"
                    android:elevation="12dp"
                    android:fontFamily="@font/quattrocento"
                    android:onClick="logout"
                    android:text="@string/logout"
                    android:textColor="@color/black"
                    android:textSize="20sp"
                    tools:targetApi="lollipop" />
            </LinearLayout>
        </ScrollView>
    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

What should I do to achieve the same results in a fragment? The keyboard covers the entire filterable list, which is displayed below the EditText.

  • Have you tried this ? https://stackoverflow.com/questions/1748977/making-textview-scrollable-on-android – Vikas Oct 02 '19 at 12:48

2 Answers2

0

you need to call editText.requestFocus();
so that the activity will get scrolled to editText

or you can try

nestedScrollView.fling(0);
nestedScrollView.smoothScrollTo(0, 0);
Amin Pinjari
  • 2,129
  • 3
  • 25
  • 53
0

Try this in your onCreateView method
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

I hope it will helps you.

Alireza Bideli
  • 834
  • 2
  • 9
  • 31