3

According to How to style PopupMenu? you cannot set a popupmenu style directly, which seems contrary to https://developer.android.com/reference/android/widget/PopupWindow.html

It states you can specify a style in the PopupMenu constructor. According to http://blog.http417.com/2014/06/styling-popupmenu.html it looks like the attributes I need to specify are "android:popupBackground" and "android:dropDownWidth". However, the following just removes the default style and doesn't apply my desired color or width:

styles.xml:

<style name="popup">
    <item name="android:popupBackground">@color/material_blue_grey_800</item>
    <item name="android:dropDownWidth">350dp</item>
</style>

ActivityA.java

public class ActivityA extends AppCompatActivity implements    
PopupMenu.OnMenuItemClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_a);
    findViewById(R.id.popupBtn).setOnClickListener(new    
    View.OnClickListener() {
        @Override
        public void onClick(View button) {
            PopupMenu popupMenu = new PopupMenu(ActivityA.this, 
                      button, Gravity.RIGHT, 0, R.style.popup);
        }
    }

@Override
public boolean onMenuItemClick(MenuItem item) {
    return false;
    }
}

activity_a.xml:

<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:paddingLeft="@dimen/activity_horizontal_margin"
           android:paddingRight="@dimen/activity_horizontal_margin"
           android:paddingTop="@dimen/activity_vertical_margin"
           android:paddingBottom="@dimen/activity_vertical_margin"
           tools:context=".activities.ActivityA">
  <Button
      android:id="@+id/popupBtn"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_alignParentRight="true"
      android:text="Press to open menu"/>
</RelativeLayout>
Community
  • 1
  • 1
user3148156
  • 168
  • 1
  • 1
  • 13

3 Answers3

0

You cannot apply a style programmatically but you can do it with a theme: https://developer.android.com/guide/topics/ui/themes.html#ApplyAStyle a similar answer can be found here: https://stackoverflow.com/a/2016344/936269

Community
  • 1
  • 1
Lars Nielsen
  • 2,005
  • 2
  • 25
  • 48
0

Add this to your AppTheme ..

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

Good luck!

Sjd
  • 1,261
  • 1
  • 12
  • 11
0

You can declare the styles in your theme like below:

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Other styles -->

        <!-- PopUpMenu styles -->
        <item name="popupMenuStyle">@style/Widget.App.PopupMenu</item>
    </style>

<!-- Widget PopUpMenu Style-->
    <style name="Widget.App.PopupMenu" parent="Widget.AppCompat.Light.PopupMenu">
        <!-- Whatever Styles Put Here-->
    </style>
Otieno Rowland
  • 2,182
  • 1
  • 26
  • 34