Tuesday, May 13, 2014

Adding Search Widget to action bar

Another most useful feature of action bar is adding widgets to it. For example like adding search widget to action bar. Search widget will be useful when user wants to search for something in the app or across android OS.
Adding search widget involves these steps.

  Adding the Search Widget to action bar action item
  Defining the searchable configuration in the xml
  Creating the activity to handle search query and display the results
  Defining the default searchable activity and SEARCH intent filter in AndroidManifest.xml file

1. So first we add the search widget to action bar. Open your activity_main_actions.xml which is located under menu folder and add search widget to search action item as below.

<item android:id="@+id/action_search"

2. Create a searchable configuration file under res ⇒ xml folder named searchable.xml (If you don’t see xml folder under res, create a new folder with the name xml). See list of Searchable Configuration options.


<xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name" />

It is suggested that always assign the values of android:hint and android:label from strings.xml file only instead of writing the values directly.
Now open the main activity and modify the code in the onCreateOptionsMenu() method.


    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.activity_main_actions, menu);
        // Associate searchable configuration with the SearchView
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
        return super.onCreateOptionsMenu(menu);

3. We need another activity to handle search query and results. Create a new class named SearchResultsActivity.java and paste following code. Also create layout file too named activity_search_results.xml.
Here I just passed the search query to another activity. You have to take appropriate action to display the search results using the query either from SQLite database or making a request to server and getting the results or some other way.


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    tools:context=".MainActivity" >
        android:layout_height="wrap_content" />


package info.dreamitsanjay.actionbar;
import android.app.ActionBar;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class SearchResultsActivity extends Activity {
    private TextView txtQuery;
    protected void onCreate(Bundle savedInstanceState) {
        // get the action bar
        ActionBar actionBar = getActionBar();
        // Enabling Back navigation on Action Bar icon
        txtQuery = (TextView) findViewById(R.id.txtQuery);
    protected void onNewIntent(Intent intent) {
     * Handling intent data
    private void handleIntent(Intent intent) {
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
             * Use this query to display search results like
             * 1. Getting the data from SQLite and showing in listview
             * 2. Making webrequest and displaying the data
             * For now we just display the query only
            txtQuery.setText("Search Query: " + query);

4. Finally in the AndroidManifest.xml file define the searchable configuration, default searchable activity and the activity performing the search.
android.app.default_searchable – Defines the default searchable activity handle search. You can add this block anywhere in the manifest file either inside tag or tag.
android.app.searchable – Defines the searchable configuration which was written in searchable.xml file
android.intent.action.SEARCH – Should be defined as a intent filter for the activity which receives the search query.
xml version="1.0" encoding="utf-8"?>
    android:versionName="1.0" >
        android:targetSdkVersion="18" />
        android:theme="@style/AppTheme" >
                android:value=".SearchResultsActivity" />
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
        <activity android:name=".SearchResultsActivity"
            android:parentActivityName="info.dreamitsanjay.actionbar.MainActivity" >
                <action android:name="android.intent.action.SEARCH" />
                android:resource="@xml/searchable" />
            android:parentActivityName="info.dreamitsanjay.actionbar.MainActivity" >

Adding Search Widget to action bar

If you are searching life partner. your searching end with kpmarriage.com. now kpmarriage.com offer free matrimonial website which offer free message, free chat, free view contact information. so register here : kpmarriage.com- Free matrimonial website