user2357839
user2357839

Reputation:

How to verify that a checkbox is just about to be unchecked?

In Android, is this code rith to verify that the checkbox is clicked to be unchecked ?

      public void onClick(View v) {

        if (((CheckBox) v).isChecked()) {
            Toast.makeText(IdentifyActivity.this, "clicked to check", Toast.LENGTH_LONG);
        }
        else{
            Toast.makeText(IdentifyActivity.this, "clicked to uncheck", Toast.LENGTH_LONG);
        }

Upvotes: 0

Views: 44

Answers (3)

Basim Sherif
Basim Sherif

Reputation: 5440

    checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

   @Override
   public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {

        if(isChecked==true)
         {
           //your checkbox is checked
          }
        else
         {
            //your checkbox is not checked
          }

   }
}

Upvotes: 0

Krauxe
Krauxe

Reputation: 6058

CheckBox cb = (CheckBox) findViewById(R.id.myCheckBox);

cb.setOnCheckedChangeListener(new OnCheckedChangeListener(){
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            // It's checked
        } else {
            // It's not checked
        }
    }
});

Upvotes: 0

Blackbelt
Blackbelt

Reputation: 157437

you can use OnCheckedChangeListener. you have to implement the callback

 onCheckedChanged(CompoundButton buttonView, boolean isChecked)

that's the way your task is usualy done. Your code works as well and, probably, between both approces there is not a real difference

Upvotes: 3

Related Questions