Search code examples
androidtoolbarandroid-toolbar

Android Toolbar menu text color


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


Solution

  • 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