I want to use SearchView in my project, but I have a problem with elements\' color. Let me show you: its fragmentDialog where I have my SearchView
I dont need to change
Searchview
which comes with support library has features to change icons
<androidx.appcompat.widget.SearchView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:searchIcon="@drawable/ic_search"
app:closeIcon="@drawable/ic_close_black"
app:commitIcon=""
app:goIcon=""
app:searchHintIcon=""
app:voiceIcon=""
>
This will help you if you want to change color of magnifying glass in Search view. if you use Material theme than this
ImageView icon = album_search.findViewById(com.google.android.material.R.id.search_button);
Drawable whiteIcon = icon.getDrawable();
whiteIcon.setTint(Color.BLACK); //Whatever color you want it to be
icon.setImageDrawable(whiteIcon);
and if Appcompact theme than just change com.google.android.material.R.id.search_button
to android.support.v7.appcompat.R.id.search_button
Thats it
In this answer I've changed colors of search icon, search hint icon, close icon, plate, query hint color, query color
view!!.findViewById<EditText>(
searchView.context.resources.getIdentifier(
"android:id/search_src_text",
null,
null
)
).apply {
setTextColor(ContextCompat.getColor(context!!, R.color.darkThemeIconColor))
setHintTextColor(ContextCompat.getColor(context!!, R.color.colorAccent))
}
view!!.findViewById<ImageView>(
searchView.context.resources.getIdentifier(
"android:id/search_button",
null, null
)
).imageTintList = ColorStateList.valueOf(
ContextCompat.getColor(context!!, R.color.darkThemeIconColor)
)
val mDrawable = SearchView::class.java.getDeclaredField("mSearchHintIcon")
mDrawable.isAccessible = true
val drawable = mDrawable.get(searchView) as Drawable
drawable.colorFilter = BlendModeColorFilterCompat.createBlendModeColorFilterCompat(
ContextCompat.getColor(context!!, R.color.darkThemeIconColor),
BlendModeCompat.SRC_ATOP
)
view!!.findViewById<ImageView>(
searchView.context.resources.getIdentifier(
"android:id/search_close_btn",
null, null
)
).imageTintList = ColorStateList.valueOf(
ContextCompat.getColor(context!!, R.color.darkThemeIconColor)
)
view!!.findViewById<View>(
searchView.context.resources.getIdentifier(
"android:id/search_plate",
null, null
)
).backgroundTintList = ColorStateList.valueOf(
ContextCompat.getColor(context!!, R.color.darkThemeIconColor)
)