Monday, 24 August 2015

Example for using SearchView in ActionBar

Beginning in Android 3.0, using the SearchView widget as an item in the action bar is the preferred way to provide search in your app. Like with all items in the action bar, you can define the SearchView to show at all times, only when there is room, or as a collapsible action, which displays the SearchView as an icon initially, then takes up the entire action bar as a search field when the user clicks the icon.



Create your menu.xml as following.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".UploadActivity">
    <item
        android:id="@+id/search"
        android:icon="@android:drawable/ic_menu_search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        android:title="@string/action_search"
        app:showAsAction="collapseActionView|ifRoom" />
</menu>
Define your searchable.xml as following. Which defines how our searchview behaves.
<?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/action_search" />
In this example Search activity and search result activity both are same. So I defined the activity as follows in my manifest file.
<activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.SEARCH" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable"
                android:value=".UploadActivity"/>
        </activity>
If you want to make it with different activity just re declare the following line result activity.

And our onCreateOptionsMenu in our activity will be
@Override
public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_search, menu);
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        final SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
        // Assumes current activity is the searchable activity
        ComponentName componentName = new ComponentName(getApplicationContext(), MainActivity.class);//getComponentName();
        SearchableInfo info = searchManager.getSearchableInfo(componentName);
        searchView.setSearchableInfo(info);

        searchView.setOnSearchClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.v("App", "setOnSearchClickListener");
                if (searchView.getQuery().length() == 0)
                    searchView.setQuery("", true);
            }
        });
        return true;
}
Add the following line in our onCreate()
handleIntent(getIntent());
And override onNewIndent() in our activity
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.v("App", "onNewIntent");
        handleIntent(intent);
    }
And do our work in handleIntent() as follows
    private void handleIntent(Intent intent) {

        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            //use the query to search your data somehow
            Log.v("App", query);
        }
    }


I hope this post is useful to you. kindly share your feedback as comment here.



Source code on GitHub



Thank You