Search code examples
androidandroid-stylesandroid-toolbarandroid-search

Changing AppCompat's SearchView colorAccent


I know this topic has been asked previously but most of the answers provide a programatic solution which is not very up to date.

I have a SearchView inside Toolbar, I need to modify it's appearance a bit, here's my style file

<resources>
    <style name="Theme.XYZ" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">#29abbd</item>
        <item name="colorPrimaryDark">#00838f</item>
        <item name="colorAccent">#29abbd</item>
        <item name="colorButtonNormal">#29abbd</item>
        <item name="android:windowBackground">@color/window_background</item>
        <item name="searchViewStyle">@style/Theme.XYZ.SearchView</item>
    </style>


    <style name="Theme.XYZ.SearchView" parent="Widget.AppCompat.Light.SearchView">
        <item name="closeIcon">@drawable/ic_launcher</item>
        <item name="colorAccent">#ffffff</item>
    </style>

</resources>

This does not work, the SearchView is not assigned with the custom close icon and the colorAccent is the color of the main theme.

Your help is appreciated.


Solution

  • I'm posting here my solution, although it defines a style to AutoCompleteTextView instead of SearchView directly

    <style name="Theme.XYZ" parent="Theme.AppCompat.Light.NoActionBar">
            <item name="colorPrimary">#29abbd</item>
            <item name="colorPrimaryDark">#00838f</item>
            <item name="colorAccent">#29abbd</item>
            <item name="colorButtonNormal">#29abbd</item>
            <item name="android:windowBackground">@color/window_background</item>
            <item name="autoCompleteTextViewStyle">@style/XYZSearchView</item>
    
    
        </style>
    
    
        <style name="XYZSearchView" parent="Widget.AppCompat.Light.AutoCompleteTextView">
            <item name="android:textCursorDrawable">@drawable/white_cursor</item>
        </style>