12

I'm trying to apply some kind of validation on a group of check boxes (e.g. Two contradictory items cannot be checked together) so I want to somehow group Check Box objects and apply something like RequiredFieldValidator on the whole group once and in that validator I will register listeners and do the whole check on my Check Boxes objects.

What I imagine would be a code that look like that:

CheckBoxView allMyCheckBoxes = new CheckBoxView(checkbox1,checkbox2,checkbox3); //varargs
validate(allMyCheckBoxes);

Validate will contain the logic of contradictory check boxes and everything.

Is that already implemented somewhere in Android? If not, anybody tried out something like that? (Hopefully share it with us here)

Mohamed Sobhy
  • 385
  • 1
  • 5
  • 17

8 Answers8

6

This logic will allow to select one and more checkbox

 private Checkbox day = (checkbox)findviewbyid(R.id.day);
 private Checkbox night =(checkbox)findviewbyid(R.id.night);

 day.setOnCheckedChangeListener(new OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (!night.isChecked() && !day.isChecked()) {
            night.setChecked(true);
        }

    }
});
night.setOnCheckedChangeListener(new OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (!day.isChecked() && !night.isChecked()) {
            day.setChecked(true);
        }

    }
});
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
4

You could use Radio Buttons and set up different groups of them.

This documentation and This tutorial should help you out if you find that Radio Buttons are the way to go.

Barak
  • 16,318
  • 9
  • 52
  • 84
  • Thanks for the answer but while I know Radio Buttons are a way around that but the idea that I'm thinking about is separating the validation from the development process, so whatever someone write in the Activity my Validation Class should handle it with no problem. Hope you understood what I mean. – Mohamed Sobhy Apr 14 '12 at 09:34
  • Radio buttons don't support mutliple selections as far as I know – aclowkay Jan 14 '17 at 18:11
3

Here is what I did. Not sure if this will work for you but its something you could use as a start. My check boxes I don't need any on check listeners, but you could add them to each one if you'd like

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private CheckBox chk1, chk2, chk3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    chk1 = (CheckBox) findViewById(R.id.checkbox1);
    chk2 = (CheckBox) findViewById(R.id.checkbox2);
    chk3 = (CheckBox) findViewById(R.id.checkbox3);
    if (chk1.isChecked()) {
        chk1.setChecked(false);
    }
        else if(chk2.isChecked()){
          chk2.setChecked(false);
         }
    else {
        chk3.setChecked(false);
    }
    chk1.setOnClickListener(this);
    chk2.setOnClickListener(this);
    chk3.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
    switch(view.getId()){
        case R.id.checkbox1:
        chk1.setChecked(true);
        chk2.setChecked(false);
        chk3.setChecked(false);
        break;
        case R.id.checkbox2:
        chk2.setChecked(true);
        chk3.setChecked(false);
        chk1.setChecked(false);
        break;
        case R.id.checkbox3:
        chk3.setChecked(true);
        chk2.setChecked(false);
        chk1.setChecked(false);
        break;
    }
    }
}
Guillaume Barré
  • 4,168
  • 2
  • 27
  • 50
Konrad Winkowski
  • 2,022
  • 1
  • 13
  • 15
3

Another Tip.

This way can register listener simply.

private CheckBox[] chkBoxs;
private Integer[] chkBoxIds = {R.id.a, R.id.b, R.id.c .....};

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    chkBoxs = new CheckBox[chkBoxIds.length];
    for(int i = 0; i < chkBoxIds.length; i++) {
        chkBoxs[i] = (CheckBox) findViewById(chkBoxIds[i]);
        chkBoxs[i].setOnCheckedChangeListener(this);
    }

}
0

Best way to do validation on checkboxes similar to radio Buttons

 // Implements your activity     
 CompoundButton.OnCheckedChangeListener 
 // Declare global variables
private CheckBox cbDriver, cbPickDrop;
private String strCB;

// Bind components and set listeners in onCreate()
cbDriver = (CheckBox) findViewById(R.id.driverCB);
cbPickDrop = (CheckBox) findViewById(R.id.pickDropCB);
cbDriver.setOnCheckedChangeListener(this);
cbPickDrop.setOnCheckedChangeListener(this);


// write this method inside your activity
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    switch (buttonView.getId()){
        case R.id.driverCB:
            if (isChecked) {
                cbPickDrop.setChecked(false);
                strCB = cbDriver.getText().toString();
                DialogManager.showToast(MyActivity.this, strCB);
            }
            break;
        case R.id.pickDropCB:
            if (isChecked) {
                cbDriver.setChecked(false);
                strCB = cbPickDrop.getText().toString();
                DialogManager.showToast(MyActivity.this, strCB);
            }
            break;
    }

}
Tara
  • 2,598
  • 1
  • 21
  • 30
0

This is logic to allow single checkbox from two , if you want to select one from multiple then change it.

checkBoxHightoLow.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (checkBoxLowToHigh.isChecked()) {
                checkBoxLowToHigh.setChecked(false);
                checkBoxHightoLow.setChecked(isChecked);
            } else if (!checkBoxLowToHigh.isChecked()) {
                checkBoxHightoLow.setChecked(isChecked);

            }
        }
    });
    checkBoxLowToHigh.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (checkBoxHightoLow.isChecked()) {
                checkBoxHightoLow.setChecked(false);
                checkBoxLowToHigh.setChecked(isChecked);
            } else if (!checkBoxHightoLow.isChecked()) {
                checkBoxLowToHigh.setChecked(isChecked);

            }
        }
    });
Tarun Pal
  • 24
  • 1
  • 4
0
binding.overPhone.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    binding.overPhone.setChecked(true);
                    binding.overMail.setChecked(false);
                }else{
                    binding.overPhone.setChecked(false);
                    binding.overMail.setChecked(true);
                }
            }
        });
        binding.overMail.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    binding.overPhone.setChecked(false);
                    binding.overMail.setChecked(true);
                }else{
                    binding.overPhone.setChecked(true);
                    binding.overMail.setChecked(false);
                }
            }
        });
Manoj Behera
  • 2,736
  • 22
  • 13
0

Using the ButterKnife library:

@BindViews({R.id.checkbox_1, R.id.checkbox_2, R.id.checkbox_3, R.id.checkbox_4})
List<CheckBox> checkBoxes;
private void setChecked(int id){
        for(int i = 0; i < checkBoxes.size(); i++){
            if(checkBoxes.get(i).getId() != id){
                checkBoxes.get(i).setChecked(false);
            }else{
                checkBoxes.get(i).setChecked(true);
            }
        }
    }

@OnClick({R.id.checkbox_1, R.id.checkbox_2, R.id.checkbox_3, R.id.checkbox_4})
    void onCheckBoxClick(View view){
        switch (view.getId()){
            case R.id.checkbox_1:
                uncheckOthers(view.getId());
                break;

            case R.id.checkbox_2:
                uncheckOthers(view.getId());
                break;

            case R.id.checkbox_3:
                uncheckOthers(view.getId());
                break;

            case R.id.checkbox_4:
                uncheckOthers(view.getId());
                break;
        }
    }
DIRTY DAVE
  • 2,523
  • 2
  • 20
  • 83