Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Checkable Menu Item

I have the following menu layout in my Android app:

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android">     <item android:id="@+id/item1"            android:titleCondensed="Options"           android:title="Highlight Options"            android:icon="@android:drawable/ic_menu_preferences" />     <item android:id="@+id/item2"           android:titleCondensed="Persist"          android:title="Persist"           android:icon="@android:drawable/ic_menu_preferences"           android:checkable="true" /> </menu> 

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?

like image 704
Icemanind Avatar asked Jun 04 '11 20:06

Icemanind


People also ask

How do you respond to a menu item?

Responding to Menu Item ClicksWhen a menu item is clicked, Android calls the onOptionsItemSelected callback method of the Activity class by passing a reference to the clicked menu item. You then use the getItemId() method on the MenuItem to see which item it is.

What types of menus does Android support?

There are three types of menus in Android: Popup, Contextual and Options. Each one has a specific use case and code that goes along with it. To learn how to use them, read on.

What is onCreateOptionsMenu in Android?

Find the onCreateOptionsMenu(Menu menu) method which needs to override in Activity class. This creates menu and returns Boolean value. inflate inflates a menu hierarchy from XML resource. public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater(). inflate(R.


2 Answers

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().

like image 79
Sergey Glotov Avatar answered Sep 28 '22 10:09

Sergey Glotov


Wrap the items in a group element, like this:

<menu xmlns:android="http://schemas.android.com/apk/res/android">     <group android:checkableBehavior="all">         <item android:id="@+id/item1"               android:titleCondensed="Options"               android:title="Highlight Options"               android:icon="@android:drawable/ic_menu_preferences">         </item>         <item android:id="@+id/item2"               android:titleCondensed="Persist"               android:title="Persist"               android:icon="@android:drawable/ic_menu_preferences"               android:checkable="true">         </item>     </group> </menu> 

From the Android docs:

The android:checkableBehavior attribute accepts either:

single - Only one item from the group can be checked (radio buttons)

all - All items can be checked (checkboxes)

none - No items are checkable

like image 40
Gabriel Negut Avatar answered Sep 28 '22 08:09

Gabriel Negut