40

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

Shashanth
  • 4,995
  • 7
  • 41
  • 51
user3172337
  • 487
  • 1
  • 5
  • 16

13 Answers13

65

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>
user543
  • 3,623
  • 2
  • 16
  • 14
  • and then how I call it in the programm? – user3172337 Jan 20 '14 at 10:35
  • Automatically it will apply to your popup menu. – user543 Jan 20 '14 at 10:42
  • still same, i put it on folder res/values, but it doesn't work – user3172337 Jan 20 '14 at 10:55
  • Did u apply this apptheme to application in ur manifest.xml. See my edit – user543 Jan 20 '14 at 11:01
  • @user3172337 : now is working, i had mistake on android manifest. Thanks – user3172337 Jan 21 '14 at 03:11
  • 2
    This is working for me. Can I change Text color of Popupmenu ? – silwar May 20 '14 at 13:32
  • 3
    Have you controlled the context you passed to the constructor? I had the same problem when using: PopupMenu popup = new PopupMenu(mContext, v); I was passing the wrong context to the PopupMenu constructor. – madx Sep 02 '14 at 14:29
  • 5
    what about the shadow?? with the following it losses the shadow – silverFoxA May 02 '15 at 08:14
  • 3
    how to do it if i am using AppTheme.NoActionBar. i mean it is not working in use i use noactionbar . – redblood Apr 18 '16 at 18:36
  • 1
    check this: http://stackoverflow.com/questions/22320663/change-background-popupmenu-in-android – Lisitso Dec 07 '16 at 19:08
  • 2
    @silverFoxA Shadow can set using setElevation in style. `` – Anjal Saneen Jan 08 '17 at 05:31
  • 3
    I think this answer is not complete and one can not solve the issue just by reading this incomplete post – Ghasem Dec 28 '17 at 08:17
  • If your `AppTheme` is based on `Theme.MaterialComponents.DayNight.DarkActionBar`, you inherit `@style/Widget.Material3.PopupMenu` to keep the shadow, like so: ` – binwiederhier Apr 28 '22 at 00:54
  • It works for a regular Android, however if your device is Nvidia Shield, then no luck. Although text color can be changed using: android:itemTextAppearance, so it should be a similar trick for background. – user2305886 Jul 23 '22 at 19:11
20

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

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>

    <!-- To change the popup menu and app text color  -->
    <item name="android:textColor">@color/colorPrimary</item>

    <!-- To change the background of options menu-->
    <item name="android:itemBackground">@color/skyBlue</item>
</style>

For more details refer this link http://www.viralandroid.com/2016/01/how-to-change-background-and-text-color-of-android-actionbar-option-menu.html

MohanRaj S
  • 1,958
  • 4
  • 30
  • 54
13

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.

Kalpesh
  • 1,767
  • 19
  • 29
  • It works if you create the PopupMenu with the Activity Context like this: new PopupMenu(getActivity(), ...) e not like this: new PopupMenu(getActivity().getApplicationContext(), ...). – Francesco Donzello Nov 14 '15 at 16:51
  • PopMenu always works with Activity Context, not with Application context. If you Down vote this post then I am requesting you that please up vote it. – Kalpesh Nov 19 '15 at 11:21
  • You last voted on this answer Nov 14 at 16:20. Your vote is now locked in unless this answer is edited. Not able right now... – Francesco Donzello Nov 19 '15 at 14:47
  • Using getActivity() rather than application context fixed it for me! – Dunfield Apr 30 '16 at 19:57
  • @Dunfield Great, You can upvote it for help others to identify this as workable solution. – Kalpesh May 09 '16 at 09:52
4

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>
user3024215
  • 196
  • 1
  • 7
3

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

<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    <item name="android:popupMenuStyle">@style/PopupMenu</item>
</style>
<style name="PopupMenu" parent="@android:style/Widget.PopupMenu">
    <item name="android:popupBackground">@android:color/holo_green_light</item>
</style>

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

3

Add PopupMenu theme to your AppTheme:

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

<style name="PopupMenuTheme" parent="Theme.AppCompat.Light">
    <item name="android:background">@android:color/white</item>
</style>

manifest.xml:

<application
    ...
    android:theme="@style/AppTheme">
    ...
</application>
2

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

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:minHeight="?attr/actionBarSize"
    android:background="#2196F3"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    style="@style/DrawerArrowStyle"
    app:popupTheme="@style/MyPopupMenu">

</android.support.v7.widget.Toolbar>
Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
  • Sorry, I thought this fixed it but I really just had to change to getActivity() as the context when creating the popup. – Dunfield Apr 30 '16 at 20:01
2

In Style Your Application Theme

 <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
     <!-- Customize your theme here. -->
 <item name="android:itemBackground">@color/list_background</item>
 <item name="android:itemTextAppearance">@style/MyActionBar.MenuTextStyle</item>
    </style>

For Text Appearence

 <style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="background">@color/list_item_title</item>
        <item name="titleTextStyle">@style/MyActionBarTitle</item>
    </style>
    <style name="MyActionBarTitle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textColor">@color/list_item_title</item>
    </style>

    <style name="MyActionBar.MenuTextStyle"
        parent="style/TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textColor">@color/list_item_title</item>
        <item name="android:textStyle">normal</item>
        <item name="android:textSize">16sp</item>
        <item name="android:gravity">center</item>
    </style>

And Use App Theme In Menifest like:

<application
        android:name="Your Package"
        android:allowBackup="true"
        android:icon="@drawable/launcher_icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
Amir Dora.
  • 2,831
  • 4
  • 40
  • 61
Droid_Mechanic
  • 1,474
  • 14
  • 13
2

There is no need to do changes in ANDROIDMAINFEST.xml

checkout this

you can achieve it by this in fragment or in activity by provideing or masking with it custom theme and after making the custom theme you have to pass that context to the popup menu. first create popup menu layout in menu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/remove"
        android:title="Remove downloads"

        />
</menu>

second style it in style.xml

 <style name="CustomPopUpStyle" parent="Widget.AppCompat.PopupMenu">
    <item name="android:textColor">@android:color/white</item>
    <item name="android:itemBackground">@color/bgColor</item>
</style>
 

method

 private fun showPopupMenu(context: Context, view: View) {
        var wrapper: Context =  ContextThemeWrapper(context, R.style.CustomPopUpStyle)
        val popup = PopupMenu(wrapper, view)
        popup.inflate(R.menu.popup_menu)
        popup.setOnMenuItemClickListener(PopupMenu.OnMenuItemClickListener { item: MenuItem? ->
            when (item!!.itemId) {
                R.id.remove -> {
                    Toast.makeText(context, item.title, Toast.LENGTH_SHORT).show()
                }
            }

            true
        })

        popup.show()
    }

lastly initialization

 showPopupMenu(holder.itemView.context, holder.viewDataBinding.more)

and this i did in adapter of recyclerview which is in fragment

hope you liked it.

the code is self explanatory too.

Gaurav Raj
  • 39
  • 4
1

Please Refer How to style PopupMenu? for more explanation. I was facing the same issue, searched alot but found the solution in the link mentioned.

PopupMenu is created in following way :

PopupMenu popup = new PopupMenu(context, view);

PopupMenu takes the styling of the context which is passed, Passing Activity as the context solved my problem.

Community
  • 1
  • 1
1
<style name="AppTheme1" parent= "android:Theme.DeviceDefault">

    <item name="android:actionBarPopupTheme">@style/popupNew</item>
</style>

<style name="popupNew" parent="android:ThemeOverlay.Material.Light">
    <item name="android:colorBackground">#ffffff</item>
</style>
Pang
  • 9,564
  • 146
  • 81
  • 122
-1

Add this to your style.xml file

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

Set the style on Spinner

<Spinner
android:theme="@style/PopupMenu"
android:popupMenuStyle="@style/PopupMenu"/>

This would work for API level 16+ devices too.

pratham kesarkar
  • 3,770
  • 3
  • 19
  • 29
-2

You can use custom layout like this..

PopupMenu pop = new PopupMenu(MainActivity.this, v);
pop.getMenuInflater().inflate(R.menu.main, pop.getMenu());
pop.show();

Design layout how you want..hope this helps..

Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
vnshetty
  • 20,051
  • 23
  • 64
  • 102
  • means? tried? I dnt knw your requirment...if you use popup dialog then you can change bg color – vnshetty Jan 20 '14 at 11:14
  • @vnshetty I'm using it but I cannot change the textSize of the items. I have tried lots of examples by changing text size in styles.xml but nothing worth. – Anshul Tyagi Aug 13 '15 at 05:21