activity class:
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class Search_DemoMainActivity extends Activity {
private ListView lv1;
private String lv_arr[] =
{
"Android", "iPhone",
"BlackBerry", "me",
"J2ME", "Listview",
"ArrayAdapter", "ListItem",
"Us", "UK", "India"
};
ListView lst;
EditText edt;
ArrayAdapter<String> arrad;
Button btn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv1=(ListView)findViewById(R.id.listView1);
edt = (EditText) findViewById(R.id.editText1);
btn=(Button)findViewById(R.id.button1);
arrad = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr);
lv1.setAdapter(arrad);
// By using setTextFilterEnabled method in listview we can filter the listview items.
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
lv1.setTextFilterEnabled(true);
lv1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
AlertDialog.Builder builder = new AlertDialog.Builder(Search_DemoMainActivity.this);
builder.setTitle("Pick a color");
builder.setSingleChoiceItems(lv_arr, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), lv_arr[item], Toast.LENGTH_SHORT).show();
}});
AlertDialog alert = builder.create();
alert.show();
}
});
edt.addTextChangedListener(new TextWatcher()
{
public void onTextChanged( CharSequence arg0, int arg1, int arg2, int arg3)
{
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
{
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged( Editable arg0)
{
Search_DemoMainActivity.this.arrad.getFilter().filter(arg0);
}
});
}
}
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class Search_DemoMainActivity extends Activity {
private ListView lv1;
private String lv_arr[] =
{
"Android", "iPhone",
"BlackBerry", "me",
"J2ME", "Listview",
"ArrayAdapter", "ListItem",
"Us", "UK", "India"
};
ListView lst;
EditText edt;
ArrayAdapter<String> arrad;
Button btn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv1=(ListView)findViewById(R.id.listView1);
edt = (EditText) findViewById(R.id.editText1);
btn=(Button)findViewById(R.id.button1);
arrad = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr);
lv1.setAdapter(arrad);
// By using setTextFilterEnabled method in listview we can filter the listview items.
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
lv1.setTextFilterEnabled(true);
lv1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
AlertDialog.Builder builder = new AlertDialog.Builder(Search_DemoMainActivity.this);
builder.setTitle("Pick a color");
builder.setSingleChoiceItems(lv_arr, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), lv_arr[item], Toast.LENGTH_SHORT).show();
}});
AlertDialog alert = builder.create();
alert.show();
}
});
edt.addTextChangedListener(new TextWatcher()
{
public void onTextChanged( CharSequence arg0, int arg1, int arg2, int arg3)
{
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
{
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged( Editable arg0)
{
Search_DemoMainActivity.this.arrad.getFilter().filter(arg0);
}
});
}
}
==============================================================
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" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>