Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to style PopupMenu?

is it possible to change pop-up menu style from default black text on white background to dark background without applying style to the whole activity (which breaks my UI)?

like image 857
Violet Giraffe Avatar asked Sep 28 '12 08:09

Violet Giraffe


People also ask

How do I pass custom layout to PopupMenu?

OnClickListener() { @Override public void onClick(View view) { PopupMenu popupMenu = new PopupMenu(Sample1. this, view); popupMenu. setOnMenuItemClickListener(Sample1. this); popupMenu.

How do I open the pop up menu on Android?

Go to app > res > right-click > New > Android Resource Directory and give Directory name and Resource type as menu. Now, we will create a popup_menu file inside that menu resource directory. Go to app > res > menu > right-click > New > Menu Resource File and create a menu resource file and name it as popup_menu.


3 Answers

Yes, you can

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

And

Context wrapper = new ContextThemeWrapper(this, R.style.YOURSTYLE_PopupMenu);
PopupMenu popup = new PopupMenu(wrapper, view);

Result

custom PopupMenu style in Android

like image 155
Frank Nguyen Avatar answered Sep 20 '22 19:09

Frank Nguyen


You cannot set PopupMenu style directly, but there are other ways.

PopupMenu is created the following way:

PopupMenu popupMenu=new PopupMenu(context, anchorView);

The style of menu is determined by the style of context you pass. So all you need to do is to pass your Activity reference as context, and menu will be styled accordingly.

If you want to define the style yourself, inherit your activity style from one of the default ones and override the following items:

<style name="style" parent="android:Theme.Holo.Light">
    <item name="android:popupMenuStyle">...</item>
    <item name="android:popupAnimationStyle">...</item>
    <item name="android:popupBackground">...</item>
    <!-- etc etc -->
</style>
like image 23
Andrii Chernenko Avatar answered Sep 20 '22 19:09

Andrii Chernenko


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.

like image 37
Jimmy Ilenloa Avatar answered Sep 23 '22 19:09

Jimmy Ilenloa