I am using a SearchView in the Action Bar. I want to use autocomplete feature on the search view to get results from the database.
Is this possible? Or do I need to
I was also facing this issue for the auto complete search view and fix it without using any extra layout and auto complete text view, there is a class called SearchAutoComplete. I used this to achieve the auto complete feature, just put a simple list adapter which contain a ArrayList of items to suggest with search view. Set the adapter to your SearchAutoComplete and auto complete feature will work below is my code. Remember you don't have to add any custom layout. Just replace your onCreateOptionsMenu(...) method with mine code:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
MenuItem searchItem = menu.findItem(R.id.search);
SearchView mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
SearchAutoComplete searchAutoComplete = (SearchAutoComplete) mSearchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_dropdown_item_1line, itemArrayList);
searchAutoComplete.setAdapter(adapter);
SearchManager searchManager =
(SearchManager) getSystemService(this.SEARCH_SERVICE);
mSearchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
Clicking the suggested item the item should appear on the search view so add the below code just after you set your adapter to searchAutoComplete inside the onCreateOptionmenu(...) method
searchAutoComplete.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
String searchString=(String)parent.getItemAtPosition(position);
searchAutoComplete.setText(""+searchString);
Toast.makeText(MainActivity.this, "you clicked "+searchString, Toast.LENGTH_LONG).show();
}
});