Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SearchView Activity
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search, menu);
final MenuItem item = menu.findItem(R.id.search);
if (item != null) {
final View actionView = item.getActionView();
if (actionView != null) {
SearchView searchView = (SearchView) actionView;
// changes the search icon
int searchImgId = getResources().getIdentifier("android:id/search_button", null, null);
final View v = searchView.findViewById(searchImgId);
if (v != null) {
ImageView vi = (ImageView) v;
vi.setImageResource(R.drawable.ic_action_action_search);
}
// Changes the EditText style
int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_plate", null, null);
View searchPlate = searchView.findViewById(searchPlateId);
if (searchPlate != null) {
searchPlate.setBackgroundResource(R.drawable.edit_text);
}
SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
final SearchableInfo searchableInfo = manager.getSearchableInfo(getComponentName());
searchView.setSearchableInfo(searchableInfo);
searchView.setOnQueryTextListener((SearchView.OnQueryTextListener) fragment);
searchView.setOnCloseListener((SearchView.OnCloseListener) fragment);
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.