-1

How I know CheckBox is clicked or not clicked? Thank all

xml file:

<CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckBox" />

java file:

public class Setting extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

}
}
m.asadi
  • 47
  • 2
  • 13

4 Answers4

0
//declaring the checked variable at the top
checked=false;
if(checkbox.isChecked())
 {
 checked=true;
 }
 else
 {
 checked=false;
 }
Javasamurai
  • 666
  • 7
  • 21
0

Try this

CheckBox cb = (CheckBox)findViewById(R.id.checkBox);
if (cb.isChecked()) {
    // code to run when checked
} else {
    // code to run when unchecked
}
ColdFire
  • 6,764
  • 6
  • 35
  • 51
0
try this hope it will help you:   
checkbox    mchk1=(CheckBox)findViewById(R.id.chk11);
Boolean cBox1;

SharedPreferences pref =   getApplicationContext().getSharedPreferences("MyPref", 0); 

        cBox1=pref.getBoolean("check1", false);

 if(cBox1==true){
            mchk1.setChecked(true);
            //Rb1.setChecked(true);
        }

SharedPreferences pref =   getApplicationContext().getSharedPreferences("MyPref", 0); 

                SharedPreferences.Editor editor = pref.edit();

                if(mchk1.isChecked() ){

                    editor.putBoolean("check1", true);
                }
 else if(!mchk1.isChecked()  )
{
                    editor.putBoolean("check1", false);
                }
editor.commit();
MurugananthamS
  • 2,395
  • 4
  • 20
  • 49
0

Make changes in your activity as below,

public class Setting extends ActionBarActivity implements CompoundButton.OnCheckedChangeListener {

CheckBox myCheckBox;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_setting);

    myCheckBox = findViewById(R.id.checkBox);
    myCheckBox.setOnCheckedChangeListener(this);
}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if(isChecked){
        //here the checkBox is checked
    }else {
        //here the checkBox is not checked
    }        
}
}

Let me know if it works...

Rohit Jagtap
  • 1,660
  • 1
  • 14
  • 12