How to style PopupMenu?
Yes, you can
<style name="YOURSTYLE.PopupMenu" parent="Widget.AppCompat.PopupMenu">
<item name="android:textColor">@android:color/white</item>
<item name="android:itemBackground">@android:color/holo_red_light</item>
</style>
And
Context wrapper = new ContextThemeWrapper(this, R.style.YOURSTYLE_PopupMenu);
PopupMenu popup = new PopupMenu(wrapper, view);
Result
Change popup menu style - not working
I also tried the solution mentioned above, but my popupmenu color has not changed, so I ended up doing as follows:
1.Created a custom drawable with required color as follows:
popup_color_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<color
android:color="@color/colorPrimary"/>
</item>
<item>
<color
android:color="#655611"/>
</item>
</selector>
Added state_pressed to get select effect
2.In my styles.xml
, added following code:
<style name="MyPopupMenu" parent="Widget.AppCompat.PopupMenu">
<item name="android:itemBackground">@drawable/popup_color_drawable</item>
</style>
I am using Theme.AppCompat.Light.DarkActionBar
as my base theme for app.
3.Then in my activity
public void showpopup(View view){
Context wrapper = new ContextThemeWrapper(this, R.style.MyPopupMenu);
PopupMenu popup = new PopupMenu(wrapper, view);
popup.inflate(R.menu.popup_menu);
popup.show();
}
Following is the result I got by using this code
Hopes this helps you.
Customizing popup menu style
For the OverflowMenu you can define in your app theme the actionOverflowMenuStyle
attribute.
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
<item name="actionOverflowMenuStyle">@style/popupOverflowMenu</item>
</style>
With:
<style name="popupOverflowMenu" parent="@style/Widget.MaterialComponents.PopupMenu.Overflow">
<item name="android:popupBackground">@drawable/my_mtrl_popupmenu_background</item>
</style>
The drawable/my_mtrl_popupmenu_background.xml
file:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="?attr/colorSurface"/>
<corners
android:bottomLeftRadius="4dp"
android:bottomRightRadius="4dp"
android:topLeftRadius="4dp"
android:topRightRadius="4dp"/>
</shape>
It the original file there is the padding
:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="?attr/colorSurface"/>
<corners
..../>
<padding
android:bottom="8dp"
android:top="8dp"/>
</shape>
For a popup you can use:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
<item name="popupMenuStyle">@style/popupMenu</item>
</style>
<style name="popupMenu" parent="@style/Widget.MaterialComponents.PopupMenu">
<item name="android:popupBackground">@drawable/my_mtrl_popupmenu_background</item>
</style>
Change Popup menu background color
I wasn't satisfied with the accepted answer since it doesn't really explain why the OPs custom popup style isn't being applied--not just the background, but also things like the text color--so I did my own experimentation.
It's important to note there is a difference between the popup created by the Toolbar
(when it has menu items) and showing one yourself with PopupMenu
. These are governed by different theme attributes. Also, be aware there are two PopupMenu
classes: android.widget.PopupMenu
, and android.support.v7.widget.PopupMenu
.
The theme attribute you need to style PopupMenu
s you show explicitly is android:popupMenuStyle
or popupMenuStyle
. You have a few options to achieve proper application of your custom style:
(1) Use android:popupMenuStyle
in the theme of the activity (or app)
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- if using android.widget.PopupMenu -->
<item name="android:popupMenuStyle">@style/PopupMenu</item>
<!-- if using android.support.v7.widget.PopupMenu -->
<item name="popupMenuStyle">@style/PopupMenu</item>
</style/>
<style name="PopupMenu" parent="ThemeOverlay.AppCompat.Dark">
<item name="android:popupBackground">@color/popupBackground</item>
</style>
PopupMenu popup = new PopupMenu(this, anchorView);
Note this requires nothing extra in your layout file.
(2) Use a ContextThemeWrapper
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- nothing special -->
</style/>
<style name="CustomPopupTheme" parent="ThemeOverlay.AppCompat.Dark">
<!-- if using android.widget.PopupMenu -->
<item name="android:popupMenuStyle">@style/PopupMenu</item>
<!-- if using android.support.v7.widget.PopupMenu -->
<item name="popupMenuStyle">@style/PopupMenu</item>
</style>
<style name="PopupMenu" parent="ThemeOverlay.AppCompat.Dark">
<item name="android:popupBackground">@color/popupBackground</item>
</style>
ContextThemeWrapper ctw = new ContextThemeWrapper(this, R.style.CustomPopupTheme);
PopupMenu popup = new PopupMenu(ctw, anchorView);
Note how this doesn't use R.style.PopupMenu
directly when constructing the ContextThemeWrapper
. This seems a bit roundabout, but it's useful if you want to keep the popup theme separated from activity or app themes (perhaps only some popups need your special theme, for example).
(3) Use your AppBarLayout
's Context
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- nothing special -->
</style/>
<style name="AppBarOverlay" parent="ThemeOverlay.AppCompat.Light">
<!-- if using android.widget.PopupMenu -->
<item name="android:popupMenuStyle">@style/PopupMenu</item>
<!-- if using android.support.v7.widget.PopupMenu -->
<item name="popupMenuStyle">@style/PopupMenu</item>
</style>
<style name="PopupMenu" parent="ThemeOverlay.AppCompat.Dark">
<item name="android:popupBackground">@color/popupBackground</item>
</style>
<style name="PopupOverlay" parent="ThemeOverlay.AppCompat.Dark">
<!-- changes the background of the Toolbar's popup -->
<item name="android:colorBackground">@color/popupBackground</item>
</style>
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
AppBarLayout appBar = (AppBarLayout) findViewById(R.id.app_bar);
PopupMenu popup = new PopupMenu(appBar.getContext(), anchorView);
Since you already have a theme overlay for the AppBar, you can use it to hold your popup theme references. This would also work with the Toolbar's context, at least given the current layout, although note that app:popupTheme
is not actually relevant here since it affects the Toolbar
's popup and not your PopupMenu
. Also note how similar this is to option 2 above, which should clue you in to how the android:theme
attribute works under the hood ;)
In my experiments, android:itemBackground
only worked when I used it in place of android:colorBackground
in the PopupOverlay
style. However, it's better to use android:colorBackground
because that will change the popup's window color, leaving the rounded corners and the selectable item highlight/ripple of the items intact.
How to set the background colour of a Popup menu?
Add popupMenu style to your AppTheme
<style name="AppTheme" parent="android:Theme.Light">
<item name="android:popupMenuStyle">@style/PopupMenu</item>
</style>
<style name="PopupMenu" parent="@android:style/Widget.PopupMenu">
<item name="android:popupBackground">@android:color/white</item>
</style>
then at mainifest.xml
<application
android:theme="@style/AppTheme" >
</application>
Related Topics
Arrayadapter in Android to Create Simple Listview
How to Create a User-Defined Function in SQLite
Android Listview Refresh Single Row
Hide/Show Bottomnavigationview on Scroll
How to Use Web Camera in Android Emulator to Capture a Live Image
Updating Android UI Using Threads
Checkbox Unchecked When I Scroll Listview on Android
Remove App from Recent Apps Programmatically
No Generated R.Java File in My Project
Android Gridlayout with Dynamic Number of Columns Per Row
How to Place Fragment Arrylist Scrollabletabsactivity
Read Only File System on Android
React-Native: Command Not Found
Export a Python Script to an Android Executable (.Apk) with Python-For-Android
Eclipse Error "Adb Server Didn't Ack, Failed to Start Daemon"
Android 4.2 Broke My Encrypt/Decrypt Code and the Provided Solutions Don't Work