Android Group Checkbox

I'm trying to apply some kind of check in a group of checkboxes (for example, two conflicting elements cannot be checked together), so I want to somehow group Check Box objects and apply something like RequiredFieldValidator for the whole group once and in which validator I I will register the listeners and conduct the entire verification of Check Boxes objects.

I think this will be code that looks like this:

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

Validate will contain the logic of conflicting flags and all.

Is it already implemented somewhere in Android? If not, has anyone tried something like this? (Hope share it with us here)

+9
source share
7 answers

This logic allows you to select one or more check boxes.

  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); } } }); 
+4
source

Here is what I did. Not sure if this will work for you, but this is what you can use as a start. My checkboxes I don’t need any checks, but you can add them to each if you want

 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; } } } 
+3
source

You can use the Radio Buttons buttons and customize them for different groups.

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

+2
source

Another tip.

This method can simply register the listener.

 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); } } 
+1
source

The best way to do a check 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; } } 
0
source

It’s logic to allow one of the two checkboxes if you want to select one of several, and 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); } } }); 
0
source
 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); } } }); 
0
source

All Articles