Search code examples
androidstylingappbar

How to style Android's AppBar action text?


I need to change text size of action buttons in AppBar/Toolbar. It should be 14sp, but I'll use 20sp in this example, because it is more evident. I am using appcompat-v7 22.1.1

At first I tried to use theme attribute android:actionButtonStyle:

<style name="FirstAttemptTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionButtonStyle">@style/Custom.Widget.AppCompat.Light.ActionButton</item>
</style>

<style name="Custom.Widget.AppCompat.Light.ActionButton" parent="Widget.AppCompat.Light.ActionButton">
    <item name="android:textSize">20sp</item>
</style>

Then I ran application on the Lollipop and the result was as needed:

enter image description here

But then I used an emulator with lower version and my theming had no effect:

enter image description here

I digged a little deeper and discovered that abc_action_menu_item_layout.xml is used for action menu items and it has a line android:textAppearance="?attr/actionMenuTextAppearance" So I tried to modify this theme attribute (I also had to add textStyle:bold):

<style name="SecondAttemptTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionMenuTextAppearance">@style/Custom.TextAppearance.AppCompat.Widget.ActionBar.Menu</item>
</style>

<style name="Custom.TextAppearance.AppCompat.Widget.ActionBar.Menu" parent="TextAppearance.AppCompat.Widget.ActionBar.Menu">
    <item name="android:textSize">20sp</item>
    <item name="android:textStyle">bold</item>
</style>

As in the first time, the result was as needed on Lollipop and no effect on any version below.

So, the question is: how to properly change text size for action menu item?

PS: I created a simple project on github to demostrate my issue


Solution

  • It appears that actionButtonStyle and actionMenuTextAppearance should be used without android: namespace.

    As it can be seen in values-v21/values.xml of support library, Lollipop uses attribute from system theme (note the android: prefix), that's why my attempts worked with it:

    <style name="Base.V21.Theme.AppCompat.Light" parent="Base.V7.Theme.AppCompat.Light">
        ...
        <item name="actionButtonStyle">?android:attr/actionButtonStyle</item>
        ...
    </style>