Search code examples
androidandroid-actionbarsearchview

Problems with actionbar and searchview


I'm using actionBar and searchView, and this is my code:

public class MainActivity extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedBundle) {

    super.onCreate(savedBundle);
    setContentView(R.layout.main_layout);

    setupImageLoader();
    setupSlidingMenu();

}

....

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);
    return true;
}

}

and my menu file is:

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
        android:id="@+id/search"
        android:icon="@drawable/ic_search"
        android:showAsAction="collapseActionView|ifRoom"
        android:actionViewClass="android.widget.SearchView"/>
</menu>

Now what I see is this:

enter image description here enter image description here

When not selected the SearchView is on the bottom of the screen, and it becames part of the actionbar only when active. How can I embed it like in Play store?


Solution

  • From your java-code, it seems that you are using FragmentActivity and thus the android-support library. But your selected SearchView-class is from the native Android framework.

    Look at the developer-docs to see how it should be done:

    http://developer.android.com/guide/topics/ui/actionbar.html#ActionView

    Also, it seems you have the splitActionbar enabled, or else your search-button would appear in the upper actionbar. So check your AndroidManifest.xml for something like this:

    <activity uiOptions="splitActionBarWhenNarrow" ... >
        <meta-data android:name="android.support.UI_OPTIONS"
                   android:value="splitActionBarWhenNarrow" />
    </activity>