Search in a Listview

The code for searching the items in a Listview is:

listview1.setTextFilterEnabled (true);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
SearchView mSearchView = new SearchView(getSupportActionBar().getThemedContext());
mSearchView.setQueryHint("Search Here");
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
@Override
public boolean onQueryTextSubmit(String query)
{
return false;
}

@Override
public boolean onQueryTextChange(String newText){
if (TextUtils.isEmpty(newText)) {
listview1.clearTextFilter();
} else {
listview1.setFilterText(newText);
}
return true;
}
});
menu.add("Search")
.setIcon(R.drawable.search)
.setActionView(mSearchView)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
return true;

Listview1 is the listview id you can change that as per your choice.

The video tutorial for this is: 

Comments

Popular posts from this blog

How to Create Material Ripple Effect in Sketchware

How to Create Custom Notifications with Listeners

How to create Custom Dialog in Sketchware