Search code examples
androidpopupandroid-themeandroid-styles

Why isn't my PopupMenu being styled?


In my app I create a PopupMenu like this:

public void openPopup(View v) {
    PopupMenu menu = new PopupMenu(MainActivity.this, v);
    menu.inflate(R.menu.menu_popup);
    menu.show();
}

I've styled my popups like this:

<style name="Theme.MyTheme" parent="@style/Theme.AppCompat.Light">
    <item name="popupMenuStyle">@style/PopupMenu.MyTheme</item>
    <item name="android:popupMenuStyle">@style/PopupMenu.MyTheme</item>    
</style>

<style name="PopupMenu.MyTheme" parent="@style/Widget.AppCompat.Light.PopupMenu">
    <item name="android:popupBackground">@color/black</item>
    <item name="android:textColor">@color/white</item>
</style>

I also apply the theme which works fine otherwise

<application
    android:theme="@style/Theme.MyTheme"
    ... >

Yet the popup shows with a white background and black text. What can be wrong?


Solution

  • It took some digging, but I figured this out eventually.Either only popupMenuStyle or popupWindowStyle needs to be overwritten. I can't remember which one at the moment.

    I'm not sure that this works with the AppCompat library, especially using listChoiceBackgroundIndicator. I haven't tested it on anything that targets something less than 14.

    <style name="Theme.MyAppTheme" parent="android:Theme.Holo.Light">
    <item name="android:popupMenuStyle">@style/MyAwesomeBackground.PopupStyle</item>
    <item name="android:popupWindowStyle">@style/MyAwesomeBackground.PopupStyle</item>
    <item name="android:textAppearanceLargePopupMenu">@style/CustomTextStyle</item>
    <item name="android:textAppearanceSmallPopupMenu">@style/CustomTextStyle</item>
    <item name="android:listChoiceBackgroundIndicator">@drawable/custom_list_selector</item>
    </style>
    <style name="AwesomeBackground.PopupStyle" parent="Widget.PopupMenu">
       <item name="android:popupBackground">@drawable/custom_background</item>
    </style>
    <style name="CustomTextStyle">
    <!-- some custom text stylings -->
    </style>