Change background popupMenu in Android

24,338

Solution 1

the following styles working perfectly for me.

<style name="popupMenuStyle" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:textColor">@color/color_white</item>
    <item name="android:itemBackground">@color/color_red</item>
</style>

here, parent should be the AppTheme parent

and in your code use these lines.

Context wrapper = new ContextThemeWrapper(context, R.style.popupMenuStyle);
PopupMenu popup = new PopupMenu(wrapper, v);

i hope it will work.

Solution 2

If bgPopumMenu is your image then use this.

<style name="popupMenuStyle" parent="@android:style/Widget.PopupMenu">
<item name="android:popupBackground">@drawable/bgPopumMenu</item>
</style>

You need to apply your style to your AppTheme. So try this.

<style name="AppTheme" parent="android:Theme.Holo.Light">
<item name="android:popupMenuStyle">@style/popupMenuStyle</item>
</style>
<style name="popupMenuStyle" parent="@android:style/Widget.PopupMenu">
<item name="android:popupBackground">@color/bgPopumMenu</item>
</style>

Solution 3

I made some changes of @Raju`s code and the following styles working for me,

Context wrapper = new ContextThemeWrapper(context,R.style.popupMenuStyle);
PopupMenu popup = new PopupMenu(wrapper, YourView);

and this is my style,

<style name="popupMenuStyle" parent="android:Theme.Holo.Light.DarkActionBar">
    <item name="android:popupMenuStyle">@style/MyApp.PopupMenu</item>
    <item name="android:textColor">@color/White</item>
</style>

<style name="MyApp.PopupMenu" parent="android:Widget.Holo.Light.ListPopupWindow">
<item name="android:popupBackground">@color/color_semi_transparent</item>       
</style>

Solution 4

If you are using custom theme :

  1. In Manifest : @style/MyMaterialTheme is my customized theme :

      <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/MyMaterialTheme">
    
  2. Use this code : R.style.popupMenuStyle for popup menu in java class :

Context wrapper = new ContextThemeWrapper(getContext(), R.style.popupMenuStyle);
PopupMenu popup = new PopupMenu(wrapper, v);

//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());

MenuPopupHelper menuHelper = new MenuPopupHelper(wrapper, (MenuBuilder) popup.getMenu(), v);
menuHelper.setForceShowIcon(true);
menuHelper.setGravity(Gravity.RIGHT);
  1. this is custom style of popup- menu for your own background drawable

<style name="popupMenuStyle" parent="@android:style/Widget.PopupMenu">
    <item name="android:popupBackground">@drawable/dropdown_bg</item>
</style>
  1. then change in your own theme "popupMenuStyle"

    <style name="MyMaterialTheme" parent="MyMaterialTheme.Base">
    
        </style>
    
        <style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
            <item name="windowNoTitle">true</item>
            <item name="windowActionBar">false</item>
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorPrimary</item>
    
            <item name="popupMenuStyle">@style/popupMenuStyle</item>
            <item name="android:popupMenuStyle">@style/popupMenuStyle</item>
        </style>
    

Solution 5

I SOLVED it,

PopupMenu popup = new PopupMenu(context, view);

In above code I used context of type Activity not of Context type.

Chill !

Share:
24,338
user1854307
Author by

user1854307

Updated on April 22, 2020

Comments

  • user1854307
    user1854307 about 4 years

    I tried to change background of popupmenu, but my implementation does not work.

    This is my code:

    <style name="MyHoloLight" parent="android:Theme.Holo.Light">
        <item name="android:popupMenuStyle">@style/popupMenuStyle</item>
    </style>
    <style name="popupMenuStyle" parent="@android:style/Widget.PopupMenu">
        <item name="android:popupBackground">@color/bgPopumMenu</item>
    </style>
    

    Apply in AndroidManifest.xml

    <application
            android:hardwareAccelerated="true"
            android:label="@string/app_name"
            android:icon="@drawable/ic_launcher"
            android:theme="@style/MyHoloLight">
    
  • Piyush
    Piyush about 10 years
    Is bgPopupMenu is your image or color name??
  • user1854307
    user1854307 about 10 years
    bgPopupMenu - this color.
  • Piyush
    Piyush about 10 years
    have you created color xml file for it?
  • Piyush
    Piyush about 10 years
    Just check my answer now.
  • user1854307
    user1854307 about 10 years
    color created in xml file.
  • user1854307
    user1854307 about 10 years
    I tried bgPopumMenu use drawable, but this not helped.
  • Piyush
    Piyush about 10 years
    But have you checked my full answer now.
  • Anshul Tyagi
    Anshul Tyagi almost 9 years
    @PiyushGupta it never worked for me. I'm still looking for an appropriate answer.
  • Sandeep Londhe
    Sandeep Londhe over 7 years
    @Piyush we should talk in a professional manner with each other at this platform, and this behaviour is expected from everyone, look at the comment of some looser above.
  • Piyush
    Piyush over 7 years
    @AndroidHacker I know but you can talk with him in your personal chat room and you can do whatever you want to tell him
  • Suhayl SH
    Suhayl SH over 7 years
    This is probably the only answer which works without any hassle. Kudos!
  • Lisitso
    Lisitso over 7 years
    I spend a lot of days trying to get menu color working. This solution is the only one perfectly working. I'm using AppCompact and MenuPopupHelper. Thanks to @Raju !
  • Raptor
    Raptor about 7 years
    Error will occur: A factory has already been set on this LayoutInflater
  • Lucian Novac
    Lucian Novac about 7 years
    because of a small change on the support library, see this link for more details: stackoverflow.com/questions/13415284/…
  • Kashish Sharma
    Kashish Sharma about 4 years
    This will change only an item's background not the whole menus background, so after doing this there'll be still some different colour margins left on top and bottom.
  • axita.savani
    axita.savani about 4 years
    You have to remove margin and it is working on popup-menu for all the item's background color will change.. also your app must use parent="Theme.AppCompat.Light.NoActionBar" this actionbar..