Wednesday, December 11, 2013

Search ListView in Android Example

No comments :
Hi,

  This is an Example for Search listview.Using listview we can display the contacts and also using the search purpose in Android


1. create a activity_main.xml in Android Project.

activity_main.xml:

<RelativeLayout 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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="search"
        android:textStyle="bold"/>

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignRight="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:ems="10" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1" >

    </ListView>

</RelativeLayout>


2.create a another xml for list_row.xml  In this xml create a textview for listview

list_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dip"
    android:textSize="16dip"
    android:textStyle="bold" >
</TextView>


3. create a java class called MainActivity.java. Using the addTextChangedListener only search the text using      method getFilter().filter(s);
  

MainActivty.java:

package com.example.list;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {
    
ListView lv;
EditText ed;
ArrayAdapter<String> adapter;
List<String> array;
String[] phone={"Samsung","Nokia","Motorola","iphone","Sony Xperia","Micromax","Karbonn","HTC","LG","Nexus"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView)findViewById(R.id.listView1);
ed=(EditText)findViewById(R.id.editText1);
   array=new ArrayList<String>(Arrays.asList(phone));
   adapter=new ArrayAdapter<String>(this,R.layout.list_row,array );
   lv.setAdapter(adapter);    
   ed.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
MainActivity.this.adapter.getFilter().filter(s);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
   
   lv.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) 
{
   Toast.makeText(getApplicationContext(),"your position is  " +arg2, 4).show();
   
}
 
});
    
}

}


4.Run the Application and Here the output is shown below.


        







     

No comments :

Post a Comment