1

I am trying to add the checkbox to menu of my Android app. But I don't know why item with android:checkable="true" attribute has this square of checkbox, but behave like others items. Do I have to write something in java code? I can read the value but tapping does not changing it's value...

My menu\main.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/menu_settings"
        android:title="@string/settings"/>
    <item
        android:id="@+id/menu_tests"
        android:title="@string/i_want_to_test"/>
    <item
        android:id="@+id/menu_translating"
        android:enabled="false"
        android:title="@string/i_want_to_translating"/>
    <item
        android:id="@+id/menu_funmade"
        android:checkable="true"
        android:title="Enable funmade"/>

</menu>

Look:

How menu looks

The last option behve like other with no specified behaviour. I can tap and manu closes and state does not change. Hope you can help.

Andret

Andret2344
  • 653
  • 1
  • 12
  • 33

1 Answers1

1

When the user selects an item from the options menu (including action items in the action bar), the system calls your activity's onOptionsItemSelected() method.

The following code goes inside the onOptionsItemSelected(MenuItem item) method in your Activity.

Code:

switch (item.getItemId()) {
        case R.id.menu_settings:
            // Specify actions for the menu click
            return true;
        case R.id.menu_tests:
            // Specify actions for the menu click
            return true;
        case R.id.menu_translating:
            // Specify actions for the menu click
            return true;
        case R.id.menu_funmade:
            // Specify actions for the menu click
            if (item.isChecked()) {
                item.setChecked(false);
            } else {
                item.setChecked(true);
            }
            return true;
    }
    return super.onOptionsItemSelected(item);

You can look at the official docs about checkable menus for more details.

Srikar Reddy
  • 3,650
  • 4
  • 36
  • 58