Search code examples
androidandroid-actionbarsearchview

Display SearchView in ActionBar Android


I am trying to display a SearchView inside the ActionBar of my activity. I followed android developer guide here here But no SearchView is displayed and I cannot figure out why. Here there is the code of my activity

    public class SearchActivity extends ActionBarActivity {


    private String query;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        FacebookSdk.sdkInitialize(getApplicationContext());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_activity_layout);


        Intent intent = getIntent();
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            this.query = intent.getStringExtra(SearchManager.QUERY);
            Toast.makeText(this,query, Toast.LENGTH_LONG);
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options_menu, menu);

        return true;
    }

}

here the is the serachable.xml file

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

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:hint="@string/search_hint" />

here there is the options_menu.xml file

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/search"
        android:title="@string/search_title"
        android:icon="@drawable/ic_action_search"
        android:showAsAction="collapseActionView|ifRoom"
        android:actionViewClass="android.widget.SearchView" />
</menu>

and this is the manifest

<activity android:name=".SearchActivity"
              android:label="SearchActivity">

        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data android:name="android.app.searchable"
            android:resource="@xml/searchable"/>


    </activity>

the only strange thing is that this line (android:showAsAction="collapseActionView|ifRoom") in the options_menu.xml file is highlighted in red.


Solution

  • Try putting this in your options_menu.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <item android:id="@+id/search"
            android:title="@string/search_title"
            android:icon="@drawable/ic_action_search"
            app:actionViewClass="android.widget.SearchView"
            app:showAsAction="collapseActionView|ifRoom" />
    </menu>
    

    And this in your AndroidManifest.xml

    <activity android:name=".SearchActivity"
        android:label="SearchActivity">
    
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
    
        <meta-data android:name="android.app.searchable"
            android:resource="@xml/searchable"/>
    
        <meta-data android:name="android.app.default_searchable"
            android:value=".SearchActivity" />
    </activity>
    

    And this in your SearchActivity.java

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options_menu, menu);
    
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false);
    
        return true;
    }
    
    @Override
    protected void onNewIntent(Intent intent) {
        setIntent(intent);
    
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            Log.e("query", intent.getStringExtra(SearchManager.QUERY));
        }
    }