0

I am trying to style the items of my popup so they look like a list of buttons below eachother. The only problem is that I can't get to change anything of the popup items. I have tried to set a global popupMenuStyle in my app style but that didn't to anything. I tried to set an actionLayout on the menu items but still no change.

How can I change the styling of my popup menu items?

My menu:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/test1"
        android:title="Test" />
    <item android:id="@+id/test2"
        android:title="Test 2" />
</menu>

How I open the popup menu:

PopupMenu popupMenu = new PopupMenu(getContext(), mButton);
popupMenu.getMenuInflater().inflate(R.menu.popup_menu, popupMenu.getMenu());
popupMenu.show();
Bart Bergmans
  • 4,061
  • 3
  • 28
  • 56

1 Answers1

0

Try this as part of your styles.xml file:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:popupMenuStyle">@style/PopupMenu</item>
        <item name="android:textAppearanceLargePopupMenu">@style/myPopupMenuTextAppearanceLarge</item>
        <item name="android:textAppearanceSmallPopupMenu">@style/myPopupMenuTextAppearanceSmall</item>
    </style>

    <style name="PopupMenu" parent="@android:style/Widget.PopupMenu">
        <item name="android:popupBackground">#FFFFFF</item>
        <item name="android:divider">#444444</item>
        <item name="android:dividerHeight">1px</item>
        <item name="android:background">#FFFFFF</item>
    </style>

    <style name="myPopupMenuTextAppearanceSmall" parent="@android:style/TextAppearance.DeviceDefault.Widget.PopupMenu.Small">
        <item name="android:textColor">#000000</item>
        <item name="android:textSize">12sp</item>
        <item name="android:background">#FFFFFF</item>
    </style>

    <style name="myPopupMenuTextAppearanceLarge" parent="@android:style/TextAppearance.DeviceDefault.Widget.PopupMenu.Large">
        <item name="android:textColor">#000000</item>
        <item name="android:textSize">18sp</item>
        <item name="android:background">#FFFFFF</item>
    </style>

</resources>

and then in your xml layout file for the activity add this line:

style="@style/AppTheme"

or in your AndroidManifest.xml file, add this to the application tag:

android:theme="@style/AppTheme"

This will affect how Android renders a popup menu in your app.

Gail
  • 316
  • 2
  • 13