CheckBox checkbox = findViewById(R.id.checkbox); checkbox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (((CheckBox) v).isChecked()) { // Checkbox is checked } else { // Checkbox is not checked } } });
import android.widget.CompoundButton; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener { private CheckBox checkbox; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); checkbox = findViewById(R.id.checkbox); checkbox.setOnCheckedChangeListener(this); } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { Toast.makeText(this, "Checkbox is checked", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "Checkbox is not checked", Toast.LENGTH_SHORT).show(); } } }In this example, we set the activity as the listener for the checkbox's onCheckedChanged event. When the checkbox is checked or unchecked, the onCheckedChanged() method will be called. We display a toast message to indicate if the checkbox is checked or not. Library package: android.widget