Java – Hides the search bar when the ListView scrolls

Hides the search bar when the ListView scrolls… here is a solution to the problem.

Hides the search bar when the ListView scrolls

How to add a search bar in a listView, hidden when scrolling?

enter image description here

Here is my code xml:

<LinearLayout 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:orientation="vertical">

<android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="50dp">

<SearchView
        android:id="@+id/maBtnSearch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:iconifiedByDefault="false"
        android:queryHint="Search User" />

<ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listView">
    </ListView>

</LinearLayout>

Here is my full layout code above, thanks for your help

Solution

Try this. Here the searchview is placed on the toolbar. Change the code according to your purpose.

 <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:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<android.support.v4.view.ViewPager
        android:background="@drawable/back"
        android:id="@+id/pager_sessions"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    </android.support.v4.view.ViewPager>

<android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:id="@+id/search_edit_frame"
        android:layout_height="wrap_content">

<android.support.v7.widget.Toolbar
            android:id="@+id/toolbar_sessions"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways">
            <android.support.v7.widget.SearchView
                android:id="@+id/search_item"
                android:layout_gravity="right"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            </android.support.v7.widget.SearchView>
        </android.support.v7.widget.Toolbar>

<android.support.design.widget.TabLayout
            app:layout_scrollFlags="scroll|enterAlways"
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:tabIndicatorColor="@android:color/background_light"
            app:tabSelectedTextColor="@android:color/background_light"
            app:tabTextColor="@android:color/background_light">
        </android.support.design.widget.TabLayout>

</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

Related Problems and Solutions