15
votes

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

5
Add this and let me know <item name="colorControlNormal">@android:color/white</item>.Aishwarya Tiwari

5 Answers

5
votes

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

20
votes

Add these lines in your AppTheme style

<item name="actionMenuTextColor">@color/white</item>
<item name="android:actionMenuTextColor">@color/white</item>
5
votes

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" />
-1
votes

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"/>
-1
votes

Create theme for toolbar

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

and apply this theme to your Toolbar view like this :

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:layout_width="match_parent"
   android:layout_height="?attr/actionBarSize"
   android:layout_gravity="top"
   android:theme="@style/AppTheme.ActionBar"/>