shnizlon
shnizlon

Reputation: 1526

Hiding keyboard in scrollview scrolling the page

I have a ScrollView layout with ViewPager and horizontal RecyclerViews inside, my problem is that when I hide the keyboard, the page scroll down a bit. I tried to change windowSoftInputMode to adjustNothing and it does fix the problem but then the keyboard hides the SearchView suggestions.

For some reason it only happens when the page reach a certain length, because it doesn't happen if I remove one RecyclerView for example or the ViewPager.

I found two workarounds for this issue, one is to change visibility of the layouts to gone when keyboard popup and back to visible when hiding it, and second one is to hide keyboard along with SearchView when the query text loses the focus, but I really want to find a proper fix for this problem.

Below is a video of the issue and the code of my layout:

https://www.youtube.com/watch?v=hyexsQz8ja0

<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none">

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

    <RelativeLayout
        android:id="@+id/fragment_courses_relativeLayout_viewPager"
        android:layout_height="200dp"
        android:layout_width="match_parent">

        <ViewPager
            android:id="@+id/main_catalog_view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true" />

        <InkPageIndicator
            android:id="@+id/main_catalog_indicator"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="10dp"
            android:visibility="gone"/>
    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/fragment_courses_software_see_all"
        android:background="?attr/selectableItemBackground"
        android:visibility="gone"
        android:clickable="true"
        android:minHeight="48.0dip">
        <FrameLayout
            android:layout_gravity="center_vertical"
            android:orientation="horizontal"
            android:layout_width="0.0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1.0">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/course_type_software"
                android:textSize="20.0sp"
                android:textColor="?android:textColorPrimary"
                android:layout_marginStart="16dp" />
        </FrameLayout>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/see_all"
            android:textColor="@color/colorAccent"
            android:textAppearance="@style/TextAppearance.Button"
            android:layout_gravity="center_vertical"
            android:layout_marginEnd="@dimen/activity_horizontal_margin"
            android:layout_marginStart="@dimen/activity_horizontal_margin" />
    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/fragment_courses_software_recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:paddingTop="4dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/fragment_courses_cyber_see_all"
        android:background="?attr/selectableItemBackground"
        android:visibility="gone"
        android:clickable="true"
        android:minHeight="48.0dip">
        <FrameLayout
            android:layout_gravity="center_vertical"
            android:orientation="horizontal"
            android:layout_width="0.0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1.0">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/course_type_cyber"
                android:textSize="20.0sp"
                android:textColor="?android:textColorPrimary"
                android:layout_marginStart="16dp" />
        </FrameLayout>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/see_all"
            android:textColor="@color/colorAccent"
            android:textAppearance="@style/TextAppearance.Button"
            android:layout_gravity="center_vertical"
            android:layout_marginEnd="@dimen/activity_horizontal_margin"
            android:layout_marginStart="@dimen/activity_horizontal_margin" />
    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/fragment_courses_cyber_recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:paddingTop="4dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/fragment_courses_it_see_all"
        android:background="?attr/selectableItemBackground"
        android:visibility="gone"
        android:clickable="true"
        android:minHeight="48.0dip">
        <FrameLayout
            android:layout_gravity="center_vertical"
            android:orientation="horizontal"
            android:layout_width="0.0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1.0">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/course_type_it"
                android:textSize="20.0sp"
                android:textColor="?android:textColorPrimary"
                android:layout_marginStart="16dp" />
        </FrameLayout>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/see_all"
            android:textColor="@color/colorAccent"
            android:textAppearance="@style/TextAppearance.Button"
            android:layout_gravity="center_vertical"
            android:layout_marginEnd="@dimen/activity_horizontal_margin"
            android:layout_marginStart="@dimen/activity_horizontal_margin" />
    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/fragment_courses_it_recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:paddingTop="4dp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/fragment_courses_tools_see_all"
        android:background="?attr/selectableItemBackground"
        android:visibility="gone"
        android:clickable="true"
        android:minHeight="48.0dip">
        <FrameLayout
            android:layout_gravity="center_vertical"
            android:orientation="horizontal"
            android:layout_width="0.0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1.0">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/course_type_tools"
                android:textSize="20.0sp"
                android:textColor="?android:textColorPrimary"
                android:layout_marginStart="16dp" />
        </FrameLayout>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/see_all"
            android:textColor="@color/colorAccent"
            android:textAppearance="@style/TextAppearance.Button"
            android:layout_gravity="center_vertical"
            android:layout_marginEnd="@dimen/activity_horizontal_margin"
            android:layout_marginStart="@dimen/activity_horizontal_margin" />
    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/fragment_courses_tools_recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:paddingTop="4dp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/fragment_courses_system_see_all"
        android:background="?attr/selectableItemBackground"
        android:visibility="gone"
        android:clickable="true"
        android:minHeight="48.0dip">
        <FrameLayout
            android:layout_gravity="center_vertical"
            android:orientation="horizontal"
            android:layout_width="0.0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1.0">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/course_type_system"
                android:textSize="20.0sp"
                android:textColor="?android:textColorPrimary"
                android:layout_marginStart="16dp" />
        </FrameLayout>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/see_all"
            android:textColor="@color/colorAccent"
            android:textAppearance="@style/TextAppearance.Button"
            android:layout_gravity="center_vertical"
            android:layout_marginEnd="@dimen/activity_horizontal_margin"
            android:layout_marginStart="@dimen/activity_horizontal_margin" />
    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/fragment_courses_system_recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:paddingTop="4dp"/>

</LinearLayout>

Upvotes: 1

Views: 1354

Answers (1)

shnizlon
shnizlon

Reputation: 1526

I managed to fix the problem by adding the following attribute to the top most view of the ScrollView, which in my case was LinearLayout:

android:focusableInTouchMode="true"

Turns out this issue was with the RecyclerViews inside the ScrollView and it was reported to Google here Issue 81854

Upvotes: 1

Related Questions