Q:

checkbox menu item doesn't work

I have this in my menu_check.xml But when I click on the check nothing happens so the Toast never show... what's the problem?  Thanks a lot
  <? xml version="1.0" encoding="utf-8"? >
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=". MainActivity">

    <item
        android:id="@+id/action_selecciontodo"
        android:title="@string/check"
        android:checkable="true"
        app:actionViewClass="android.widget. CheckBox"
        app:showAsAction="always" />

</menu>

and this in my java class
 public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_check, menu);

    checkBox = (CheckBox) menu.findItem(R.id.action_selecciontodo).getActionView();
    checkBox.setText("Select all");

    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_selecciontodo:
            CheckBox checkBox= (CheckBox) item.getActionView();
            if (checkBox.isChecked())
            Toast.makeText(getApplicationContext(), "You selected all", Toast. LENGTH_SHORT).show();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

A:

No need to app:actionViewClass="android.widget. CheckBox" just do like this
in oncreate options menu
 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.main_menu, menu);
    return true;
}

and in onOptionsItemSelected write this code
 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menu_bookmark:
            if (item.isChecked()) {
                item.setChecked(false);
                Toast.makeText(MainActivity.this, "Un Checked", Toast. LENGTH_SHORT).show();
            } else {
                item.setChecked(true);
                Toast.makeText(MainActivity.this, "Checked", Toast. LENGTH_SHORT).show();
            }
            break;
    }
    return super.onOptionsItemSelected(item);
}

and in your menu.xml in menu folder declare menu like this
<? xml version="1.0" encoding="utf-8"? >

<item android:id="@+id/menu_bookmark"
      android:checkable="true"
      android:title="Bookmark"/>

so when you check uncheck the menu item it show toast and show checkbox checked or unchecked

