I have a checkbox in android which has the following XML:
<CheckBox
android:id="@+id/item_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="itemClicked" />
This is my onClick() method in my Activity class.
public void itemClicked(View v) {
//code to check if this checkbox is checked!
}
I am aware that we can create an object of the checkbox and assign id to it. But is there a better way to achieve the functionality when declaring onClick
method via XML?
Main Activity from here
//check box clicked
try this one :
This will do the trick:
Obviously you have to make your XML with the id of your checkbox :
So, the method to know if the check box is checked is :
(CheckBox) yourCheckBox.isChecked()
it returnstrue
if the check box is checked.You can try this code: