Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide navigation drawer toggle button when search view expands

I want my search view widget to fill the entire toolbar so in menu.xml I did this:

...
app:showAsAction="always"

This seem to hide all other items in the toolbar when search view expands, but not the drawer toggle button:

enter image description here

Is there anyway I can make the search view widget fill the entire toolbar and hide the toggle button too? Thanks

like image 443
Toni Joe Avatar asked Nov 20 '25 01:11

Toni Joe


1 Answers

You can achieve this by hiding the hamburger button. Set your toolbar navigation icon to null.

toolbar.setNavigationIcon(null);

EDIT

To remove the navigation icon, add the following line in your onCreate method.

getActionBar().setDisplayHomeAsUpEnabled(false);

or

getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(false);

To make the SearchView fill the action bar, set the width of the SearchView to max in the onCreateOptionsMenu method.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_search, menu);
SearchView searchView = 
(SearchView)menu.findItem(R.id.menu_search).getActionView();
searchView.setMaxWidth(Integer.MAX_VALUE);
like image 113
Abhi Avatar answered Nov 22 '25 15:11

Abhi