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

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


try this one :

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


This will do the trick:

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


<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         {         }     }