Daisy
Daisy

Reputation: 527

toast msg went crazy for my checkbox validation

I want to create a validation for the checkbox. For example, if the provider does not check one of the checkboxes, it will issue a Toast msg, "Please tick at least one of your selections."

My interface:-

enter image description here

My coding for validation method:-

else if(!(cbBM.isChecked()) || !(cbBI.isChecked()) || !(cbMath.isChecked()) || !(cbSc.isChecked()))
    {
        Toast.makeText(getActivity(), "Please tick at least one of your selections.", Toast.LENGTH_SHORT).show();
    }

My coding for checkbox:-

cbBM = view.findViewById(R.id.bm);
    cbBM.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton buttonView,
                                     boolean isChecked)
        {
            if (isChecked) {
                //DO NOTHING
            }

            else {
                Toast.makeText(getActivity(), "Please tick at least one of your selections.", Toast.LENGTH_SHORT).show();
            }
        }
    });

The problem I faced was, the toast message always display if I did not tick the "Bahasa Melayu" option.

Upvotes: 1

Views: 317

Answers (2)

navylover
navylover

Reputation: 13619

Notice when first express is true, the remain expressions are not checked. so if Bahasa Melayu is not ticked ,below logic express is always equal true, so toast message always displays.

 !(cbBM.isChecked()) || !(cbBI.isChecked()) || !(cbMath.isChecked()) || !(cbSc.isChecked())

You could try this:

  private boolean showHint = true;
  if(cbBM.isChecked()) showHint = false;
  if(cbBI.isChecked()) showHint = false;
  if(cbMath.isChecked()) showHint = false;
  if(cbSc.isChecked()) showHint = false;
  if(showHint == true){
     Toast.makeText(getActivity(), "Please tick at least one of your selections.", Toast.LENGTH_SHORT).show();
  }

Edited: add above codes in other place, may be in submit button click listener, and change showHint to class field.

Upvotes: 0

forpas
forpas

Reputation: 164204

Use the && (logical AND) operator instead of || (logical OR).
With the || operator if any of the checkboxes is unchecked you will see the toast.
Try this:

if(!cbBM.isChecked() && !cbBI.isChecked() && !cbMath.isChecked() && !cbSc.isChecked())

if you want to use the listener, modify it like this:

cbBM = view.findViewById(R.id.bm);
    cbBM.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton buttonView,
                                     boolean isChecked)
        {
            checkNoSelection()
        }
    });

and create 3 more listeners for the other 3 checkboxes like the one above.

This is the checkNoSelection() method:

private void checkNoSelection() {
    if(!cbBM.isChecked() && !cbBI.isChecked() && !cbMath.isChecked() && !cbSc.isChecked()) {
           Toast.makeText(getActivity(), "Please tick at least one of your selections.", Toast.LENGTH_SHORT).show();
    }
}

Upvotes: 1

Related Questions