How to change searchview icon color?
If you want to change SearchView's search icon, you just need to get the image view within the view as follow:
searchView = v.findViewById(R.id.searchView);
//change icon color
ImageView searchIcon = searchView.findViewById(android.support.v7.appcompat.R.id.search_button);
searchIcon.setImageDrawable(ContextCompat.getDrawable(getActivity(),R.drawable.ic_search_icon));
It is important that the icon is R.id.search_button and you can replace it with a white vector asset that you provide, in this case R.drawable.ic_search_icon
Similarly, you can change the text within the SearchView as follows:
//set color to searchview
SearchView.SearchAutoComplete searchAutoComplete = searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchAutoComplete.setHintTextColor(getResources().getColorandroid.R.color.white));
searchAutoComplete.setTextColor(getResources().getColor(android.R.color.white));
final SearchView searchViewAndroidActionBar = (SearchView) MenuItemCompat.getActionView(searchViewItem);
// change close icon color
ImageView iconClose = (ImageView) searchViewAndroidActionBar.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
iconClose.setColorFilter(getResources().getColor(R.color.grey));
//change search icon color
ImageView iconSearch = searchViewAndroidActionBar.findViewById(android.support.v7.appcompat.R.id.search_button);
iconSearch.setColorFilter(getResources().getColor(R.color.grey));
For android.x
,
@Override
public boolean onCreateOptionsMenu(Menu menu) {
...
ImageView searchIcon=
searchView.findViewById(androidx.appcompat.R.id.search_mag_icon);
// To change color of close button, use:
// ImageView searchCloseIcon = (ImageView)searchView
// .findViewById(androidx.appcompat.R.id.search_close_btn);
searchIcon.setColorFilter(getResources().getColor(R.color.colorPrimary),
android.graphics.PorterDuff.Mode.SRC_IN);
...
return true;
}
To change the search view's icon color use the following lines of code:
SearchView searchView = (SearchView) findViewById(R.id.searchview);
ImageView icon = searchView.findViewById(android.support.v7.appcompat.R.id.search_button);
icon.setColorFilter(Color.BLACK);