Monday, April 28, 2014

Android Action Bar search inside activity | Searching into list-view using action bar | Android Action Bar

Hello Friend,

Today I am going to share some of my experience related to Action bar in Android. Have you seen many apps like whats-app and some others top app there are a action-bar search functionality. So let us see how can we do it-





















1)If you want to get search result on same activity-

i-In your manifest,xml just add search action and searchable meta data-

 <activity
            android:name="com.androidhub4you.searchactionbardemo.SearchActivity"
            android:launchMode="singleTop" >
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>

            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />
        </activity>

ii- Create a searchable file inside res/xml-

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

iii- In your menu.xml file add below code-

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- Search Widget -->
    <item
        android:id="@+id/action_search"
        android:actionViewClass="android.widget.SearchView"
        android:icon="@drawable/ic_launcher"
        android:showAsAction="always"
        android:title="Search"/>

</menu>

iv- And finally inside your activity class-


public class SearchActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_results);
// get the action bar
ActionBar actionBar = getActionBar();

// Enabling Back navigation on Action Bar icon
actionBar.setDisplayHomeAsUpEnabled(true);

//txtQuery = (TextView) findViewById(R.id.txtQuery);

handleIntent(getIntent());
}

@Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}

*//**
* Handling intent data
*/
 
  
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);

txtQuery.setText("Search Query: " + query);

}
}

2)If you want searching into listview-

package com.androidhub4you.searchactionbardemo;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;

public class SearchActivity extends Activity {

       ArrayAdapter<String> myAdapter;
    ListView listView;
    String[] dataArray = new String[] {"India","Androidhub4you", "Pakistan", "Srilanka", "Nepal", "Japan"};

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_search_results);
             
             
              //==========================
             
              listView = (ListView) findViewById(R.id.listview);
           myAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dataArray);
           listView.setAdapter(myAdapter);
           listView.setTextFilterEnabled(true);
          
        listView.setOnItemClickListener(new OnItemClickListener() {

              @Override
              public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                           long arg3) {

              System.out.println(arg2+" --postion");
              }
       });

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

               SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
               SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();

                   searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
                   searchView.setIconifiedByDefault(false);  

               SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener()
               {
                   @Override
                   public boolean onQueryTextChange(String newText)
                   {
                       // this is your adapter that will be filtered
                       myAdapter.getFilter().filter(newText);
                       System.out.println("on text chnge text: "+newText);
                       return true;
                   }
                   @Override
                   public boolean onQueryTextSubmit(String query)
                   {
                       // this is your adapter that will be filtered
                       myAdapter.getFilter().filter(query);
                       System.out.println("on query submit: "+query);
                       return true;
                   }
               };
               searchView.setOnQueryTextListener(textChangeListener);

               return super.onCreateOptionsMenu(menu);

       }
}

Thank you!