How to change background color popup menu android
Add popupMenu style to ur 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>
manifest.xml:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
.............
</application>
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.
Android change background color of popup menu
To change the color of the Options Menu in Android, changing themes and styles won't help. You have to initialise LayoutInflater Factory Class.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
getLayoutInflater().setFactory(new Factory() {
@Override
public View onCreateView(String name, Context context,
AttributeSet attrs) {
if (name .equalsIgnoreCase(“com.android.internal.view.menu.IconMenuItemView”)) {
try {
LayoutInflater f = getLayoutInflater();
final View view = f.createView(name, null, attrs);
new Handler().post(new Runnable() {
public void run() {
view.setBackgroundResource(R.drawable.your_color);
((TextView) view).setTextColor(Color.your_color);
}
});
return view;
} catch (InflateException e) {
} catch (ClassNotFoundException e) {
}
}
return null;
}
});
return super.onCreateOptionsMenu(menu);
}
popup menu background color change not working
You can customize the background color using the android:popupBackground
attribute.
<!-- Popup Menu -->
<style name="MyPopup" parent="@style/Widget.MaterialComponents.PopupMenu">
<item name="android:popupBackground">@color/custom</item>
</style>
You can configure globally this style in your app theme using the popupMenuStyle
attribute:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
....
<item name="popupMenuStyle">@style/MyPopup</item>
</style>
Just use:
PopupMenu popup = new PopupMenu(this, anchor);
popup.getMenuInflater().inflate(R.menu.custom_menu, popup.getMenu());
popup.show();
Currently PopupMenu uses colorOnPrimary
to define the textColor. To change it you have to define a textAppearance:
<style name="MytextAppearanceLargePopupMenu" parent="@style/TextAppearance.MaterialComponents.Subtitle1">
<item name="android:textColor">@color/colorAccent</item>
</style>
and then in your app theme adding this attribute:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
<item name="textAppearanceLargePopupMenu">@style/MytextAppearanceLargePopupMenu</item>
</style>
how to change option menu background color
There problem with the material theme
<style name="Theme.MyApplication" parent="Theme.AppCompat.DayNight.DarkActionBar">
<item name="android:itemBackground">@color/bluish_green</item>
</style>
<style name="ThemeOverlay.MyTheme" parent="Theme.MyApplication">
<!-- To change the popup menu and app text color -->
<item name="android:textColor">@color/white</item>
<!-- To change the background of options menu-->
<item name="android:itemBackground">@color/black</item>
<item name="android:popupBackground">@color/black</item>
<item name="android:drawablePadding">0dp</item>
<item name="android:dividerHeight">1dp</item>
</style>
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>
Android: how to change background color in popupmenu
I solved the problem using this code:
Context wrapper = new ContextThemeWrapper(getContext(), R.style.PopupMenu);
PopupMenu popupMenu = new PopupMenu(wrapper, v);
reference: blog.stylingandroid.com
Related Topics
Does Setwidth(Int Pixels) Use Dip or Px
Gridview VS Gridlayout in Android Apps
Trying to Add Adb to Path Variable Osx
Onclick on Viewpager Not Triggered
How to Use Device Default Theme for App
How to Get All Child's Data in Firebase Database
How to Analyze Memory Using Android Studio
What Is Meant by Ems? (Android Textview)
Runonuithread VS Looper.Getmainlooper().Post in Android
Error:Program Type Already Present: Android.Support.Design.Widget.Coordinatorlayout$Behavior
Cannot Find Symbol Databindingcomponent on Android Studio 3.2 Canary 16 Kotlin Project
How to Create a Number Picker Dialog
Differencebetween an Intentservice and a Service
How to Modify Ripple Color When Using Attr/Selectableitembackground as Background
Glide Image Loading with Application Context
Android - How to Check Proguard Obfuscation Has Worked
Why Does Gradle Build My Module in Release Mode When the App Is in Debug