programmer one
programmer one

Reputation: 79

how to check if atleast one checkbox is checked android

This what I have done so far, even though I check all of them I get AlertDialog message.

  private void validateCheckBoxes() {

    if (toilets.isSelected() || wifi.isSelected() || trolleys.isSelected() || lifts.isSelected()
            && ticketMachine.isSelected() || stepFree.isSelected()) {
        saveRecordsToDatabase();


    } else {
        AlertDialog.Builder facilitiesError = new AlertDialog.Builder(AddStation.this);
        facilitiesError.setTitle("Station Facilities are not selected");
        facilitiesError.setMessage("Please select at least one facility ");

        facilitiesError.setNegativeButton("OK", null);
        facilitiesError.create().show();

    }

}

Upvotes: 1

Views: 2600

Answers (2)

AndroidEx
AndroidEx

Reputation: 15824

By "selected" do you mean "checked"?

if (toilets.isChecked() || wifi.isChecked() || trolleys.isChecked() || lifts.isChecked()
    || ticketMachine.isChecked() || stepFree.isChecked()) {
    saveRecordsToDatabase();
}

Upvotes: 3

The Guest
The Guest

Reputation: 708

modify your code:

if (toilets.isSelected() || wifi.isSelected() || trolleys.isSelected() || lifts.isSelected()
        || ticketMachine.isSelected() || stepFree.isSelected()) {
    saveRecordsToDatabase();
}

Explanation: you are using all && operators in if statement, which means you get alert message only if all the check boxes are checked.

If you use all || (or) operator, it means you get alert message if at least one checkbox is checked.

Upvotes: 2

Related Questions