Android change background color of popup menu

Viewed 1582

I don't understand why is so painful to change a color for an android app. I tried several ways to change the background color of the popup menu in my action bar with no success.

I'm using a AppTheme.NoActionBar to style, obviously, the action bar.

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="navigationIcon">@drawable/ic_return_dark</item>
    <item name="overlapAnchor">false</item>
    <item name="popupMenuStyle">@style/PopupMenu</item>
</style>

<style name="PopupMenu" parent="Widget.AppCompat.PopupMenu">
    <item name="android:popupBackground">@color/color4</item>
</style>

Following the examples that I found, there was this explanation that you need to insert in your custom style (in my case AppTheme.NoActionBar) a custom popupMenuStyle in order to custom the popupBackground, which changes the background color of popup menu. It doesn't work.

enter image description here

What can I do to change the background color of popup menu?

2 Answers

If someone is still wondering is there is a better solution to this problem, here's the explanation and answer.

Since, the question clearly mentions using NoActionBar theme for app, setting popupMenu directly to AppTheme will not work.

Instead you need to apply that theme manually to your toolbar in xml

<androidx.appcompat.widget.Toolbar
  android:layout_width="match_parent"
  android:layout_height="?attr/actionBarSize"
  app:popupTheme="@style/PopupMenu" />

and in your styles.xml file

<style name="PopupMenu" parent="Theme.AppCompat.Light">
    <item name="android:textColor">@color/white</item>
    <item name="android:colorBackground">@color/popup_color</item>
</style>
Related