Home > Back-end >  Android Menu Items don't show as checkable
Android Menu Items don't show as checkable

Time:02-20

Here's my xml code

<group
    android:id="@ id/group1"
    >
<item android:id="@ id/btnNightMode"
    android:title="Night Mode"
    android:icon="@drawable/night_mode"
    android:checkable="true"
    android:checked="true"
    />
</group>

Here's my kotlin code

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    if (toggle.onOptionsItemSelected(item)) {
        return true
    }

    when (item.itemId) {
        R.id.btnNightMode -> {
            item.setChecked(false)
            if (item.isChecked) {
                // If item already checked then unchecked it
                item.isChecked = false
                toast("la")
            } else {
                // If item is unchecked then checked it
                item.isChecked = true
                toast("la")
            }
        }
    }
    return super.onOptionsItemSelected(item)

}

For some reason, the checkbox doesn't appear next to the item, any help? It sais in the documentation that I should set it programatcially, so I did, and still it doens't show the checkbox

CodePudding user response:

Try to add app:actionViewClass="android.widget.CheckBox".

<item
    android:id="@ id/btnNightMode"
    android:title="Night Mode"
    android:icon="@drawable/night_mode"
    android:checkable="true"
    android:checked="true"
    app:actionViewClass="android.widget.CheckBox" />
  • Related