Change Color of checkbox menu item in Android toolbar
Asked Answered
G

1

5

How do I change the color of an unchecked menu item in a toolbar's submenu?

I know that the checked state is defined by the accentColor of the corresponding theme. But I couldn't find a way to define a color for the unchecked state.

toolbar submenu with checks Just to be clear: I can't define a toolbar's menu item using a custom layout XML and I do not have direct access to the view object and the checkbox.

A menu like this is defined like so:

<menu>
        <item
                android:id="@+id/sortByDescriptionDescendingAction_mediumDark"
                android:icon="@drawable/ic_sort_white_24dp"
                android:title="@string/sortByDescriptionDescendingAction"
                android:checkable="true"/>
        <item
                android:id="@+id/sortByDescriptionAscendingAction_mediumDark"
                android:icon="@drawable/ic_sort_white_24dp"
                android:title="@string/sortByDescriptionAscendingAction"
                android:checkable="true"/>
        <item
                android:id="@+id/sortByDateDescendingAction_mediumDark"
                android:icon="@drawable/ic_sort_white_24dp"
                android:title="@string/sortByDateDescendingAction"
                android:checkable="true"
                android:checked="true"/>
        <item
                android:id="@+id/sortByDateDescAction_mediumDark"
                android:icon="@drawable/ic_sort_white_24dp"
                android:title="@string/sortByDateAscendingAction"
                android:checkable="true"/>
</menu>
Greenfinch answered 24/2, 2017 at 19:52 Comment(0)
S
7

Try using R.attr of android: see this

in your values/styles.xml:

Define custom style for toolbar:

  <style name="CustomPopupTheme" parent="ThemeOverlay.AppCompat.Dark">  
    <item name="android:colorControlActivated">@color/BLUE</item>
    <item name="android:colorControlHighlight">@color/BLUE</item>
    <item name="android:colorControlNormal">@color/white</item>
</style> 

Apply it using:

<android.support.v7.widget.Toolbar  
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/CustomPopupTheme" />
Storekeeper answered 24/2, 2017 at 20:5 Comment(1)
I'd love to, but the checkbox in a checkable menu item is not accessible and cannot be defined in a layout file as you are suggesting.Greenfinch

© 2022 - 2024 — McMap. All rights reserved.