Android ListView With Search Functionality(Data Filtering) Tutorial/Example

ListView With Search Functionality helps the user to easily get the results required.In search Functionality, we filter the data according to data entered by the user and thus showing the results user wants.So, Let’s Start to implement search functionality

  1. Open Eclipse or Android Studio -> New Project->Android Application Project-> Name of Application-> Follow all instructions and complete by clicking on Finish.
  2. We will need two layouts for this one for main screen containing view for search and second to show the results as required by the user.

//layout for main screen

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <EditText
        android:id="@+id/searchdata"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="enter to search"/>

    <ListView
        android:id="@+id/showdata"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"></ListView>

</LinearLayout>

//layout to show the results

searchresults.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/results"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textSize="20dp"/>

</LinearLayout>

3. Now defining MainActivity.java, containg list and search editext  and adapter to set to list.

MainActivity.java

package com.coderzpassion.mylistviewsearch;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    //declaring variables
    private ListView listviewforresults;
    //Adapter for listview
    ArrayAdapter<String> adapter;
    //Edittext for search
    EditText searchdata;

    //ArrayList for listview
    ArrayList<String>  data=new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //intializing
        listviewforresults=(ListView)findViewById(R.id.showdata);
        searchdata=(EditText)findViewById(R.id.searchdata);

        //prepare dummy data for searching
        prepareDummyData();

        //set data to Adapter
        adapter=new ArrayAdapter<String>(MainActivity.this,R.layout.searchresults,R.id.results,data);
        listviewforresults.setAdapter(adapter);
        //search data when text changes in edittext
        searchdata.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                MainActivity.this.adapter.getFilter().filter(s);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    public void prepareDummyData()
    {
        data.add("India");
        data.add("Usa");
        data.add("Canada");
        data.add("Iran");
        data.add("Iraq");
        data.add("Russia");
        data.add("Uk");
        data.add("Cameroon");

    }

    @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_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.coderzpassion.mylistviewsearch" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>