Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RadioGroup with two columns which have ten RadioButtons

I have a RadioGroup and I want to align buttons next to each other in two columns and five rows and I am unable to achieve it. Things I have tried:

  1. RelativeLayout -> Outside RadioGroup -> Inside RadioGroup. All RadioButtons are selected, but I want only one to be selected.
  2. RadioGroup : orientation
  3. Span, stretchcolumns
  4. TableRow
  5. TableLayout

Please let me know how create one RadioGroup and have two columns and many RadioButtons within.

like image 533
Jason Wood Avatar asked May 03 '12 05:05

Jason Wood


2 Answers

You can simulate that RadioGroup to make it look like you have only one. For example you have rg1 and rg2(RadioGroups with orientation set to vertical(the two columns)). To setup those RadioGroups:

rg1 = (RadioGroup) findViewById(R.id.radioGroup1); rg2 = (RadioGroup) findViewById(R.id.radioGroup2); rg1.clearCheck(); // this is so we can start fresh, with no selection on both RadioGroups rg2.clearCheck(); rg1.setOnCheckedChangeListener(listener1); rg2.setOnCheckedChangeListener(listener2); 

To select only one RadioButton in those RadioGroups the listeners above will be:

private OnCheckedChangeListener listener1 = new OnCheckedChangeListener() {          @Override         public void onCheckedChanged(RadioGroup group, int checkedId) {             if (checkedId != -1) {                 rg2.setOnCheckedChangeListener(null); // remove the listener before clearing so we don't throw that stackoverflow exception(like Vladimir Volodin pointed out)                 rg2.clearCheck(); // clear the second RadioGroup!                 rg2.setOnCheckedChangeListener(listener2); //reset the listener                 Log.e("XXX2", "do the work");             }         }     };      private OnCheckedChangeListener listener2 = new OnCheckedChangeListener() {          @Override         public void onCheckedChanged(RadioGroup group, int checkedId) {             if (checkedId != -1) {                 rg1.setOnCheckedChangeListener(null);                 rg1.clearCheck();                 rg1.setOnCheckedChangeListener(listener1);                 Log.e("XXX2", "do the work");             }         }     }; 

To get the checked RadioButton from the RadioGroups you could do:

int chkId1 = rg1.getCheckedRadioButtonId(); int chkId2 = rg2.getCheckedRadioButtonId(); int realCheck = chkId1 == -1 ? chkId2 : chkId1; 

If you use the check() method of the RadioGroup you have to remember to call clearCheck() on the other Radiogroup.

like image 145
user Avatar answered Sep 17 '22 21:09

user


The RadioGroup is extended from LinearLayout.

the linearlayout can not do it, so RadioGroup can not do it.

Why not implement it self.

Use RelativeLayout to layout the child view. And record the state of the child view. use setLevel to control the states.

Good luck for you!.

like image 33
Changwei Yao Avatar answered Sep 17 '22 21:09

Changwei Yao