Search code examples
androidandroid-actionbarsearchviewnavigationview

Action bar items are showing in NavigationView and unable to hide 3 dots from action bar


I have NavigationView and search filter on Action bar in same page(Home page), After adding search item on action bar its showing in both action bar and navigation view(menu). And I am unable hide the 3 dots on the right side of the action bar which I don't want. Find the below code and screen shot

activity_main_drawer.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">

    <group android:id="@+id/grp1">
        <item
            android:id="@+id/nav_camera"
            android:icon="@drawable/theme"
            android:title="@string/nav_theme" />
    </group>

    <group android:id="@+id/grp2">
        <item
            android:id="@+id/nav_gallery"
            android:icon="@drawable/fingerprint"
            android:title="@string/nav_touchid" />
    </group>

    <group android:id="@+id/grp3">
        <item
            android:id="@+id/nav_contact"
            android:icon="@drawable/contact_us"
            android:title="@string/nav_contacts" />
    </group>
    <group android:id="@+id/grp4">
        <item
            android:id="@+id/nav_manage"
            android:icon="@drawable/notification"
            android:title="@string/nav_notifications" />
    </group>
    <group android:id="@+id/grp5">
        <item
            android:id="@+id/nav_user_guide"
            android:icon="@drawable/user_guide"
            android:title="@string/nav_userguide" />
    </group>
    <group android:id="@+id/grp6">
        <item
            android:id="@+id/nav_logout"
            android:icon="@drawable/logout"
            android:title="Log Out" />

    </group>

    <item
        android:id="@+id/action_search"
        android:icon="@drawable/searchicon"
        android:title="Search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="always" />
</menu>

MainActivity.java

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main_drawer, menu);
        // Retrieve the SearchView and plug it into SearchManager
        final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
        SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

enter image description here


Solution

  • You're using the same Menu file for Navigation drawer as well as Menu Items.

    Create a different menu file R.menu.menu_main or whatever you want for your Menu items(the one with three dots), this will contain the SearchView.

    Create another menu file R.menu.menu_navigation for Navigation drawer, this will contain the items Camera, Gallery, Contacts, Connect, and then attach this Menu file to the Navigation drawer.