How to add a search to List View

Initially design a xml file as show below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    <EditText android:id="@+id/EtBrandName" android:layout_height="wrap_content" 
    android:layout_width="fill_parent" android:hint="Search">
    <ListView android:id="@+id/lvItems"
android:layout_height="wrap_content" >

Then write the following code in your activity:-

import java.util.ArrayList;
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 LSearch extends Activity {
private ListView lv1;
private EditText ed;
private String lv_arr[]={"Android","Cupcake","Donut","Eclairs","AndroidPeople","Froyo","Apple","Samsung","Storeage","Sony"};
private ArrayList<String> arr_sort= new ArrayList<String>();
int textlength=0;
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        // By using setAdpater method in listview we an add string array in list.
        lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
        ed.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable s) {

        public void beforeTextChanged(CharSequence s, int start, int count,
        int after) {

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

        for(int i=0;i<lv_arr.length;i++)
        if(ed.getText().toString().equalsIgnoreCase((String) lv_arr[i].subSequence(0, textlength)))

        lv1.setAdapter(new ArrayAdapter<String>(LSearch.this,android.R.layout.simple_list_item_1 , arr_sort));


your output will look as shown below:

Search & Download lots of Eclipse-Android Tutorials:-
  Using Pdf Wallet you can find useful and free Android Tutorials.


Please Select File-Format

PDF  - eBooks in pdf format.
PPT  - Presentations:2003.
PPTX- Presentations:2007+.
DOC - Microsoft Word Files:2003.
DOCXMicrosoft Word Files:2007+.
XLS  - Excel File format:2003.
XLSX - Excel File Format:2007+.
CHM - Compiled Help Files.
RTF  - Rich Text format.
TXT  - Text Files. 

No comments:

Post a Comment