0

I have a menu_item and here is the code:

 <item
    android:id="@+id/action_search"
    android:actionViewClass="android.widget.TextView"
    android:title="In Stocks"
    app:showAsAction="always" />

<item
    android:id="@+id/action_checkbox"
    app:actionViewClass="android.widget.CheckBox"
    android:title="@string/action_check"
    app:showAsAction="always"
    />

I am trying to catch Menu_item_clicked_event So, I write following code in Activity file

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    switch (id) {
        case R.id.action_checkbox:
            if (item.isChecked()) item.setChecked(false);
            else item.setChecked(true);
            return true;
        case R.id.action_search:
            System.out.println("xas");
        default:
            return super.onOptionsItemSelected(item);
    }
}

When I am pressing the action_search it's working but not for action_checkbox. Why this is happening? I am getting No exception in ADB logs. Is this is the right way to do check for Checkboxes?

1 Answers1

0

app:actionViewClass should be android:actionViewClass="android.widget.CheckBox"

 <item
        android:id="@+id/action_checkbox"
        android:actionViewClass="android.widget.CheckBox"
        android:icon="@drawable/unchecked"
        android:checkable="true"
        android:checked="false"
        android:title="@string/action_check"
        app:showAsAction="always"
        />

However checkbox will not be visble. Referthis

you could have your own icon and set it android:icon=

And in class

case R.id.action_checkbox:
        if(item.isChecked()){
        item.setIcon(R.drawable.unchecked);
        }
        else {
        item.setIcon(R.drawable.checked);
        }
    item.setChecked(!item.isChecked());
break;
Community
  • 1
  • 1
Dhinakaran Thennarasu
  • 3,336
  • 4
  • 26
  • 39
  • That means I need to replace the icon with selected checkbox when user click on it –  Oct 26 '15 at 05:17