2

I've been struggling with PopupMenu styling, and after a lot of research, the process of creating a custom PopupMenu style for my theme seemed to be fairly straightfoward. For example the answer on this question seems like an easy way to change the background color of a PopupMenu. However, none of these solutions have worked for my project. So I started digging into the style sources to find out what I should actually be inheriting to style the PopupMenu in my own project.

I'm running my tests on a handset running API 19, and my base theme looks like this:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- ... -->
    <item name="android:popupMenuStyle">@style/CustomPopupMenu</item>
</style>

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

This is the solution that most of the accepted answers here show. But this does not style my PopupMenu. So I dug into the sources to find the geneology of my theme and see what default settings where being used. This is what I found:

<style name="Theme.AppCompat.Light.DarkActionBar" parent="Base.Theme.AppCompat.Light.DarkActionBar"/>
<style name="Base.Theme.AppCompat.Light.DarkActionBar" parent="Base.Theme.AppCompat.Light"> 
<style name="Base.Theme.AppCompat.Light" parent="Base.V7.Theme.AppCompat.Light"/>

And then in Base.V7.Theme.AppCompat.Light I found these default settings:

<!-- Popup Menu styles -->
<item name="popupMenuStyle">@style/Widget.AppCompat.Light.PopupMenu</item>
<item name="textAppearanceLargePopupMenu">@style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Large</item>
<item name="textAppearanceSmallPopupMenu">@style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Small</item>
<item name="listPopupWindowStyle">@style/Widget.AppCompat.ListPopupWindow</item>
<item name="dropDownListViewStyle">?android:attr/dropDownListViewStyle</item>

So as far as I can tell, my theme inherits from Theme.AppCompat.Light.DarkActionBar which uses a default PopupMenu style of @style/Widget.AppCompat.Light.PopupMenu. I then inherit that style to set my own PopupMenu settings. I made sure my Activity and my Application are all using the correct Theme. I'm not sure why else this just won't work. I've also tried these settings:

<item name="android:textAppearanceLargePopupMenu">@style/CustomPopupMenuLarge</item>
<item name="android:textAppearanceSmallPopupMenu">@style/CustomPopupMenuSmall</item>
<item name="android:popupWindowStyle">@style/CustomPopupMenuListWindow</item>

I provided custom styles for each one, and none of them worked. What could be overriding all my custom styles? Any help would be appreciated, because I'm stumped.


EDITS

Here's my manifest...

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.blah.blah">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".Activities.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Here is AppTheme.NoActionBar

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>
Community
  • 1
  • 1
NoChinDeluxe
  • 3,446
  • 1
  • 16
  • 29

2 Answers2

1

Do not specify android: in

<item name="android:popupMenuStyle">@style/CustomPopupMenu</item>.

Instend:

<item name="popupMenuStyle">@style/CustomPopupMenu</item>

李鸿章
  • 343
  • 1
  • 2
  • 10
0

I think you need to define a parent for AppCompat.NoActionBar. Try parent="Theme.AppCompat.Light.NoActionBar" and also add in <item name="android:popupMenuStyle">@style/CustomPopupMenu</item> to it.

Sree
  • 2,727
  • 3
  • 29
  • 47
  • I've already tried this and it made no difference. I tried making `AppTheme` the parent, and now I just gave your suggestion a try and it changes some colors of things, but the PopupMenu remains unchanged. – NoChinDeluxe Apr 21 '16 at 21:24
  • Hmm.. that should be working. Can i see the changes you made with my suggestions – Sree Apr 21 '16 at 21:25
  • I just changed it the way you suggested. I made the top line ` – NoChinDeluxe Apr 21 '16 at 21:28
  • Do one more thing, make your `CustomPopupMenu`'s parent `parent="@android:style/Widget.PopupMenu"` – Sree Apr 21 '16 at 21:31
  • I've already tried that as well, lol. Just tried it again as a sanity check, and nothing changed. I might need to call a priest soon. – NoChinDeluxe Apr 21 '16 at 21:33
  • Can you try to make the theme AppTheme and see if that works. That way we know if the problem lies with that theme – Sree Apr 21 '16 at 21:47