Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Menu: Two single-checkable groups in the same menu

Tags:

android

My xml code is:

<group android:checkableBehavior="single">
    <item android:id="@+id/group_1_box_1"/>
    <item android:id="@+id/group_1_box_2"/>
</group>

<group android:checkableBehavior="single">
    <item android:id="@+id/group_2_box_1"/>
    <item android:id="@+id/group_2_box_2"/>
</group>

The problem I am having is that when I change the state of group_1_box_1 to true, it not only sets group_1_box_2 to false, it also sets the group_2 boxes to false.

Edit: Here's my code

public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
    case R.id.group_1_box_1:
        item.setChecked(true);
        return true;
    case R.id.group_1_box_2:
        item.setChecked(true);
        return true;
    case R.id.group_2_box_1:
        item.setChecked(true);
        return true;
    case R.id.group_2_box_2:
        item.setChecked(true);
        return true;
    default:
        return super.onOptionsItemSelected(item);
}
like image 448
psoulos Avatar asked Jul 14 '11 19:07

psoulos


1 Answers

You just need to add unique IDs to each of the group items in the xml file.

<group
    android:id="@+id/menuGroup_1"           <!-- Here -->
    android:checkableBehavior="single">
    <item android:id="@+id/group_1_box_1"/>
    <item android:id="@+id/group_1_box_2"/>
</group>

<group
    android:id="@+id/menuGroup_2"           <!-- And here -->
    android:checkableBehavior="single">
    <item android:id="@+id/group_2_box_1"/>
    <item android:id="@+id/group_2_box_2"/>
</group>
like image 176
Matt L Avatar answered Nov 15 '22 06:11

Matt L