android – Adding MenuItem to Group programmatically doesn't add radio button-ThrowExceptions

Exception or error:

I’m adding MenuItems programmatically and I want to include a radio button when doing so.

Any advice?

Menu.xml

<item
        android:id="@+id/filter_lines"
        android:title="Lines">

        <menu
            android:id="@+id/lines_sub_menu"
            android:checkableBehavior="single">

            <group
                android:orderInCategory="101"
                android:id="@+id/lines_group"
                android:checkableBehavior="single">

                <item
                    android:id="@+id/youi"
                    android:title="Hello World"/>

            </group>

        </menu>
    </item>

Adding the MenuItem like this

 val item = linesSubMenu.add(R.id.lines_group, 100, Menu.NONE, lineName)

Look

EDIT: Tried setCheckable(true) doesn’t have singleselectionbehavior

enter image description here

How to solve:

So the answer is to use this method:

linesSubMenu.setGroupCheckable(R.id.lines_group, true, true)

Found here: https://developer.android.com/reference/android/view/Menu#setGroupCheckable(int,%20boolean,%20boolean)

Leave a Reply

Your email address will not be published. Required fields are marked *