I have an edittext and a listview in my application my listview show contact list. I want listview filter with edittext. I searched a lot on google and found some examles bu
Android has an built in search bar feature .
Step 1 Add the following in your manifest :
<application
android:allowBackup="true"
android:icon="@drawable/dolphin1"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.RareMediaCompany.MuditaBulkScanner.DocketListActivity"
android:label="@string/app_name"
android:launchMode="singleTop"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
<meta-data
android:name="android.app.default_searchable"
android:value="com.RareMediaCompany.MuditaBulkScanner.DocketListActivity" />
</application>
Step2
onSearchRequested(); will call your search bar .You can add this in on click of your search button.
Step3 Simply call this function in your activity's on create :
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
myList.clear();
String query = intent.getStringExtra(SearchManager.QUERY);
new getQueryResultAndPopulateListTask(this, query).execute();
}
}
This one worked for me:
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
int textlength = cs.length();
ArrayList<ContactStock> tempArrayList = new ArrayList<ContactStock>();
for(ContactStock c: arraylist){
if (textlength <= c.getName().length()) {
if (c.getName().toLowerCase().contains(cs.toString().toLowerCase())) {
tempArrayList.add(c);
}
}
}
mAdapter = new ContactListAdapter(activity, tempArrayList);
lv.setAdapter(mAdapter);
}