Reputation: 1089
I need to implement the search bar in navigation bar for android in xamarin.forms,while searching i found many examples for ios but not for android.How to achieve this for android through customrenderer?
Upvotes: 1
Views: 629
Reputation: 21
Easy way to create a Search is to create class and make use of Filter options like this
Search Bar Button on Top
<item android:id="@+id/action_search"
android:title="Search"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="always" />
Classes You May Require
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(this);
return true;
}
//getting text from user
@Override
public boolean onQueryTextChange(String newText) {
activity.getFilter().filter(newText);
return false;
}
//submitting Text
@Override
public boolean onQueryTextSubmit(String newText) {
activity.getFilter().filter(newText);
return false;
}
This is other Activity
public Filter getFilter() {
return filter;
}
private Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
List<Pokemon> to_be_filtered = new ArrayList<>();
String filterPattern = constraint.toString().toLowerCase().trim();
//implement search
if(filterPattern.isEmpty()) {
to_be_filtered.addAll(fulllist);
}
else {
for (some this iterate over ) {
if (get_name_of_required
search.getName().toLowerCase().contains(filterPattern)) {
add_created_database_to_be_filtered.add(name of list variable);
}
}
}
In this you have to use inbuilt function called getfilter() it will handle Search bar. Here I had used statements like to_be_filtered and add_created_database, You have to modify code according to database you are using.
I my case I am having 2 activities in which the first three class i.e. OnQuerySearch... , Submit text and Save text are in MainActivity and getFilter() is in another Activity
Thanks I hope you got your answer
Upvotes: 1