37

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,539
  • 7
  • 35
  • 48
user3172337
  • 459
  • 1
  • 5
  • 16

13 Answers13

64

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,607
  • 2
  • 15
  • 14
17

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,878
  • 4
  • 31
  • 53
12

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,709
  • 19
  • 28
  • 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);

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
  • 36,896
  • 19
  • 133
  • 145
  • 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,777
  • 4
  • 31
  • 53
Droid_Mechanic
  • 1,474
  • 14
  • 13
2

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>
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,073
  • 146
  • 84
  • 117
0

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,606
  • 3
  • 16
  • 27
0

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
  • 11
  • 2
-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
  • 36,896
  • 19
  • 133
  • 145
vnshetty
  • 19,781
  • 23
  • 63
  • 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