Add checkbox to android action bar

I have a problem when I was adding a checkbox in the action bar, it doesn't show the checkbox in the action bar which only shows the title of the checkbox.

`

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_settings"/>
<item
    android:id="@+id/action_delete"
    android:icon="@drawable/ic_action_discard"
    android:showAsAction="always"
    android:title=""/>
<item
    android:id="@+id/check_all"
    android:actionViewClass="android.widget.checkbox"
    android:showAsAction="always"
    android:title="@string/action_check"/>
<item
    android:id="@+id/action_add"
    android:icon="@drawable/ic_action_add"
    android:showAsAction="always"
    android:title="@string/action_add"/>

      

`this is my .XML menu

+3


source to share


2 answers


You need to change android:actionViewClass="android.widget.checkbox"

to android:actionViewClass="android.widget.checkbox"

so that android can recognize the actual class name of the checkbox.



+4


source


The accepted answer doesn't work for me. The tag Checkable

did the trick:

<item
    android:id="@+id/check_all"
    android:checkable="true"
    android:showAsAction="always"
    android:title="@string/action_check"/>

      



And then in the Activity that inflates it you should have something like this to reflect the change in the monitored state (otherwise it won't change the selected state even if you click it):

public boolean onOptionsItemSelected(MenuItem item) {

    switch(item.getItemId()){
        case R.id.check_all:
            item.setChecked(!item.isChecked());

            return true;
    }
}

      

+4


source







All Articles