XML layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical">
<EditText android:id="@+id/EditText01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:hint="Search"></EditText>
<ListView android:id="@+id/ListView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" ></ListView>
</LinearLayout>
Java Source Code
package com.sai.sample;
import java.util.ArrayList;
import android.app.Activity;
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 Sort extends Activity {
private ListView listview;
private EditText edittext;
private String list[]={"Iphone",
"Cupcake","Andoird","Blackberry","Samsung","Nokia"
};
private ArrayList<String > arraylist= new ArrayList<String>();
private int textlenght=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
listview=(ListView)findViewById(R.id.ListView01);
edittext=(EditText)findViewById(R.id.EditText01);
listview.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , list));
edittext.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
textlenght=edittext.getText().length();
arraylist.clear();
for(int i=0;i<list.length;i++){
if(textlenght<=list[i].length())
{
if(edittext.getText().toString().equalsIgnoreCase((String) list[i].subSequence(0, textlenght)))
{
arraylist.add(list[i]);
}
}
}
listview.setAdapter(new ArrayAdapter<String>(Sort.this,android.R.layout.simple_list_item_1 , arraylist));
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
}
Run as Android Application
This comment has been removed by the author.
ReplyDelete