How to change the background color of PopupMenu?

3

How to change the background color of PopupMenu and set the height of the shadow. The only result I had was setting the popupBackgound in styles.xml the problem is that it loses the shadow.

minSdkVersion 16 Compatibility API

style.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

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

    </style>

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

I also tested

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light">
   <item name="android:background">@android:color/white</item>
</style>

    
asked by anonymous 24.10.2016 / 00:46

1 answer

3

The type of resource that PopupMenu expects as Background is an image, more exactly a 9-Patch bitmap , not a Color .

After creating the 9-Patch bitmap you want to use put it in the res\drawable folder. You should preferably create one for each screen density.

Declare a style in the file values\styles.xml and assign the bitmap to popupBackground .

<style name="PopupMenu.Laranja" parent="@style/Widget.AppCompat.Light.PopupMenu">
    <item name="android:popupBackground">@drawable/menu_dropdown_panel_background</item>
</style>  

In style for the theme of the application, indicate that PopupMenu should use this new style .

<item name="popupMenuStyle">@style/PopupMenu.Laranja</item>

The file values\styles.xml looks like this:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <!-- Style que o popupMenu deve utilizar -->
        <item name="popupMenuStyle">@style/PopupMenu.Laranja</item>

    </style>

    <!-- Style do popupMenu. -->
    <style name="PopupMenu.Laranja" parent="@style/Widget.AppCompat.Light.PopupMenu">
        <item name="android:popupBackground">@drawable/popup_menu_background</item>
    </style>
</resources>

To test use this 9-Patch bitmap :

Give it the name popup_menu_background.9.png and put it in the res\drawable folder.

    
24.10.2016 / 14:57