-2

How to implement more into this code so that it returns a message telling the user to 'select at least one checkbox' if he/ she does not select any?

public void verificaCheckBox() {

    Listcheck.clear();

    if (cbPapel.isChecked())
        Listcheck.add(cbPapel.getText().toString());

    if (cbPlastico.isChecked())
        Listcheck.add(cbPlastico.getText().toString());

    if (cbMetal.isChecked())
        Listcheck.add(cbMetal.getText().toString());

    if (cbVidro.isChecked())
        Listcheck.add(cbVidro.getText().toString());

    cbSelecionado = (Listcheck.toString());
}
juanjo
  • 3,737
  • 3
  • 39
  • 44
E FRANÇA
  • 3
  • 1

3 Answers3

0
public String verificaCheckBox(){

Listcheck.clear();

if (cbPapel.isChecked())
    Listcheck.add(cbPapel.getText().toString());

if (cbPlastico.isChecked())
    Listcheck.add(cbPlastico.getText().toString());

if (cbMetal.isChecked())
    Listcheck.add(cbMetal.getText().toString());

if (cbVidro.isChecked())
    Listcheck.add(cbVidro.getText().toString());


    cbSelecionado = (Listcheck.toString());

return Listcheck.isEmpty() ? "Message goes here" : "";

}

Then wherever calls it can check to see if the return is an empty string or the message. If you just want to show the user a message you could do this:

public void verificaCheckBox(){

Listcheck.clear();

if (cbPapel.isChecked())
    Listcheck.add(cbPapel.getText().toString());

if (cbPlastico.isChecked())
    Listcheck.add(cbPlastico.getText().toString());

if (cbMetal.isChecked())
    Listcheck.add(cbMetal.getText().toString());

if (cbVidro.isChecked())
    Listcheck.add(cbVidro.getText().toString());


    cbSelecionado = (Listcheck.toString());

if(Listcheck.isEmpty()) {
    Toast.makeText(applicationContext, "Your message", Toast.LENGTH_SHORT).show();
}


}
Bakon Jarser
  • 703
  • 6
  • 20
0

Try using an Alert Dialog to accomplish that. I believe that the following link gives more relevant details on how to do so: How to add message box with ok button

To put it for you in simpler steps:

  • Add an else-statement at the end of your code.
  • Use the Alert Dialog to notify the user of the changes needed to be done.
A. Yassin
  • 75
  • 1
  • 2
  • 10
0

I am not sure that I have understood your question, but if you want to send message to the user you can use a Toast

For example:

if (Listcheck.size() == 0)
   Toast.makeText(this, "select at least one checkbox", Toast.LENGTH_SHORT).show();
yotam ravits
  • 181
  • 10