17

I am using the toolbar for android. I just want to change the background color of the overflow menu. But it is not changing.

Style xml

<style name="MyDarkToolbarStyle" parent="Widget.AppCompat.Toolbar">
    <item name="popupTheme">@style/PopupMenuStyle</item>
    <item name="theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
</style>

<style name="PopupMenuStyle" parent="android:Widget.Holo.Light.PopupMenu">
    <item name="android:popupBackground">@android:color/white</item>
</style>

Toolbar XML

    <android.support.v7.widget.Toolbar
    android:id="@+id/tool_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/ColorPrimary"
    android:elevation="2dp"
    android:theme="@style/MyDarkToolbarStyle" />
Apurva
  • 7,871
  • 7
  • 40
  • 59
WISHY
  • 11,067
  • 25
  • 105
  • 197

4 Answers4

79

To change the toolbar options menu color, add this to your toolbar element

app:popupTheme="@style/MyDarkToolbarStyle"

Then in your styles.xml define the popup menu style

<style name="MyDarkToolbarStyle" parent="ThemeOverlay.AppCompat.Light">
    <item name="android:colorBackground">@color/mtrl_white_100</item>
    <item name="android:textColor">@color/mtrl_light_blue_900</item>
</style>

Note that you need to use colorBackground not background. The latter would be applied to everything (the menu itself and each menu item), the former applies only to the popup menu.

Apurva
  • 7,871
  • 7
  • 40
  • 59
11

Edit:

if you just want a white overflow popup menu just do this

<android.support.v7.widget.Toolbar
    android:id="@+id/tool_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/ColorPrimary"
    android:elevation="2dp"
    app:theme="@style/MyDarkToolbarStyle"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"" />

and remove the redundant popupTheme your Style xml

<style name="MyDarkToolbarStyle" parent="Widget.AppCompat.Toolbar">
     <item name="theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
</style>

You should also include this in your top (parent) Layout

xmlns:app="http://schemas.android.com/apk/res-auto"
cozeJ4
  • 1,555
  • 2
  • 11
  • 20
10

Simplest way

If you just want a white overflow popup menu just do this:

<android.support.v7.widget.Toolbar
    android:id="@+id/tool_bar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/ColorPrimary"
    android:elevation="2dp"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

Also, take a look at the value of android:layout_height attribute.

Filipe Brito
  • 5,329
  • 5
  • 32
  • 42
0

Option Menu

This is how I changed the background color of the options menu for MaterialToolbar or Toolbar, I used the popupMenuBackground (android:colorBackground not work for API 21 or upper for me) attribute in a custom style and apply that style to the popupTheme attribute of the toolbar.

In layout file:

<com.google.android.material.appbar.MaterialToolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:layout_collapseMode="pin"
    app:menu="@menu/main_menu"
    app:popupTheme="@style/MenuStyle"
    app:title="@string/app_name" />

In styles.xml file:

<style name="MenuStyle" parent="Theme.MaterialComponents.Light">
    <item name="popupMenuBackground">@color/colorSecondary</item>
    <item name="android:textColor">@color/white</item>
</style>
Rakibul Islam
  • 61
  • 2
  • 4