Change popup menu style - not working

change popup menu background android
custom popup menu android
android action bar popup menu style
change popup menu text color android
android popup menu text color programmatically
android popup menu programmatically
android popup menu with icons
popup menu animation android

I'm trying to apply style to Android popup menu. Menu is shown on button click. In my example I want to set black menu background.

So, my menu layout:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:title="Item 1"
        android:id="@+id/menu1">
    </item>

    <item
        android:title="Item 2"
        android:id="@+id/menu2">
    </item>

</menu>

Next, my activity layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.michal.popupmenu.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:onClick="btnClick"
        android:nestedScrollingEnabled="true"/>
</RelativeLayout>

Code to show menu on button click:

public class MainActivity extends AppCompatActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void btnClick(View view)
    {
        showMenu(view);

    }


    public void showMenu(View v)
    {
        PopupMenu popup = new PopupMenu(this, v);
        popup.inflate(R.menu.popup_menu);
        popup.show();
    }
}

Styles xmle was generated automatically. I have only added menu style to set black menu background, here it is:

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

</style>

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

But still menu background is white and it should be black. Any ideas what's wrong?

[edit] According to comments, updated code:

<resources>

    <style name="PopupMenu" parent="@style/Widget.AppCompat.Light.PopupMenu">
        <item name="android:popupBackground">#FF4081</item>
    </style>

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



</resources>

Main activity:

public void showMenu(View v)
    {
        Context wrapper = new ContextThemeWrapper(getApplicationContext(), R.style.PopupMenu);
        PopupMenu popup = new PopupMenu(wrapper, v);

        // This activity implements OnMenuItemClickListener
        //popup.setOnMenuItemClickListener((PopupMenu.OnMenuItemClickListener) this);
        popup.inflate(R.menu.popup_menu);
        popup.show();
    }

Result is not what I expect

Menu background is still black, not a color I want to set.

I also tried the solution mentioned above, but my popupmenu color has not changed, so I ended up doing as follows:

1.Created a custom drawable with required color as follows:

popup_color_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
  <selector
      xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_pressed="true">
     <color
         android:color="@color/colorPrimary"/>
      </item>
      <item>
          <color
              android:color="#655611"/>
      </item>
  </selector>

Added state_pressed to get select effect

2.In my styles.xml, added following code:

<style name="MyPopupMenu" parent="Widget.AppCompat.PopupMenu">
    <item name="android:itemBackground">@drawable/popup_color_drawable</item>
</style>

I am using Theme.AppCompat.Light.DarkActionBar as my base theme for app.

3.Then in my activity

public void showpopup(View view){
    Context wrapper = new ContextThemeWrapper(this, R.style.MyPopupMenu);
    PopupMenu popup = new PopupMenu(wrapper, view);
    popup.inflate(R.menu.popup_menu);
    popup.show();
}

Following is the result I got by using this code

Hopes this helps you.

How to style PopupMenu?, You cannot set PopupMenu style directly, but there are other ways. PopupMenu is If you want to change the background of the PopupMenu, do this The PopupMenu items do not support showing the icons by default. Change popup menu style - not working. Ask Question Asked 3 years, 8 months ago. Active 1 year, 5 months ago. Android change background color of popup menu.

you should try this:

 public void showMenu(View v)
    {
        Context wrapper = new ContextThemeWrapper(getContext(), R.style.PopupMenu);
        PopupMenu popup = new PopupMenu(wrapper, v);
        popup.inflate(R.menu.popup_menu);
        popup.show();
    }

Android [Kotlin] : Make your Popup Menu Style Great Again, It possible to change pop-up menu style… From the hard-working boy want to help community grow up text font, it's so different from others, it will make your app does not professional and then you want to change style? You can keep the other control settings you want, but the right click menu doesn't recognize a CTRL/SHIFT + mouse button for the pop-up menu. If you want to use your custom navigation settings, you can switch your pop-up menu click to be Middle Mouse, Left Mouse Button + Alt, or ALT + Right Click. Please sign in to leave a comment.

In the original question all you needed to do was change:

<item name="android:popupBackground">#FF4081</item>

to:

 <item name="android:itemBackground">@android:color/white</item>

(Question) How change PopupMenu Style! · Issue #34 · mancj , Hi there, I've tried to change SearchBar Menu style by using style, but does not work! I'm using search bar in fragment, my parent activity style is  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.

my problem was using the wrong popup inflater. Changing from:

popup.menuInflater.inflate(R.menu.menu_language, popup.menu)

to

popup.inflate(R.menu.menu_language)

works!

Change popup menu style, Change popup menu style - not working. android popup menu text color programmatically custom popup menu android popup menu text color android popup  How to change background color popup menu android i mean it is not working in use i use item> </style> here i want to change popup menu background in a

Adobe Dreamweaver CS4: Comprehensive Concepts and Techniques, For some properties, however, changes are not applied until you click outside the button arrow displays a pop-up menu from which you can select a style. Context wrapper = new ContextThemeWrapper(mContext, R.style.popupMenuStyle); PopupMenu popup = new PopupMenu(wrapper, sourceView); This does show a dividing line, BUT the height of the menu doesn't appear to be calculated correctly to include the new divider and a vertical scrollbar is displayed.

Menus, Known issues · StackOverflow Actions in a popup menu should not directly affect the corresponding However, you should use onCreateOptionsMenu() only to create the initial menu state and not to make changes during the activity Providing an overflow-style menu for actions that relate to specific  Home / Code Snippets / jQuery Code Snippets / Styled Popup Menu Styled Popup Menu . Author like the mac style popup though, keep up the good work. If I change

Elevation not shown in PopupMenu in Android, Elevation is not working in PopupMenu. I tried to change popup menu header style. I have a </style> EDIT 2: Context Menu-creation #Override public void  The Start Menu is the central hub for everything in Windows 10, and because of that, its default look is very “stock”. The good news is that there are plenty of ways to customize the Start

Comments
  • How co change text background and text color with this soultion? If I set color to transparent, I'm getting text background not transparent.
  • you can change the textcolor by mentioning <item name="android:textColor">#ff0000</item> in MyPopupMenu
  • It works... however it looks bad when menu item is selected (by tap and hold on it), because menu item color is changed and text background is not. Is there any solution for it?
  • Instead of changing background property, use itemBackground, I have updated my answer
  • Thanks, it worked. However, I can't set lower opacity to make menu transparent, is there any solution for it?
  • In my case, getApplicationContext() and it works. Why can't I declare style directly in xml?
  • bcz its inflated view.. you cant declare style to inflated view. and also its menu's xml file. i tried so many time, and i failed.. so i did this, and its work..
  • One edit... it does not work. Now menu background is always black and text is always white. Not values I set.
  • what do you meant.. Now menu background is always black and text is always white, this is what you want right..? and also you can change text color
  • In my styles.xml I set background color to red. But with your code I'm getting always menu background black and menu text white, no matter what color I set. But - I have gateContext undefined, so I used getApplicationContext instead.