Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add Checkbox on Action Bar Android

I've a problem when i was adding Check box on Action Bar, it is not showing Check box on Action Bar only showing checkbox title.

`

<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 menu.XML

like image 682
Praveen Singh Bora Avatar asked Jan 13 '15 09:01

Praveen Singh Bora


People also ask

How to add checkbox in android?

To define the click event handler for a checkbox, add the android:onClick attribute to the <CheckBox> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.

What is Android check box?

Android CheckBox is a type of two state button either checked or unchecked. There can be a lot of usage of checkboxes. For example, it can be used to know the hobby of the user, activate/deactivate the specific action etc. Android CheckBox class is the subclass of CompoundButton class.

Which xml element is used to design a custom checkbox in android?

To customize the checkbox we will implement a selector in the checkbox. xml file under the drawable folder.


1 Answers

Accepted answer not working for me. Checkable tag 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 who inflates it you should have something like this in order to reflect the change of the checked state (otherwise it won't change the selected state even if you press it):

public boolean onOptionsItemSelected(MenuItem item) {

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

            return true;
    }
}
like image 132
Joaquin Iurchuk Avatar answered Sep 21 '22 05:09

Joaquin Iurchuk