Icemanind Icemanind - 1 year ago 145
Android Question

Android Checkable Menu Item

I have the following menu layout in my Android app:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="">
<item android:id="@+id/item1"
android:title="Highlight Options"
android:icon="@android:drawable/ic_menu_preferences" />

<item android:id="@+id/item2"
android:checkable="true" />

My problem is that the second menu item doesn't appear to be "checkable" when I run my app in the Android emulator. There should be a green tick about the item, right? To indicate that its checkable.

Am I doing something wrong?

Answer Source

Layout looks right. But you must check and uncheck menu item in code.

From the documentation:

When a checkable item is selected, the system calls your respective item-selected callback method (such as onOptionsItemSelected()). It is here that you must set the state of the checkbox, because a checkbox or radio button does not change its state automatically. You can query the current state of the item (as it was before the user selected it) with isChecked() and then set the checked state with setChecked().

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download