Styling Android PopupMenu Divider Lines
Asked Answered
C

2

11

I'm using a couple of PopupMenus in my app and they all working as expected

Menu image

But I would like to have white lines separating the individual items, but I can't find where I'm meant to be setting this. I was hoping I could get to the underlying ListView but that doesn't seem to be possible. I can't see an style item that relates to the divider line either.

Is this possible, where/how should I be setting this?

Cryptogam answered 19/11, 2016 at 15:18 Comment(2)
Does this help @flexicoder? https://mcmap.net/q/334638/-how-to-style-popupmenuPantheas
@Pantheas no thats the text not the divider lineCryptogam
C
13

I've been able to update the divider colour on a PopupMenu using the following technique (based on clues from this answer https://mcmap.net/q/339594/-change-background-popupmenu-in-android)

Create a style as follows...

<style name="popupMenuStyle" >
    <item name="android:textColor">#ffffff</item>
    <item name="android:itemBackground">#000000</item>
    <item name="android:divider">#eaeaea</item>
    <item name="android:dividerHeight">1dp</item>
</style>

Then when you create the menu create a context wrapper

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.

If anyone knows the reason please add a comment

Cryptogam answered 17/12, 2016 at 14:34 Comment(5)
Hey @Cryptogam can you try adding the divider and dividerHeight as below: <style name="popupMenuStyle" > <item name="android:textColor">#ffffff</item> <item name="android:itemBackground">#000000</item> <item name="android:dropDownListViewStyle">@style/DropDown.Theme</item> </style> <style name="DropDown.Theme" parent="ThemeOverlay.AppCompat"> <item name="android:divider">@color/your_color</item> <item name="android:dividerHeight">1dp</item> </style> I could get rid of the vertical scroll bars this way.Hetaera
why it is not working for me on Android 6.0. Only thing works for me is setting the textColor and background but divider does not show up...Omeromero
per our UX I have to use White for background and unfortunately, divider also uses white background. when I change the menu background to any other color I see the divider but not when it is white... So, how do I change the divider color? <item name="android:divider">@android:color/holo_red_dark</item> does not work....Omeromero
@Omeromero - sounds like you need to ask a new questionCryptogam
@pablogeorge's answer solved it for me. Thanks a lot to both!Presbytery
O
2

this is how I achieved it. colorBankground is to change the color of the line divider.

    <style name="PopupMenu">
    <item name="android:itemBackground">@color/background_medium_gray</item>
    <item name="android:background">@android:color/transparent</item>
    <item name="android:textColor">@android:color/black</item>
    <item name="android:colorBackground">@color/BackgroundGray</item>
    <item name="android:dividerHeight">1dp</item>
</style>

    Context context = new ContextThemeWrapper(getActivity(), R.style.PopupMenu);
    final PopupMenu popupMenu = new PopupMenu(context, view);

    final MenuInflater menuInflater = popupMenu.getMenuInflater();
Omeromero answered 21/11, 2017 at 18:40 Comment(1)
I wonder if you'd know how to set the divider width, like 80 percent of the total menu width;Dawndawna

© 2022 - 2024 — McMap. All rights reserved.