How to implement searchView on action bar and filter list in android?.
Create menu_search.xml file in project.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_search"
android:title="Search"
android:icon="@drawable/ic_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"
/>
</menu>
Create this code where we are getting the data in listView.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_search, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
(Adapter name).getFilter().filter(newText);
return true;
}
});
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.action_search:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Create the Adapter and implement the Filterable in adpater class.
public class ApproveAdapter extends BaseAdapter implements Filterable{
private ArrayList<ApproveItem> approveItems = new ArrayList<ApproveItem>();
private ArrayList<ApproveItem> orignalItems = new ArrayList<>();
private Activity activity;
Context con;
private final Context context;
private ViewHolder viewHolder;
public ApproveAdapter(Context context, ArrayList<ApproveItem> approveItems) {
// TODO Auto-generated constructor stub
this.context = context;
this.approveItems = approveItems;
this.orignalItems =approveItems;
}
/**
* implement base BaseAdapter method here.....
*/
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
approveItems = (ArrayList<ApproveItem>) results.values; // has the filtered values
notifyDataSetChanged(); // notifies the data with new filtered values
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults(); // Holds the results of a filtering operation in values
ArrayList<ApproveItem> FilteredArrList = new ArrayList<ApproveItem>();
if (orignalItems == null) {
orignalItems= new ArrayList<ApproveItem>(approveItems); // saves the original data in mOriginalValues
}
/********
*
* If constraint(CharSequence that is received) is null returns the mOriginalValues(Original) values
* else does the Filtering and returns FilteredArrList(Filtered)
*
********/
if (constraint == null || constraint.length() == 0) {
// set the Original result to return
results.count = orignalItems.size();
results.values = orignalItems;
} else {
constraint = constraint.toString().toLowerCase();
for (int i = 0; i < orignalItems.size(); i++) {
String data = orignalItems.get(i).leaveType;
if (data.toLowerCase().startsWith(constraint.toString())) {
FilteredArrList.add(orignalItems.get(i));
}
}
// set the Filtered result to return
results.count = FilteredArrList.size();
results.values = FilteredArrList;
}
return results;
}
};
return filter;
}
May this code help you. Thanks!!!!!!
Create menu_search.xml file in project.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_search"
android:title="Search"
android:icon="@drawable/ic_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"
/>
</menu>
Create this code where we are getting the data in listView.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_search, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
(Adapter name).getFilter().filter(newText);
return true;
}
});
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.action_search:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Create the Adapter and implement the Filterable in adpater class.
public class ApproveAdapter extends BaseAdapter implements Filterable{
private ArrayList<ApproveItem> approveItems = new ArrayList<ApproveItem>();
private ArrayList<ApproveItem> orignalItems = new ArrayList<>();
private Activity activity;
Context con;
private final Context context;
private ViewHolder viewHolder;
public ApproveAdapter(Context context, ArrayList<ApproveItem> approveItems) {
// TODO Auto-generated constructor stub
this.context = context;
this.approveItems = approveItems;
this.orignalItems =approveItems;
}
/**
* implement base BaseAdapter method here.....
*/
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
approveItems = (ArrayList<ApproveItem>) results.values; // has the filtered values
notifyDataSetChanged(); // notifies the data with new filtered values
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults(); // Holds the results of a filtering operation in values
ArrayList<ApproveItem> FilteredArrList = new ArrayList<ApproveItem>();
if (orignalItems == null) {
orignalItems= new ArrayList<ApproveItem>(approveItems); // saves the original data in mOriginalValues
}
/********
*
* If constraint(CharSequence that is received) is null returns the mOriginalValues(Original) values
* else does the Filtering and returns FilteredArrList(Filtered)
*
********/
if (constraint == null || constraint.length() == 0) {
// set the Original result to return
results.count = orignalItems.size();
results.values = orignalItems;
} else {
constraint = constraint.toString().toLowerCase();
for (int i = 0; i < orignalItems.size(); i++) {
String data = orignalItems.get(i).leaveType;
if (data.toLowerCase().startsWith(constraint.toString())) {
FilteredArrList.add(orignalItems.get(i));
}
}
// set the Filtered result to return
results.count = FilteredArrList.size();
results.values = FilteredArrList;
}
return results;
}
};
return filter;
}
Output:-
Screen |
May this code help you. Thanks!!!!!!