How to check if android checkbox is checked within its onClick method (declared in XML)?

JavaAndroidCheckboxOnclick

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

try this one :

public void itemClicked(View v) {
  //code to check if this checkbox is checked!
  CheckBox checkBox = (CheckBox)v;
  if(checkBox.isChecked()){
    		
  }
}

Solution 2 - Java

This will do the trick:

  public void itemClicked(View v) {
	if (((CheckBox) v).isChecked()) {
		Toast.makeText(MyAndroidAppActivity.this,
	 	   "Checked", Toast.LENGTH_LONG).show();
	}
  }

Solution 3 - Java

<CheckBox
      android:id="@+id/checkBox1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Fees Paid Rs100:"
      android:textColor="#276ca4"
      android:checked="false"
      android:onClick="checkbox_clicked" />

> Main Activity from here

   public class RegistA extends Activity {
CheckBox fee_checkbox;
 @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_regist);
 fee_checkbox = (CheckBox)findViewById(R.id.checkBox1);// Fee Payment Check box
}

> checkbox clicked

     public void checkbox_clicked(View v)
     {
    	 
    	 if(fee_checkbox.isChecked())
         {
    		// true,do the task 
    		 
         }
         else
         {
        	 
         }
    	 
     }

Solution 4 - Java

You can try this code:

public void itemClicked(View v) {
 //code to check if this checkbox is checked!
 if(((CheckBox)v).isChecked()){
   // code inside if
 }
}

Solution 5 - Java

@BindView(R.id.checkbox_id) // if you are using Butterknife
CheckBox yourCheckBox;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_activity); 
    yourCheckBox = (CheckBox)findViewById(R.id.checkbox_id);// If your are not using Butterknife (the traditional way)

    yourCheckBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            yourObject.setYourProperty(yourCheckBox.isChecked()); //yourCheckBox.isChecked() is the method to know if the checkBox is checked
            Log.d(TAG, "onClick: yourCheckBox = " + yourObject.getYourProperty() );
        }
    });
}

Obviously you have to make your XML with the id of your checkbox :

<CheckBox
    android:id="@+id/checkbox_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Your label"
    />

So, the method to know if the check box is checked is : (CheckBox) yourCheckBox.isChecked() it returns true if the check box is checked.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionaBhijitView Question on Stackoverflow
Solution 1 - JavaAnandView Answer on Stackoverflow
Solution 2 - JavaSami EltamawyView Answer on Stackoverflow
Solution 3 - JavavbijoorView Answer on Stackoverflow
Solution 4 - JavaKishita VariyaView Answer on Stackoverflow
Solution 5 - JavaNicoolasensView Answer on Stackoverflow