lundi 11 octobre 2021

Adding condition for checkbox in Android studio

I am making an ordering system and I want the user to be able to choose one or more food from the menu with checkbox. When the btnCheckout is clicked, it will proceed to another activity. My code requires all checkbox to be checked to proceed but I want it to be able to choose 1 or more, not all.

This is what I have done so far

public void btnCheckout(View view) {
boolean valid = true;

   if (!getChkPlain.isChecked() && !getChkCheese.isChecked() && !getChkSpecial.isChecked()  && !getChkMonster.isChecked() && !getChkMonolith.isChecked())
      Toast.makeText(getApplicationContext(), "Nothing selected", Toast.LENGTH_SHORT).show();
   else {
      String qtyPlain = getQtyPlainB.getText().toString();
      String qtyCheese = getQtyCheeseB.getText().toString();
      String qtySpecial = getQtySpecialB.getText().toString();
      String qtyMonster = getQtyMonsterB.getText().toString();
      String qtyMonolith = getQtyMonolithB.getText().toString();

      if (getQtyPlainB.getText().length() == 0){
         getQtyPlainB.setError("Qty cannot be empty");
         valid = false;
      }

      if (getQtyCheeseB.getText().length() == 0){
         getQtyCheeseB.setError("Qty cannot be empty");
         valid = false;
      }

      if (getQtySpecialB.getText().length() == 0){
         getQtySpecialB.setError("Qty cannot be empty");
         valid = false;
      }

      if (getQtyMonsterB.getText().length() == 0) {
         getQtyMonsterB.setError("Qty cannot be empty");
         valid = false;
      }

      if (getQtyMonolithB.getText().length() == 0) {
         getQtyMonolithB.setError("Qty cannot be empty");
         valid = false;
      }

      if (valid) {
         //proceed with code
      }
}

Sample output

enter image description here




Aucun commentaire:

Enregistrer un commentaire