Search code examples
androidandroid-layoutandroid-toolbarmenuitemsearchview

Android SearchLayout is not appearing properly in toolbar?


I want to Make SearchView with RecyclerView My filtering function working perfect,but SearchLayout is not appearing properly.

I referred this link to integrate SearchView with RecyclerView

Here is my Menu.xml

    <?xml version="1.0" encoding="utf-8"?>
    <menu 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"
        tools:context="com.learn2crack.recyclerviewsearch.MainActivity">
        <item
            android:id="@+id/search"
            android:icon="@drawable/search_icon"
            android:title="Search"
            app:showAsAction="ifRoom|collapseActionView"
            app:actionViewClass="android.support.v7.widget.SearchView"/>
    </menu>

Here is My XML design code:

<?xml version="1.0" encoding="utf-8"?>
<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:background="@color/appyellow"
    tools:context="app.msupply.com.ideaurben.Activity.Report_Here">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbarmenu"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay"/>


    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:nestedScrollingEnabled="false"
        android:scrollbars="vertical" />
</LinearLayout>

Here is my java code:

  Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarmenu);
        this.setSupportActionBar(toolbar);
       // toolbar.setTitle("Report Search");
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("Report Search");
        getSupportActionBar().setDisplayShowTitleEnabled(true);




 @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.menu_main, menu);

        MenuItem search = menu.findItem(R.id.search);
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(search);
        searchView.setQueryHint("Search Here");
        searchView.setBackgroundColor(Color.WHITE);
        search(searchView);
     //   searchView.setOnQueryTextListener(this);

        return true;


    }

I want to make Searchview as same in that provided link. I followed almost the same but it is not coming properly. Here is my image


Solution

  • The Problem with your code is that you are using LinearLayout instead of a CoordinatorLayout All such kind of animations is the result of the latter one. Enjoy.