Advanced Android - Search Functionality



Adding search functionality to listview will filters the list data with a matching string, hence provides user an easy way to find the information

Example

This example demostrate about how to integrate Android Search Functionality.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
   android:layout_width = "fill_parent"
   android:layout_height = "fill_parent"
   android:orientation = "vertical" >
   <EditText android:id = "@+id/inputSearch"
      android:layout_width = "fill_parent"
      android:layout_height = "wrap_content"
      android:hint = "Search products.."
      android:inputType = "textVisiblePassword"/>
   <ListView
      android:id = "@+id/list_view"
      android:layout_width = "fill_parent"
      android:layout_height = "wrap_content" />
</LinearLayout>

Step 3 − Add the following code to src/MainActivity.java

package myapplication.example.com.myapplication;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity {
   // List view
   private ListView lv;
   
   ArrayAdapter<String> adapter;
   EditText inputSearch;
   ArrayList<HashMap<String, String>> productList;
   
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      String products[] = {
         "Dell Inspiron", 
         "HTC One X", 
         "HTC Wildfire S", 
         "HTC Sense", 
         "HTC Sensation XE",
         "iPhone 5S", 
         "iPhone 6S", 
         "iPhone 7S", 
         "Samsung Galaxy Note 5",
         "Samsung Galaxy S7", 
         "MacBook Air", 
         "Mac Mini", 
         "MacBook Pro"
      };
      
      lv = (ListView) findViewById(R.id.list_view);
      inputSearch = (EditText) findViewById(R.id.inputSearch);
      adapter = new ArrayAdapter<String>(
         this, R.layout.list_item, R.id.product_name, products);
      
      lv.setAdapter(adapter);
      inputSearch.addTextChangedListener(new TextWatcher() {
         @Override
         public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            MainActivity.this.adapter.getFilter().filter(cs);
         }
         
         @Override
         public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
         } 
         
         @Override
         public void afterTextChanged(Editable arg0) {
         } 
      });
   }
}

Step 4 − Add the following code to res/layout/list_item.xml

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   android:orientation = "vertical" >
   <TextView android:id = "@+id/product_name"
      android:layout_width = "fill_parent"
      android:layout_height = "wrap_content"
      android:padding = "10dip"
      android:textSize = "16dip"
      android:textStyle = "bold"/>
</LinearLayout>

Step 5 − Add the following code to manifest.xml

<?xml version = "1.0" encoding = "utf-8"?>
<manifest xmlns:android = "http://schemas.android.com/apk/res/android"
   package = "myapplication.example.com.myapplication">
   <application
      android:allowBackup = "true"
      android:icon = "@mipmap/ic_launcher"
      android:label = "@string/app_name"
      android:supportsRtl = "true"
      android:theme = "@style/AppTheme">
      <activity android:name = ".MainActivity"
         android:windowSoftInputMode = "stateHidden">
         <intent-filter>
            <action android:name = "android.intent.action.MAIN" />
            <category android:name = "android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>

</manifest>

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run Eclipse Run Icon icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

Search
Advertisements