Search code examples
androidandroid-toolbarandroid-themeandroid-styles

actionMenuTextColor not working when using ?/attr


I am trying to change the color of the text menu item in my toolbar

When using :

<style name="Base.App" parent="Theme.MaterialComponents.Light.NoActionBar">
        <item name="actionMenuTextColor">@color/blue</item>

It works but when using :

<style name="Base.App" parent="Theme.MaterialComponents.Light.NoActionBar">
            <item name="actionMenuTextColor">?attr/colorPrimary</item>

It doesn't.

Any idea?

Edit 1 :

toolbar styling is the following

<item name="toolbarStyle">@style/Widget.App.Toolbar</item>

<style name="Widget.App.Toolbar" parent="Widget.MaterialComponents.Toolbar.Primary">
        <item name="materialThemeOverlay">@style/ThemeOverlay.App.Toolbar</item>
        <item name="titleTextAppearance">@style/Widget.App.Toolbar.TitleTextAppearance</item>
    </style>

    <style name="Widget.App.Toolbar.TitleTextAppearance" parent="TextAppearance.App.Headline6">
        <item name="android:textStyle">bold</item>
    </style>

Edit 2 :

using

<style name="Base.App" parent="Theme.MaterialComponents.Light.NoActionBar">
            <item name="actionMenuTextColor">?attr/colorPrimaryVariant</item>

actually works. But again, not colorPrimary


Solution

  • <item name="actionMenuTextColor">?attr/colorPrimary</item>
    

    It doesn't use the colorPrimary because in the Toolbar you are overriding the colorPrimary with:

    <style name="Widget.App.Toolbar" parent="Widget.MaterialComponents.Toolbar.Primary">
        <item name="materialThemeOverlay">@style/ThemeOverlay.App.Toolbar</item>      
    </style>
    
    <style name="ThemeOverlay.App.Toolbar" parent=""> 
        <item name="colorPrimary">?attr/colorApp4</item>  <!-- HERE -->
    </style>
    

    It means that using ?attr/colorPrimary the actionMenuTextColor uses the colorApp4 in the Toolbar.

    If you want to change the default color just apply:

    <style name="ThemeOverlay.App.Toolbar" parent=""> 
        <item name="colorPrimary">?attr/colorApp4</item>  
        <item name="actionMenuTextColor">......</item>
    </style>