Search code examples
androidandroid-studiostylingtheming

How to apply colorControl* attributes only to the Toolbar via styles/theme without affecting remaing item styles


I'm trying to change the color of buttons and their ripples in my Toolbars without affecting the rest of the app, but no matter how many different ways I try, something in the styles "breaks".

So far I've had the best results (as in that breaks other styles the least) by setting colorControlNormal and colorControlHighlight in the root theme of my app, however any other icon that has ?colorControlNormal set for their tint will also inherit this change as well as any ?selectableItemBackgroundBorderless and the color of scrollbars.

Previous attempts that resulted in worse results (or no results at all) count with defining the toolbar style via app theme:

<item name="toolbarStyle">@style/ToolbarStyle</item>

and in the toolbar style defining the colorControl* properties

<style name="ToolbarStyle" parent="Widget.AppCompat.Toolbar">
    <item name="colorControlNormal">@color/blue</item>
    <item name="colorControlHighlight">@color/blue</item>
</style>

having absolutely no result whatsoever

The other attempt was made via the theme attribute directly in the Toolbar view:

<androidx.appcompat.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ToolbarStyle"/>

Having the result that I wanted, but also changes a lot more than what I desired; the title + subtitle heights become the same height as the toolbar, since android:minHeight is no longer being overriden, but if I override that property in the styles then the search view will lose it's vertical alignment, the back button loses its height, etc. I end up at a point where I can't fix anything else via theme/style alone.

So now I am stuck with no clues in the documentation on how I can just change the icon colors and their respective ripple colors in the Toolbar only without affecting anything else, does anyone know how to do this without having to resort to doing it programmatically?


Solution

  • The styles' inheritance does not allow this to happen. The only way to do this is programmatically.