How to change SearchView elements' color?

后端 未结 9 1751
北海茫月
北海茫月 2020-12-31 02:47

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

相关标签:
9条回答
  • 2020-12-31 03:33

    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=""
        >
    
    0 讨论(0)
  • 2020-12-31 03:35

    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

    0 讨论(0)
  • 2020-12-31 03:42

    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)
            )
    
    0 讨论(0)
提交回复
热议问题