I have a requirement to create a Google play and maps like search bar with filter in my project, but I have no idea how to start with. I tried it by own custom implementation like as follows:
Autocomplete TextView inside ToolBar android
MainActivity.java
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar (toolbar);
ArrayList<String> searchArrayList = new ArrayList<String>();
searchArrayList.add("United States");
searchArrayList.add("France");
searchArrayList.add("United Kingdom");
searchArrayList.add("India");
searchArrayList.add("Indonesia");
searchArrayList.add("Germany");
searchArrayList.add("Spain");
AutoCompleteAdapter adapter = new AutoCompleteAdapter(this,
R.layout.list_item, R.id.textView1, searchArrayList);
AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.searchAutoComplete);
autoCompleteTextView.setDropDownWidth(getResources().getDisplayMetrics().widthPixels);
autoCompleteTextView.setAdapter(adapter);
Custom AutoCompleteAdapter class implementation using ArrayAdapter
AutoCompleteAdapter.java
public class AutoCompleteAdapter extends ArrayAdapter<String> implements
Filterable {
private ArrayList<String> fullList;
private ArrayList<String> mOriginalValues;
private ArrayFilter mFilter;
public AutoCompleteAdapter(Context context, int resource,
int textViewResourceId, ArrayList<String> fullList) {
super(context, resource, textViewResourceId, fullList);
this.fullList = fullList;
mOriginalValues = new ArrayList<String>(fullList);
}
@Override
public int getCount() {
return fullList.size();
}
@Override
public String getItem(int position) {
return fullList.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return super.getView(position, convertView, parent);
}
@Override
public Filter getFilter() {
if (mFilter == null) {
mFilter = new ArrayFilter();
}
return mFilter;
}
private class ArrayFilter extends Filter {
private Object lock;
@Override
protected FilterResults performFiltering(CharSequence prefix) {
FilterResults results = new FilterResults();
if (mOriginalValues == null) {
synchronized (lock) {
mOriginalValues = new ArrayList<String>(fullList);
}
}
if (prefix == null || prefix.length() == 0) {
synchronized (lock) {
ArrayList<String> list = new ArrayList<String>(
mOriginalValues);
results.values = list;
results.count = list.size();
}
} else {
final String prefixString = prefix.toString().toLowerCase();
ArrayList<String> values = mOriginalValues;
int count = values.size();
ArrayList<String> newValues = new ArrayList<String>(count);
for (int i = 0; i < count; i++) {
String item = values.get(i);
if (item.toLowerCase().contains(prefixString)) {
newValues.add(item);
}
}
results.values = newValues;
results.count = newValues.size();
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
if (results.values != null) {
fullList = (ArrayList<String>) results.values;
} else {
fullList = new ArrayList<String>();
}
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}
I know that above implementation is not the right way to do it. But what I need to know whether is there any other way or library to accomplish it. I also see that in recent days many of the applications are using similar search bar, kindly please help me with the solutions since no where i can see the exact solutions for these requirement.
In most of the blogs and websites there is only solutions like.
Sorry to say this too that most of the StackOverflow posts are also left un answered for the similar questions as follows:
Implement search bar like in play store app
How to create search bar look like google play
All your solutions and suggestions are welcome. Kindly please forgive me if my post is not clear and seems unrelevant. Expecting your answers. Thanks in advance