Tuesday, 23 October 2012

Search in Custom Listview in Android | Android Custom Search | Custom Listview


Hello Friends ,

Today , I am Sharing my code my code with the help of which we can easily implements the
search functionality on listview or on a custom list view.

android search
android search bar

android search
Search  icon inside edit text 




1. Create a new project in Eclipse File New ⇒ Android ⇒ Application Project and fill the required details.
2. Create required files needed to generate a list view. I am using my default activity_main.xml as list view and created a new xml file for single list item named listview.xml
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/EditText01"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"
         
        android:drawableLeft="@android:drawable/ic_menu_search"      

        android:hint="Search" >

    </EditText>

    <ListView

        android:id="@+id/ListView01"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content" >

    </ListView>

</LinearLayout>



listview.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="wrap_content"
    android:background="@android:color/background_dark"
    android:gravity="left|center"
    android:paddingBottom="5px"
    android:paddingLeft="5px"
    android:paddingTop="5px" >

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10px"
        android:textColor="#0099CC"
        android:textSize="20px"
        android:textStyle="bold" >
    </TextView>

</LinearLayout>


MainActivity.java


package com.mukesh.customsearch;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

 EditText edittext;
 ListView listview;

 String[] text = { "One", "HTC One X", "Samsung Galaxy S3", "Samsung 
         Galaxy Note 800", "HTC Sense", "HTC Sensation XE", "HTC Wildfire S",
   "HTC Wildfire", "Wildfire S", "HTC" };

 int textlength = 0;

 ArrayList<String> text_sort = new ArrayList<String>();
 ArrayList<Integer> image_sort = new ArrayList<Integer>();

 public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  edittext = (EditText) findViewById(R.id.EditText01);
  listview = (ListView) findViewById(R.id.ListView01);
  listview.setAdapter(new CustomAdapter(text));

  edittext.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) {

    textlength = edittext.getText().length();
    text_sort.clear();
    image_sort.clear();

    for (int i = 0; i < text.length; i++) {
     if (textlength <= text[i].length()) {
      if (edittext
        .getText()
        .toString()
        .equalsIgnoreCase                                                                    (
          (                                                                  String) text[i]                                     .subSequence(0,textlength))) {
       text_sort.add(text[i]);
      }
     }
    }

    listview.setAdapter(new CustomAdapter(text_sort));

   }
  });
 }

 class CustomAdapter extends BaseAdapter {

  String[] data_text;

  CustomAdapter() {

  }

  CustomAdapter(String[] text) {
   data_text = text;
  }

  CustomAdapter(ArrayList<String> text) {

   data_text = new String[text.size()];

   for (int i = 0; i < text.size(); i++) {
    data_text[i] = text.get(i);
   }

  }

  public int getCount() {
   return data_text.length;
  }

  public String getItem(int position) {
   return null;
  }

  public long getItemId(int position) {
   return position;
  }

  public View getView(int position, View convertView,
                                         ViewGroup parent) {

   LayoutInflater inflater = getLayoutInflater();
   View row;

   row = inflater.inflate(R.layout.listview, parent, false);

   TextView textview = (TextView) row.findViewById(R.id.Text                                               View01);

   textview.setText(data_text[position]);

   return (row);

  }
 }

}




Enjoy Coding :)


Blog Related to this :
1. Horizontal ListView


Mukesh Kumar

Hi Guys I am from Delhi working as Web/Mobile Application Developer(Android Developer), also have knowledge of Roboelctric and Mockito ,android test driven development... Blogging has been my passion and I think blogging is one of the powerful medium to share knowledge and ideas....

5 comments:

  1. Thank you for another great article. Where else could anyone get that kind of information in such a perfect way of writing? I have a presentation next week, and I am on the look for such information.
    Android application

    ReplyDelete
  2. what if search is not available in list

    ReplyDelete
  3. please help me, how to setOnItemclick in this code?

    ReplyDelete
  4. Thanks man this is really help full :-)

    ReplyDelete

 

Copyright @ 2013 Android Developers Blog.