item How to change background color popup menu android




popup window background color android (9)

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>

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

final PopupMenu popupMenu = new PopupMenu(getBaseContext(), v); 
SQLiteDatabase db = AdapterDb.getReadableDatabase(); 
Cursor cursor = db.rawQuery(sql, null);
int ctritem = 0;
if (cursor.moveToFirst()) {
    popupMenu.getMenu().add(Menu.NONE, ctritem, Menu.NONE, "ALL ITEMS"); 
    do {
        ctritem++;   
        popupMenu.getMenu().add(Menu.NONE, ctritem, Menu.NONE, cursor.getString(0)); 
    } while (cursor.moveToNext());
}

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


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>

Adding to what Deville suggested, you can also add the following attributes to your theme style.

<style name="style" parent="android:Theme.Holo.Light">        
    <!-- other attributes -->
    <item name="textAppearanceLargePopupMenu">@style/myPopupMenuTextAppearanceLarge</item>
    <item name="android:textAppearanceLargePopupMenu">@style/myPopupMenuTextAppearanceLarge</item>

    <item name="textAppearanceSmallPopupMenu">@style/myPopupMenuTextAppearanceSmall</item>
    <item name="android:textAppearanceSmallPopupMenu">@style/myPopupMenuTextAppearanceSmall</item>

    <item name="popupMenuStyle">@style/myPopupMenuStyle</item>
    <item name="android:popupMenuStyle">@style/myPopupMenuStyle</item>
</style>

Other styles referenced in the above style definition

<style name="myPopupMenuStyle" parent="@style/Widget.AppCompat.Light.PopupMenu">

</style>
<style name="myPopupMenuTextAppearanceSmall" parent="@style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Small">
    <item name="android:textColor">#000000</item>
</style>
<style name="myPopupMenuTextAppearanceLarge" parent="@style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Large">
    <item name="android:textColor">#000000</item>
</style>

You would notice AppCompat in my xml style definitions, that is because I am using the android support library to target lower android API Levels.


You can obviously customize the PopupMenu in your own way. Its quite simple. But first you need to take care of one very important thing.

The PopupMenu is created like this,

PopupMenu popupMenu = new PopupMenu(context, anchorView);

Now the "context" here plays a very important role in styling. The style of the PopupMenu depends on the style of the context that you pass. So be very careful in this. I wasted almost half-and-hour figuring out this.

If you are in a fragment just pass "getActivity()" and you are done.

Styling of the PopupMenu items

Just override the following items in your style,

<item name="textAppearanceLargePopupMenu">@style/myPopupMenuTextAppearanceLarge</item>
<item name="android:textAppearanceLargePopupMenu">@style/myPopupMenuTextAppearanceLarge</item>

<item name="textAppearanceSmallPopupMenu">@style/myPopupMenuTextAppearanceSmall</item>
<item name="android:textAppearanceSmallPopupMenu">@style/myPopupMenuTextAppearanceSmall</item>

And customize the text appearance as you want,

<style name="myPopupMenuTextAppearanceSmall" parent="@style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Small">
            <item name="android:textColor">@color/text_hint_color</item>
</style>

<style name="myPopupMenuTextAppearanceLarge" parent="@style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Large">
            <item name="android:textColor">@color/text_hint_color</item>
</style>

If you want to change the background of the PopupMenu, do this

<item name="popupMenuStyle">@style/myPopupMenuStyle</item>
    <item name="android:popupMenuStyle">@style/myPopupMenuStyle</item>

Advanced Styling

The PopupMenu items do not support showing the icons by default. But showing the icons can make it look so much better. I recommend trying it.

To implement this just put the following code in your activity and you are good to go,

@Override
public boolean onMenuOpened(int featureId, Menu menu) {
    if (featureId == Window.FEATURE_ACTION_BAR && menu != null) {
        if (menu.getClass().getSimpleName().equals("MenuBuilder")) {
            try {
                Method m = menu.getClass().getDeclaredMethod(
                        "setOptionalIconsVisible", Boolean.TYPE);
                m.setAccessible(true);
                m.invoke(menu, true);
            } catch (NoSuchMethodException e) {
                Log.e("tag", "onMenuOpened", e);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
    return super.onMenuOpened(featureId, menu);
}

Hope it helps you all. Thanks.


Change background popupMenu in Android

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.


how to change color of background in menu?

You can override menu style in your app theme from styles xml file using attribute

<item name="actionOverflowMenuStyle">@style/your_custom_optionsMenuStyle</item>

where your_custom_optionsMenuStyle specifies the behaviour of each menu attribute

<style name="your_custom_optionsMenuStyle" parent="Widget.AppCompat.PopupMenu.Overflow">
     <item name="android:popupBackground">your color code</item>
</style>

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

 <style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="popupMenuStyle">@style/popupMenuStyle</item>
</style>

<style name="popupMenuStyle" parent="Widget.AppCompat.PopupMenu">
    <item name="android:popupBackground">@android:color/white</item>
</style>

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



if you are using AppCompat theme then use

<style name="PopupMenu" parent="Widget.AppCompat.PopupMenu">
    <item name="android:popupBackground">@android:color/black</item>

</style>

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="popupMenuStyle">@style/PopupMenu</item>
</style>

otherwise

<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>