Android Toolbar menu text color
Asked Answered
B

6

29

I'm trying to change my Toolbar's menu item text color here, but it doesn't work. Here's my style:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

    <item name="toolbarStyle">@style/AppTheme.ToolbarStyle</item>
    <item name="buttonStyle">@style/AppTheme.ButtonStyle</item>
    <item name="colorControlHighlight">@color/colorPrimary</item>
</style>
<style name="AppTheme.ToolbarStyle" parent="Base.Theme.AppCompat.Light.DarkActionBar">
    <item name="android:background">@color/colorPrimary</item>
    <item name="titleTextColor">@android:color/white</item>
    <item name="titleTextAppearance">@style/TextAppearance.AppCompat.Widget.ActionBar.Title
    </item>
    <item name="actionMenuTextColor">@android:color/white</item>
</style>

layout xml:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:title="@string/app_name"
    app:titleMarginStart="@dimen/margin_l"
    />

I have tried to set the Toolbar theme directly in xml, but the menu item is still back. Is there a solution to this?

enter image description here

Berzelius answered 7/4, 2017 at 10:54 Comment(1)
Add this and let me know <item name="colorControlNormal">@android:color/white</item>.Winze
P
12

Having a material toolbar you can play with styling modifying text title and menu texts as follows:

<com.google.android.material.appbar.MaterialToolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?actionBarSize"
    android:background="@android:color/white"
    android:elevation="6dp"
    android:theme="@style/App.ToolbarStyle"
    app:titleTextAppearance="@style/App.ToolbarTitleTex"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:menu="@menu/create_item_menu"
    app:titleTextColor="@android:color/black" />

Where this style lets you change the color of the icon of the left:

<style name="App.ToolbarStyle" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
    <item name="colorOnPrimary">@color/colorPrimary</item>
</style>

Also you can change the title text appearance with another style:

<style name="App.ToolbarTitleTex" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
    <item name="android:textSize">18sp</item>
    <item name="fontFamily">@font/roboto_bold</item>
</style>

