Android. Live search using ListView

September 2, 2016 26 Yehor Rykhnov

A simple example of a live search in several steps in Android using ListView.

Step 1. Open Android Studio, create a new project with the Empty Activity.

Step 2. Open activity_main.xml file (path: res/layout/activity_main.xml) and paste the following code:

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >

    <EditText
        android:id="@+id/txtsearch"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Search"
        />

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

</LinearLayout>

Step 3. Create a file list_item.xml (path: res/layout/list_item.xml) and paste in it following code for display element in ListView:

<?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/txtitem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
    />

</LinearLayout>

Step 4. Open MainActivity.java and paste the following code:

import android.support.v7.app.ActionBarActivity;
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 java.util.ArrayList;
import java.util.Arrays;

public class MainActivity extends ActionBarActivity {

    String[] items;
    ArrayList<String> listItems;
    ArrayAdapter<String> adapter;
    ListView listView;
    EditText editText;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView=(ListView)findViewById(R.id.listview);
        editText=(EditText)findViewById(R.id.txtsearch);
        initList();

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(s.toString().equals("")){
                    // reset listview
                    initList();
                } else {
                    // perform search
                    searchItem(s.toString());
                }
            }

            @Override
            public void afterTextChanged(Editable s) {        
            }

        });        
    }

    public void searchItem(String textToSearch){
        for(String item:items){
            if(!item.contains(textToSearch)){
                listItems.remove(item);
            }
        }
        adapter.notifyDataSetChanged();
    }

    public void initList(){
       items=new String[]{"Java","JavaScript","C#","PHP", "С++", "Python", "C", "SQL", "Ruby", "Objective-C"};
       listItems=new ArrayList<>(Arrays.asList(items));
       adapter=new ArrayAdapter<String>(this, R.layout.list_item, R.id.txtitem, listItems);
       listView.setAdapter(adapter);
  }

}

It remains only to start the application. As a result, we obtain the following: