Change background popupMenu in Android
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 :
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">
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);
- 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>
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 !
user1854307
Updated on April 22, 2020Comments
-
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 about 10 yearsIs bgPopupMenu is your image or color name??
-
user1854307 about 10 yearsbgPopupMenu - this color.
-
Piyush about 10 yearshave you created color xml file for it?
-
Piyush about 10 yearsJust check my answer now.
-
user1854307 about 10 yearscolor created in xml file.
-
user1854307 about 10 yearsI tried bgPopumMenu use drawable, but this not helped.
-
Piyush about 10 yearsBut have you checked my full answer now.
-
Anshul Tyagi almost 9 years@PiyushGupta it never worked for me. I'm still looking for an appropriate answer.
-
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 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 over 7 yearsThis is probably the only answer which works without any hassle. Kudos!
-
Lisitso over 7 yearsI 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 about 7 yearsError will occur: A factory has already been set on this LayoutInflater
-
Lucian Novac about 7 yearsbecause of a small change on the support library, see this link for more details: stackoverflow.com/questions/13415284/…
-
Kashish Sharma about 4 yearsThis 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 about 4 yearsYou 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..