0

How do I set the color of RadioButtons in a checkable PopupMenu. I am using the AppCompat version. I successfully set the background color and the text color via the styles, but I cannot figure out how to set the color of the radio buttons.

This is what I have so far;

    <style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
        <item name="popupMenuStyle">@style/myPopupMenuStyle</item>
        <item name="textAppearanceLargePopupMenu">@style/myPopupMenuTextAppearanceLarge</item>
        <item name="textAppearanceSmallPopupMenu">@style/myPopupMenuTextAppearanceSmall</item>
    </style>

   <style name="myPopupMenuStyle" parent="Widget.AppCompat.PopupMenu">
        <item name="android:popupBackground">@color/accent</item>
        <item name="android:textColor">@android:color/white</item>
    </style>


   <style name="myPopupMenuTextAppearanceSmall" parent="TextAppearance.AppCompat.Widget.PopupMenu.Small">
        <item name="android:textColor">@android:color/white</item>
    </style>

    <style name="myPopupMenuTextAppearanceLarge" parent="TextAppearance.AppCompat.Widget.PopupMenu.Large">
        <item name="android:textColor">@android:color/white</item>
    </style>

I have tried many other attributes, but nothing helped.

Sufian
  • 5,997
  • 14
  • 60
  • 111
lionscribe
  • 2,917
  • 1
  • 12
  • 17

3 Answers3

2
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
    <item name="colorAccent">@color/radio_button_color</item>
    <item name="android:textColorSecondary">@color/radio_button_color2</item>
</style>

Source: https://stackoverflow.com/a/26850668/1269953

colorAccent is the primary color for your radio buttons, and textColorSecondary is as it says the secondary for when the radio button is not selected. When using colorAccent in AppCompat note that there is no android: prefix this is the correct way of using it on versions below Lollipop

Community
  • 1
  • 1
Pztar
  • 3,484
  • 5
  • 29
  • 39
  • Technically this is right, but you can only do it by changing the theme for the whole activity or app, which is not what I need. I want to specifically change all the colors only in the PopupMenu. – lionscribe Jul 19 '16 at 18:28
  • I have referenced your reply in the accepted solution. Thanks. – lionscribe Jul 19 '16 at 19:19
1

Based on Galdino's reply at: https://stackoverflow.com/a/34702166/2661303, and based on Pztar's reply, and with some trial and error, I have figured out the solution. I am using the AppCompat PopupMenu.
Add just the following to your style file (no need to add anything to your main theme);

<style name="MyPopupTheme" parent="Widget.AppCompat.PopupMenu">
    <item name="colorAccent">@color/primary_dark</item>
    <item name="android:popupBackground">@color/accent</item>
    <item name="android:textColor">@android:color/white</item>
    <item name="android:textColorSecondary">@android:color/white</item>
</style>

Then when creating PopupMenu, use following;

Context wrapper = new ContextThemeWrapper(getActivity(), R.style.MyPopupTheme);
PopupMenu popup = new PopupMenu(wrapper, bAnchor, Gravity.END | Gravity.RIGHT | Gravity.TOP, 0, R.style.MyPopupTheme);

I don't know why, but even when using the ContextThemeWrapper, I still have to pass the style to the constructor. As we are using the ContextWrapper, we are using a specific theme for this PopupMenu, therefore we can override the accent color for only the PopupMenu. So now I have a reversed PopupMenu, with the accent color as the background, the text and buttons in white, and the selected button in the primary color.

Hope this helps somebody,
Lionscribe

Community
  • 1
  • 1
lionscribe
  • 2,917
  • 1
  • 12
  • 17
  • You saved me a day. Thank you! Don't understand why the style cannot be applied from activity theme: @style/MyPopupTheme It doesn't work ( – Oleksandr Albul Feb 28 '17 at 16:55
-1

An easy way Android Holo Color Choose theme color , ON radio button option , Download zip file copy pics to drwable and copy XML files to in drawable and set them as

here

Community
  • 1
  • 1
theshivamlko
  • 201
  • 2
  • 10