And finally to change the menu item style you need to add this item property to the main style/theme of the app (the one you set in the AndroidManifest file:

<item name="actionMenuTextAppearance">@style/App.ToolbarMenuItem</item>

With:

<style name="App.ToolbarMenuItem" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
    <item name="android:textSize">14sp</item>
    <item name="fontFamily">@font/roboto_bold</item>
    <item name="textAllCaps">true</item>
    <item name="android:textStyle">bold</item>
</style>

The final result would be something like this: enter image description here

Psychographer answered 31/10, 2020 at 20:40 Comment(1)
Not able to add this in my main theme <item name="actionMenuTextAppearance">@style/App.ToolbarMenuItem</item>Puck
E
25

Add these lines in your AppTheme style

<item name="actionMenuTextColor">@color/white</item>
<item name="android:actionMenuTextColor">@color/white</item>
Epistyle answered 7/4, 2017 at 11:1 Comment(3)
android:actionMenuTextColor is for higher API levels, and actionMenuTextColor is used if you are using support library.Epistyle
But why did it work in AppTheme, but didn't work in AppTheme.ToolbarStyle?Iosep
I cant change title toolbar color in API 17 with this way!!Guesstimate
D
13

Modern way under Material3 theming.

<style name="AppTheme" parent="Theme.Material3.Dark">
    <item name="toolbarStyle">@style/ToolBarStyle</item>
...
</style>
<style name="ToolBarStyle" parent="@style/Widget.Material3.Toolbar">
    <item name="titleTextColor">@android:color/holo_green_light</item>
    <item name="navigationIconTint">@android:color/holo_blue_light</item>
    <item name="materialThemeOverlay">@style/ToolbarThemeOverlay</item>
</style>
<style name="ToolbarThemeOverlay" parent="">
    <item name="actionMenuTextColor">@android:color/holo_red_light</item>
</style>

materialThemeOverlay - is the key to apply the colors to action menu items enter image description here

Deina answered 22/10, 2021 at 10:59 Comment(0)
P
12

Having a material toolbar you can play with styling modifying text title and menu texts as follows:

<com.google.android.material.appbar.MaterialToolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?actionBarSize"
    android:background="@android:color/white"
    android:elevation="6dp"
    android:theme="@style/App.ToolbarStyle"
    app:titleTextAppearance="@style/App.ToolbarTitleTex"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:menu="@menu/create_item_menu"
    app:titleTextColor="@android:color/black" />

Where this style lets you change the color of the icon of the left:

<style name="App.ToolbarStyle" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
    <item name="colorOnPrimary">@color/colorPrimary</item>
</style>

Also you can change the title text appearance with another style:

<style name="App.ToolbarTitleTex" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
    <item name="android:textSize">18sp</item>
    <item name="fontFamily">@font/roboto_bold</item>
</style>

And finally to change the menu item style you need to add this item property to the main style/theme of the app (the one you set in the AndroidManifest file:

<item name="actionMenuTextAppearance">@style/App.ToolbarMenuItem</item>

With:

<style name="App.ToolbarMenuItem" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
    <item name="android:textSize">14sp</item>
    <item name="fontFamily">@font/roboto_bold</item>
    <item name="textAllCaps">true</item>
    <item name="android:textStyle">bold</item>
</style>

The final result would be something like this: enter image description here

Psychographer answered 31/10, 2020 at 20:40 Comment(1)
Not able to add this in my main theme <item name="actionMenuTextAppearance">@style/App.ToolbarMenuItem</item>Puck
V
9

You need to declare a Theme like this

<style name="Theme.PopupOverlay.Menu" parent="ThemeOverlay.AppCompat.Light" >
    <item name="android:textColor">@android:color/white</item>
</style>

And then, in your Toolbar in layout view you must specify to use that theme

    <androidx.appcompat.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/Theme.PopupOverlay.Menu" />
Vacillate answered 22/7, 2020 at 22:4 Comment(0)
D
0

To get control over the whole text appearance based on @Osvel Alvarez Jacomino's answer, you should do the following:

Define your TopAppBar theme:

<style name="TopAppBar" parent="@style/Widget.Material3.Toolbar">
        <item name="popupTheme">@style/Theme.PopupOverlay.Menu</item>
</style>

PopupTheme should utilize these attributes:

<style name="Theme.PopupOverlay.Menu" parent="ThemeOverlay.Material3.Light">
        <item name="textAppearanceSmallPopupMenu">@style/TypographyStyle</item>
        <item name="textAppearanceLargePopupMenu">@style/TypographyStyle</item>
</style>

The typography style might look something like this:

<style name="TypographyStyle" parent="TextAppearance.Material3.BodyLarge">
        <item name="android:textColor">@android:color/darker_gray</item>
        <item name="android:letterSpacing">0.02</item>
</style>
Diamagnetism answered 30/11, 2023 at 16:52 Comment(0)
D
-1

In your theme file you have to put this :

<style name="AppTheme.ActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
         ...
    <item name="actionMenuTextColor">@color/text_color</item>
         ...
 </style>

and apply above theme to Toolbar view as like this android:theme="@style/AppTheme.ActionBar"

detailed example:

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:id="@+id/main_toolbar"
   android:layout_width="match_parent"
   android:layout_height="?attr/actionBarSize"
   android:background="?attr/colorPrimary"
   android:layout_gravity="top"
   app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
   android:theme="@style/AppTheme.ActionBar"/>
Depside answered 7/4, 2017 at 11:2 Comment(3)
This is not the correct answer. I have already tried it. You have to read the questions carefully.Iosep
@ЄвгенГарастович ok, But you didn't provide api level you are targeting. i have added this <item name="actionMenuTextColor">@color/text_color</item>, For higher version <item name="android:actionMenuTextColor">@color/white</item>Depside
It doesn't matter. Neither of those lines work for Toolbar theme. I had to add these lines to AppTheme directly, then everything worked. Please check the accepted answer.Iosep

© 2022 - 2024 — McMap. All rights reserved.