Skip to content Skip to sidebar Skip to footer

How To Keep Expanded Searchview On The Right Side Of Actionbar (while Using Custom Icon)?

My question is closely related to SearchView wrong alignment and SearchView positions when expanded in ActionBar but the answers posted there do not work. So, I'm using a SearchVie

Solution 1:

Alright, I found a way:

In onCreateOptionsMenu(), after the standard menu inflating stuff, set ActionBar.LayoutParams with Gravity.RIGHT for the SearchView instance:

@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {        
    MenuInflaterinflater= getMenuInflater();
    inflater.inflate(R.menu.main_activity_actions, menu);

    // ...// Find the SearchView; make it aligned to right, also in expanded mode:MenuItemitem= menu.findItem(R.id.action_search);
    SearchViewsearch= (SearchView) item.getActionView();
    search.setLayoutParams(newActionBar.LayoutParams(Gravity.RIGHT));
}

(In the menu definition XML, I still use android:showAsAction="ifRoom|collapseActionView".)

Now the SearchView opens on the right, in the same place where the icon is, and the custom icon is still in use.

Solution 2:

Try editing this line

android:showAsAction="ifRoom|collapseActionView"

to:

android:showAsAction="ifRoom"

it will align the edittext to the right.

Solution 3:

Using this worked for me:

finalSearchViewsearchView= (SearchView) searchMenuItem.getActionView();
searchView.setOnSearchClickListener(newView.OnClickListener()
{
     @OverridepublicvoidonClick(View v)
     {
         searchView.setLayoutParams(newToolbar.LayoutParams(Gravity.END));
     }
});

Post a Comment for "How To Keep Expanded Searchview On The Right Side Of Actionbar (while Using Custom Icon)?"