I am unable to set a submenu item as checked

杀马特。学长 韩版系。学妹 提交于 2019-12-01 07:16:55

For submenu should be added the group with android:checkableBehavior="single"

Your xml should be something like this:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
    <item
        android:id="@+id/item_1"
        android:checked="true"
        android:title="Item 1" />
    <item
        android:id="@+id/item_2"
        android:title="Item 2" />
    <item
        android:id="@+id/item_3"
        android:title="Item 3" />
    <item
        android:id="@+id/item_4"
        android:title="Item 4" />
    <item android:title="List 1">
        <menu>
            <group android:checkableBehavior="single">
                <item
                    android:id="@+id/subitem_1"
                    android:title="SubItem 1" />
                <item
                    android:id="@+id/subitem_2"
                    android:title="SubItem 2" />
            </group>
        </menu>
    </item>
    <item android:title="List 2">
        <menu>
            <group android:checkableBehavior="single">
                <item
                    android:id="@+id/subitem_3"
                    android:title="SubItem 3" />
                <item
                    android:id="@+id/subitem_4"
                    android:title="SubItem 4" />
            </group>
        </menu>
    </item>
</group>

Maybe try doing it programmatially. Add this method if you have it already. The key part is the "menu.findItem(R.id.item_1).setChecked(true);"

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    menu.findItem(R.id.item_1).setChecked(true);
    return true;
}

I will answer my own question. So the design library was released recently. And as such, it does not have the option yet to set a submenu item as checked. Nor does it have the ability to set items created within groups which is in turn wrapped by another group as checked. Guess, we will have to wait.

I can set it to checked in the event listener where I get menuItem reference. But, you have to set this in XML for it to work:

android:checkable="true"

Now I can set it to checked state, but the display of it is broken (in my case) and I am fixing it, I will add info to this post later.

Generalizing @jlga's answer:

You need to enclose all your item tags within a group tag and set the group's android:checkableBehavior attribute as single.

For instance, assume that you have the following menu arrangement,

<!-- Your previous sections here -->

<item android:title="New Section">
        <menu>

            <item
                android:id="@+id/one"
                android:title="1" />

            <item
                android:id="@+id/two"
                android:title="2" />

        </menu>
</item>

setChecked would have no effect on any item in this "New Section". Hence, to be able to do so, change it as follows:

<!-- Your previous sections here -->

<item android:title="New Section">
        <menu>
            <group android:checkableBehavior="single">
                <item
                    android:id="@+id/one"
                    android:title="1" />

                <item
                    android:id="@+id/two"
                    android:title="2" />

             </group>
        </menu>
</item>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!