Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add checkbox on a menu item in ActionBar

I have a problem in my action bar. I'm trying to add a checkbox on a menu item in ActionBar, but it does not work. It only shows the title of the checkbox.

This is my menu.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.tracking.bus.maps.SingleViewMapsActivity" >

        <item
        android:id="@+id/actionbar_alarm"
        android:title="@string/actionbar_alarm"
        app:actionViewClass="android.widget.checkbox"
        app:showAsAction="ifRoom"/>
</menu>
like image 275
Amay Diam Avatar asked Feb 11 '23 15:02

Amay Diam


1 Answers

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Checkable items appear only in submenus or context menus. -->

    <!-- Carefully look at the attribute name checkableBehavior on groups, but
         the attribute name checkable on items. The checkableBehavior encompasses
         the number of items that will be checkable within that group. -->

    <item android:title="None">
        <menu>
            <!-- The none checkableBehavior is default, but we explicitly show it here. -->
            <group
                android:id="@+id/noncheckable_group"
                android:checkableBehavior="none">
                <!-- Notice how these items inherit from the group. -->
                <item
                    android:id="@+id/noncheckable_item_1"
                    android:title="@string/item_1" />
                <item
                    android:id="@+id/noncheckable_item_2"
                    android:title="@string/item_2" />
                <item
                    android:id="@+id/noncheckable_item_3"
                    android:title="@string/item_3" />
            </group>
        </menu>
    </item>

    <item android:title="All">
        <menu>
            <group
                android:id="@+id/checkable_group"
                android:checkableBehavior="all">
                <!-- Notice how these items inherit from the group. -->
                <item
                    android:id="@+id/checkable_item_1"
                    android:title="@string/item_1" />
                <item
                    android:id="@+id/checkable_item_2"
                    android:checked="true"
                    android:title="@string/item_2" />
                <item
                    android:id="@+id/checkable_item_3"
                    android:checked="true"
                    android:title="@string/item_3" />
            </group>
        </menu>
    </item>

    <item android:title="Single">
        <menu>
            <group
                android:id="@+id/exclusive_checkable_group"
                android:checkableBehavior="single">
                <!-- Notice how these items inherit from the group. -->
                <item
                    android:id="@+id/exclusive_checkable_item_1"
                    android:title="@string/item_1" />
                <item
                    android:id="@+id/exclusive_checkable_item_2"
                    android:title="@string/item_2" />
                <item
                    android:id="@+id/exclusive_checkable_item_3"
                    android:checked="true"
                    android:title="@string/item_3" />
            </group>
        </menu>
    </item>

    <item android:title="All without group">
        <menu>
            <!-- Notice how these items have each set. -->
            <item
                android:id="@+id/nongroup_checkable_item_1"
                android:checkable="true"
                android:title="@string/item_1" />
            <item
                android:id="@+id/nongroup_checkable_item_2"
                android:checkable="true"
                android:checked="true"
                android:title="@string/item_2" />
            <item
                android:id="@+id/nongroup_checkable_item_3"
                android:checkable="true"
                android:checked="true"
                android:title="@string/item_3" />
        </menu>
    </item>

</menu>
like image 58
Kamuy Avatar answered Feb 23 '23 11:02

Kamuy