I want to show some default suggestions in SearchView when User hasn't typed anything. I am setting my custom suggestion adapter manually using matrix cursor. I tried setting adapter in onFocusChange and onClickListner but suggestions get hidden behind a keyboard, apparently suggestions are loading earlier than keyboard. I did not find any listener which tells me that searchview is completely loaded with keyboard in the view. Help me in finding the right solution or any workaround. I have tried a workaround postDelayed but it's not smooth experience.
I have this code inside onCreateOptionsMenu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.clear();
final MenuInflater inflater = this.getMenuInflater();
inflater.inflate(R.menu.searchview_in_menu, menu);
this.menu=menu;
basketProductCount = ShoppinglistDataSource.get(getContext()).getProductCountToOrder();
if(basketProductCount>0 && menu!=null){
this.activateBasket();
}
SearchManager manager = (SearchManager) getSystemService(SEARCH_SERVICE);
MenuItem searchMenuItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) searchMenuItem.getActionView();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
int searchButtonId = mSearchView.getContext().getResources().getIdentifier("android:id/search_button", null, null);
ImageView searchButton = (ImageView) mSearchView.findViewById(searchButtonId);
if(searchButton!=null)
searchButton.setImageResource(R.drawable.search);
mSearchView.setSearchableInfo(manager.getSearchableInfo(new ComponentName(getApplicationContext(), SearchProductActivity.class)));
mSearchView.setIconifiedByDefault(true);
mSearchView.setIconified(true);
mSearchView.setQueryHint("Search from 4000+ products");
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
Log.d("SEARCH_VIEW_DEBUG","Inside onQueryTextSubmit for query: "+s);
mSearchView.clearFocus();
mSearchView.setIconified(true);
//return false to let the SearchView perform the default action
return false;
}
@Override
public boolean onQueryTextChange(String query) {
query = query.toLowerCase().trim();
Log.d("Query changed to ",query);
if (query.length() > 1) {
Bundle b = new Bundle();
b.putString("query",query);
if(getLoaderManager().getLoader(1)!=null)
getLoaderManager().restartLoader(1, b, suggestionLoaderCallback);
else
getLoaderManager().initLoader(1, b, suggestionLoaderCallback);
}
return true;
}
});
mSearchView.setOnQueryTextFocusChangeListener(new SearchView.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean queryTextFocused) {
Log.d("DEBUGGIN", "onFocusChange");
if(!queryTextFocused) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
mSearchView.onActionViewCollapsed();
mSearchView.clearFocus();
}
} else {
/**This is doing the trick, but not smooth experience**/
/**also suggestions may get behind the keyboard if keyboard takes time to pop up**/
view.postDelayed(new Runnable() {
@Override
public void run() {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
mSearchView.onActionViewExpanded();
Log.d("DEBUGGIN", "Inside expand");
if (mSearchView.getQuery().length() == 0) {
Log.d("DEBUGGIN", "getting default suggestions");
sug.clear();
sug.add("Personal Care");
sug.add("Oil");
sug.add("Spices");
sug.add("Breakfast");
sug.add("Snacks");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
String[] columns = new String[]{"_id", "text"};
Object[] temp = new Object[]{0, "default"};
final MatrixCursor cursor = new MatrixCursor(columns);
int i = 0;
for (i = 0; i < sug.size(); i++) {
if (!sug.get(i).equals("")) {
temp[0] = i;
temp[1] = sug.get(i);
cursor.addRow(temp);
}
}
Log.d("Initial categories", sug.toString());
mSearchView.setSuggestionsAdapter(new CustomSuggestionAdapter(ListAll.this, cursor, sug));
mSearchView.getSuggestionsAdapter().notifyDataSetChanged();
mSearchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
@Override
public boolean onSuggestionSelect(int i) {
Log.d("SEARCH_VIEW_DEBUG", "Inside onSuggestionSelect");
return false;
}
@Override
public boolean onSuggestionClick(int i) {
Log.d("SEARCH_VIEW_DEBUG", "Inside onSuggestionClick");
MatrixCursor cursor = (MatrixCursor) mSearchView.getSuggestionsAdapter().getItem(i);
String suggest1 = cursor.getString(cursor
.getColumnIndex("text"));
Log.d("Clicked Item", suggest1);
mSearchView.setQuery(suggest1, true);
return true;
}
});
}
}
}
}
}, 600);
}
}
});
}
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
searchMenuItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
/**This never gets called**/
Log.d("DEBUGGIN", "Inside onMenuItemActionExpand");
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
/**This never gets called**/
Log.d("DEBUGGIN", "Inside onMenuItemActionCollapse");
return true;
}
});
} else {
MenuItemCompat.setOnActionExpandListener(searchMenuItem, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
/**This never gets called**/
Log.d("DEBUGGIN", "Inside MenuItemCompat onMenuItemActionExpand");
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
/**This never gets called**/
Log.d("DEBUGGIN", "Inside MenuItemCompat onMenuItemActionCollapse");
return true;
}
});
}
//return true onCreateOptionsMenu
return true;
}
You can set an expand/collapse listener on the MenuItem
like this:
menuItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return true;
}
});
This listener was introduced with API level 14, so for backwards compatibility you have to use the v4 support library. If you do then you have to set the expand/collapse listener like this:
MenuItemCompat.setOnActionExpandListener(this.searchItem, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return true;
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With