Android popupmenu background color

Change Popup menu background color

NOTE: I have searched for an hour and tried all solutions already provided by stackoverflow.

I am studying theme overlays. I have made a sample app, which opens a popup menu on clicking an action bar icon. Here is my styles.xml

and here is my toolbar style.

I have set the popupTheme to the one I have in my styles.xml . Now I want to change the background color of popup menu, which is currently white.

Here is the code.

12 Answers 12

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)

Note this requires nothing extra in your layout file.

(2) Use a ContextThemeWrapper

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

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 😉

Читайте также:  Dark темы для андроида

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 change background color popup menu android

I generate each option of menu from querying database. Here is my code.

Everything is okay, but the problem is how to change color of option menu or background color of popup menu (from black to white), Is it possible ? Thanks

13 Answers 13

Add popupMenu style to ur AppTheme:

Please Add Following lines in style xml file, I hope this will help for material design application. Style.xml

If your are using AppCompact-v7 then you can style PopupMenu as below:

NOTE: PopMenu always works with Activity Context, not with Application context.

if you are using AppCompat theme then use

at first define your style for popup menu background in styles.xml file in my case i use this.

here i want to change popup menu background in a specific activity , so apply theme(android:theme=»@style/AppTheme») with activity declaration and it’s surely working. another important thing to create popup menu using this code PopupMenu popup = new PopupMenu(your activity, viewObj);

In the layout where you are giving toolbar, specify theme by app:popupTheme=»@style/MyPopupMenu»

Источник

Change background popupMenu in Android

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

This is my code:

Apply in AndroidManifest.xml

11 Answers 11

the following styles working perfectly for me.

here, parent should be the AppTheme parent

and in your code use these lines.

i hope it will work.

If bgPopumMenu is your image then use this.

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

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

and this is my style,

Читайте также:  Смартфон umidigi bison 2021 nfc android 11 ip68 ip69k

If you are using custom theme :

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

Use this code : R.style.popupMenuStyle for popup menu in java class :

  1. this is custom style of popup- menu for your own background drawable

then change in your own theme «popupMenuStyle»

PopupMenu popup = new PopupMenu(context, view);

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

what about this :

and write this in the setMenuBackground() method

You cannot give android:popupBackground as just a color. You should use or create a drawable .

You can use this link http://jgilfelt.github.io/android-actionbarstylegenerator/ to generate your desired color. And set it to that drawable .

Try to define android:actionBarWidgetTheme in your theme:

If bgPopumMenu is your drawable then use this

You just put that directly into your AppTheme like so

You can easily create a style for popup menu and apply that style to a theme and you can assign the theme to the Activity/Parent Activity*** in the android manifest like this

Create @style/PopupMenu like this

Assign the MyThemePopup theme to your Activity/Parent Activity*** in AndroidManifest as

***When you using the fragment apply the theme for the parent activity defined in AndroidManifest

Источник

How to change the background color of Action Bar’s Option Menu in Android 4.2?

I’d like to change the background color of the option (overflow) menu in Android 4.2. I have tried all the methods but it is still showing the default color set by the theme. I used the following code & XML configs.

MainActivity.java

Menu.xml

color.xml

The above setMenuBackground is not taking any effect:

In the above picture, I want to change the menu background from black to the blue color in the Action Bar. How can I achieve this, and what I did do wrong?

17 Answers 17

In case people are still visiting for a working solution, here is what worked for me:— This is for Appcompat support library. This is in continuation to ActionBar styling explained here

Following is the styles.xml file.

and this is how it looks—MenuItem background color is skyblue and MenuItem text color is pink with textsize as 25sp:—

The Action Bar Style Generator, suggested by Sunny, is very useful, but it generates a lot of files, most of which are irrelevant if you only want to change the background colour.

Читайте также:  Huawei nova 5t сброс аккаунта google 10 андроид

So, I dug deeper into the zip it generates, and tried to narrow down what are the parts that matter, so I can make the minimum amount of changes to my app. Below is what I found out.

In the style generator, the relevant setting is Popup color, which affects «Overflow menu, submenu and spinner panel background».

Go on and generate the zip, but out of all the files generated, you only really need one image, menu_dropdown_panel_example.9.png , which looks something like this:

So, add the different resolution versions of it to res/drawable-* . (And perhaps rename them to menu_dropdown_panel.9.png .)

Then, as an example, in res/values/themes.xml you would have the following, with android:popupMenuStyle and android:popupBackground being the key settings.

And, of course, in AndroidManifest.xml :

What you get with this setup:

Note that I’m using Theme.Holo.Light as the base theme. If you use Theme.Holo (Holo Dark), there’s an additional step needed as this answer describes!

Also, if you (like me) wanted to style the whole Action Bar, not just the menu, put something like this in res/drawable/blue_action_bar_background.xml :

Works great at least on Android 4.0+ (API level 14+).

Источник

android popup menu text color (AppCompat)

I need to change text color of a popuo menu but I don’t find any way for do this, I can change background of popmenu but not the text, I edit the style.xml in this way:

where is the mistake?

4 Answers 4

and add in AppTheme

I think that you are using TextAppearance.AppCompat.Base.Widget.PopupMenu. Here is the error, you are using another parent that doesn´t response the current style.

You have to use:

Context wrapper = new ContextThemeWrapper(getContext(), R.style.PopupMenu); final PopupMenu popupMenu = new PopupMenu(wrapper, view);

Not the answer you’re looking for? Browse other questions tagged android colors popupmenu or ask your own question.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.12.3.40888

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Оцените статью