How to keep expanded SearchView on the right side of ActionBar (while using custom icon)?

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.