I know there\'s already a lot of answers for this question here, and I wouldn\'t be creating another thread if I hadn\'t already tried everything I saw here. Anyway, the qu
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));
Use below code:
ImageView searchIcon = searchView.findViewById(R.id.search_button);
searchIcon.setColorFilter(Color.WHITE);
If you want to change close icon color, use this one:
ImageView searchClose = searchView.findViewById(R.id.search_close_btn);
searchIcon.setColorFilter(Color.WHITE);
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);
You can change or customize the icons of the SearchView by adding you're own drawable icons.
<androidx.appcompat.widget.SearchView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
app:closeIcon="@drawable/close_icon"
app:searchHintIcon="@drawable/search_icon"
app:searchIcon="@drawable/search_icon"
android:textStyle="bold" />
Take the Drawable you want to tint, wrap it with a tinted drawable , and set it as a new one for the search menu item:
@JvmStatic
fun getTintedDrawable(inputDrawable: Drawable, @ColorInt color: Int): Drawable {
val wrapDrawable = DrawableCompat.wrap(inputDrawable.mutate())
DrawableCompat.setTint(wrapDrawable, color)
DrawableCompat.setTintMode(wrapDrawable, Mode.SRC_IN)
return wrapDrawable
}
Usage:
val drawable =getTintedDrawable(...)
searchMenuItem.icon = drawable
And this should work for all Android versions that android-x (support library) supports.