How to change the background color of Popupmenu?

Asked

Viewed 116 times

3

To change the background color of the 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

xml style.

<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>

inserir a descrição da imagem aqui

1 answer

3


The type of Resource that Popupmenu expects how Background is an image, more exactly a 9-Patch bitmap, and not a Color.

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

Declare a style in the archive values\styles.xml and attribute the bitmap at the popupBackground.

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

In the style concerning the Theme of the application indicates that the Popupmenu shall use that new style.

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

The archive values\styles.xml will look 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:

inserir a descrição da imagem aqui

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

  • It worked perfectly! Abusing a little of your good will, where I get a png of that in white color. I tried to change the orange png in Fireworks but it was not good.

  • 1

    A quick way is to go here. Change the color of Popup color to white. Do the download of ZIP and use the file menu_dropdown_panel.9.png which is in the folders for different densities.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.