Android Simple ListView with Search Functionality Example

Here you will get android simple listview with search functionality example.

Adding search functionality in listview helps users to find information in easy way. When user writes something in the textbox, the items in the list is filtered and an updated list of items is displayed.

Below example shows you how to implement this.

 

Android Simple ListView with Search Functionality Example

First of all create a new project with package name thecrazyprogrammer.androidexample. Now add following code in respective files.

 

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" tools:context=".MainActivity"
    android:orientation="vertical"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_marginTop="15dp"
    android:layout_marginBottom="15dp">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textBox"/>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list"/>

</LinearLayout>

 

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">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/text"
        android:textSize="20dp"/>
</LinearLayout>

 

MainActivity.java

package thecrazyprogrammer.androidexample;

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;
import android.widget.TextView;

public class MainActivity extends Activity {
    EditText textBox;
    TextView text;
    ListView list;
    String country[]={"India","USA","China","Australia","Japan","England","Nepal","Canada","Sri-Lanka","Russia"};
    ArrayAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textBox=(EditText)findViewById(R.id.textBox);
        text=(TextView)findViewById(R.id.text);
        list=(ListView)findViewById(R.id.list);

        adapter=new ArrayAdapter(this,R.layout.list_item,R.id.text,country);
        list.setAdapter(adapter);


        textBox.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                adapter.getFilter().filter(s);
            }

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

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
}

 

Output

Android Simple ListView with Search Functionality Example 1

Android Simple ListView with Search Functionality Example 2

 

The above code is self-explanatory, if still you are facing difficulty to understand then feel free to comment below.

 

3 thoughts on “Android Simple ListView with Search Functionality Example”

  1. Hey neeraj thanks alotttttttttttt…..
    U are doing a great job of sharing these small programs that help newcomers like me…
    Won’t be able to read your blog for next 3 months due to my exams
    But will be back sooon..
    Please do help me in personal wheni need 3 months later..please do send me an email on my email id so I can contact u after 3 months for any difficulties that I face.

  2. Great tutorial!! I would like to know one thing… For E.g..I have two separate lists in separate activities ..months and weekdays.. and implement a searchview as suggested here.. it searches for the months perfectly.. and the other activity searches for weeks perfectly.. what if i want to add a searchview wherein both the lists (months & weekdays) can be searched.. i.e if the user searches JANUARY or MONDAY the list should filter and show the results.. how to do this when the list are in separate activities… Your reply in this regard will be greatly appreciated….

  3. tnx with this good article.
    how can I search in whole array? for example search “in” ,and the result show
    { india, china} like this?

Leave a Comment

Your email address will not be published. Required fields are marked *