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 u
Alright, I found a way:
In onCreateOptionsMenu()
, after the standard menu inflating stuff, set
ActionBar.LayoutParams with Gravity.RIGHT
for the SearchView
instance:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
// ...
// Find the SearchView; make it aligned to right, also in expanded mode:
MenuItem item = menu.findItem(R.id.action_search);
SearchView search = (SearchView) item.getActionView();
search.setLayoutParams(new ActionBar.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.
Try editing this line
android:showAsAction="ifRoom|collapseActionView"
to:
android:showAsAction="ifRoom"
it will align the edittext to the right.
Using this worked for me:
final SearchView searchView = (SearchView) searchMenuItem.getActionView();
searchView.setOnSearchClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
searchView.setLayoutParams(new Toolbar.LayoutParams(Gravity.END));
}
});