Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expand Search View to use entire Action Bar (hide other things)

I have a SearchView inside my ActionBar, and I want to use the entire ActionBar when the search icon is pressed, but I can only use the ActionBar free space

eg.: http://imgur.com/wnjMfWO

my menu code:

<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:context="br.com.moderna.houaiss.activity.SearchActivity" >  <item     android:id="@+id/search"     android:actionViewClass="android.widget.SearchView"     android:icon="@drawable/ic_search_white_48dp"     android:showAsAction="collapseActionView|always"     android:title="@string/search"/> <item     android:id="@+id/backWardHistory"     android:icon="@drawable/ic_arrow_back_white_48dp"     android:showAsAction="always"     android:title="@string/back_history"/> <item     android:id="@+id/forWardHistory"     android:icon="@drawable/ic_arrow_forward_white_48dp"     android:showAsAction="always"     android:title="@string/forward_history"/> <item     android:id="@+id/action_home"     android:orderInCategory="100"     android:showAsAction="never"     android:title="@string/action_home"/> <item     android:id="@+id/action_about"     android:orderInCategory="100"     android:showAsAction="never"     android:title="@string/action_about"/> <item     android:id="@+id/action_configuration"     android:orderInCategory="100"     android:showAsAction="never"     android:title="@string/action_configuration"/> <item     android:id="@+id/action_logout"     android:orderInCategory="100"     android:showAsAction="never"     android:title="@string/action_logout"/> 

I had tryed android:showAsAction="ifRoom", but I need them to be always on my ActionBar, and not inside my menu.

EDIT ------

Thanks to @Simas,

I used setOnActionExpandListener method to achieve this effect

final MenuItem searchItem = menu.findItem(R.id.search);      searchItem.setOnActionExpandListener(new OnActionExpandListener() {          @Override         public boolean onMenuItemActionExpand(final MenuItem item) {             SearchActivity.this.setItemsVisibility(menu, searchItem, false);             return true;         }          @Override         public boolean onMenuItemActionCollapse(final MenuItem item) {             SearchActivity.this.setItemsVisibility(menu, searchItem, true);             return true;         }     });   private void setItemsVisibility(final Menu menu, final MenuItem exception,         final boolean visible) {     for (int i = 0; i < menu.size(); ++i) {         MenuItem item = menu.getItem(i);         if (item != exception)             item.setVisible(visible);     } } 
like image 288
user3672263 Avatar asked Jun 01 '15 15:06

user3672263


People also ask

How do I customize search view?

just do your own search view, it is very simple. you then can include this layout in your activity layout file. This is a simple layout which includes a "search icon" followed by EditText, followed by "clear icon". The clear icon is shown after user types some text.

How Remove action bar from all activities?

If you want to hide Action Bar from the entire application (from all Activities and fragments), then you can use this method. Just go to res -> values -> styles. xml and change the base application to “Theme.

How do you display action bar back button on all versions of Android?

Show back button using actionBar. setDisplayHomeAsUpEnabled(true) this will enable the back button. Custom the back event at onOptionsItemSelected. This will enable the back function to the button on the press.


2 Answers

Well you could imitate that yourself by hiding all the other items when the SearchView is expanded:

@Override public boolean onCreateOptionsMenu(final Menu menu) {     getMenuInflater().inflate(R.menu.menu_main, menu);      final MenuItem searchItem = menu.findItem(R.id.search);     SearchView searchView = (android.widget.SearchView) searchItem.getActionView();      // Detect SearchView icon clicks     searchView.setOnSearchClickListener(new View.OnClickListener() {         @Override         public void onClick(View v) {             setItemsVisibility(menu, searchItem, false);         }     });     // Detect SearchView close     searchView.setOnCloseListener(new SearchView.OnCloseListener() {         @Override         public boolean onClose() {             setItemsVisibility(menu, searchItem, true);             return false;         }     });      return super.onCreateOptionsMenu(menu); }  private void setItemsVisibility(Menu menu, MenuItem exception, boolean visible) {     for (int i=0; i<menu.size(); ++i) {         MenuItem item = menu.getItem(i);         if (item != exception) item.setVisible(visible);     } } 
like image 129
Simas Avatar answered Sep 23 '22 21:09

Simas


This would be a late answer but you could add this attribute to your menu item and the work is done for you.

app:showAsAction="collapseActionView|always" 

Keyword here being the collapseActionView.

like image 21
PlanetAstro_William Avatar answered Sep 20 '22 21:09

PlanetAstro_William