Search code examples
androidandroid-layoutandroid-actionbaralignmentsearchview

SearchView wrong alignment


I'm having a problem with the alignment of the SearchView when it's expanded. For some reason, when collapsed it aligns rights, but when expanded it aligns left. I did everything folowing the ActionBarSherlock examples.

Here are two screenshots of the problem:

Screenshot collapsed http://img59.imageshack.us/img59/8976/.png

Screenshot expanded http://img577.imageshack.us/img577/9890/.png

I have yet to decide if I want to use ActionBar tabs. I don't want the searchView to hide them when expanded. But that's for another question. My main issue here is the alignment of the SearchView when exapanded. I want it to keep the right alignment no matter what.

Here's my onCreateOptionsMenu:

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getSupportMenuInflater().inflate(R.menu.menu_main, menu);
    searchMenuItem = menu.findItem(R.id.action_search);
    refreshMenuItem = menu.findItem(R.id.action_refresh);

    refreshMenuItem.setActionView(R.layout.refresh_progressbar);

    searchMenuItem.setOnActionExpandListener(new OnActionExpandListener() {

        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            flightsListAdapter.getFilter().filter("");
            return true;
        }
    }); 

    searchView = (SearchView)searchMenuItem.getActionView();
    searchView.setQueryHint(getResources().getString(R.string.search_hint));
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            searchView.clearFocus();
            return true;
        }

        @Override
        public boolean onQueryTextChange(String query) {
            flightsListAdapter.getFilter().filter(query);
            return true;
        }
    });
    searchView.clearFocus();

    return true;
}

And here is my menu_main.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/action_search"
    android:title="@string/search"
    android:icon="@drawable/ic_search"
    android:showAsAction="always|collapseActionView"
    android:actionViewClass="com.actionbarsherlock.widget.SearchView" />

<item
    android:title="@string/date"
    android:icon="@drawable/ic_compose"
    android:showAsAction="always" >
    <menu>
        <item
            android:id="@+id/menuYesterday"
            android:title="Yesterday"/>
        <item
            android:id="@+id/menuToday"
            android:title="Today"/>
        <item
            android:id="@+id/menuTomorrow"
            android:title="Tomorrow"/>
    </menu>
</item>

<item
    android:id="@+id/action_refresh"
    android:icon="@drawable/ic_refresh"
    android:showAsAction="always"
    android:title="@string/action_refresh" />
</menu>

Thank you for reading :)


Solution

  • Ok. Just for the sake of common knowledge, I'm answering my own question.

    The trick was to remove "collapseActionView" on the SerchView. Once I removed that, the SearchView stays in place (to the right) when exanded.