Ales
Ales

Reputation: 537

Align radio buttons horizontally and vertically in the same radiogroup

I would like to have radio buttons in one radiogroup divided into 2 columns and several rows.

So far, I have been able to divide them by putting LinearLayouts inside the radiogroup. The problem is that they are not exclusive when I click on them (I can check all of them at the same time). Is there a way to make them exclusive (besides a programmatic way)?

Upvotes: 7

Views: 20175

Answers (3)

Sk Saad Al Mahmud
Sk Saad Al Mahmud

Reputation: 3019

Follow below steps:

  1. Create Horizontal RadioGroup for RadioButtons in same line/row
  2. If you need 2 rows of RadioButtons, follow step 1 two times
  3. Now use below approach in java code, (assuming 2 rows of RadioButton means 2 RadioGroups)

.

private void initView(){
    radioGroup.setOnCheckedChangeListener(radioGroup1CheckedChangeListener);

    radioGroup2.setOnCheckedChangeListener(radioGroup2CheckedChangeListener);
}

RadioGroup.OnCheckedChangeListener radioGroup1CheckedChangeListener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            switch (i) {
                case R.id.rb1:
                    radioGroup2.setOnCheckedChangeListener(null);
                    radioGroup2.clearCheck();               
radioGroup2.setOnCheckedChangeListener(rgPaymentMethods2CheckedChangeListener);
                    break;
                // more radiobutton id with same approach
            }
        }
    };

RadioGroup.OnCheckedChangeListener radioGroup2CheckedChangeListener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            switch (i) {
                case R.id.rb2:
                    radioGroup1.setOnCheckedChangeListener(null);
                    radioGroup1.clearCheck();
                    radioGroup1.setOnCheckedChangeListener(rgPaymentMethodsCheckedChangeListener);
                    break;
                // more radiobutton id with same approach
            }
        }
    };

Upvotes: 0

josephus
josephus

Reputation: 8304

If by "programmatic way" you mean you want to control these radio buttons using only XML, then no. There is no solution.

Doing it "programmatically" should be pretty easy.

Upvotes: -6

Joseph Selvaraj
Joseph Selvaraj

Reputation: 2237

I came to this page with a slightly different question. I want to keep all my radio buttons horizontal. May this this will be helpful to someone. Setting the orientation will take care of placing all the radio buttons in a single row.

<RadioGroup
        android:id="@+id/commuteby"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@+id/line0" >

Note: if the components cross the screen then also it will place the components in same row. So some of your columns may be hidden in small screen phones. But you can create another layout file for small screens.

Upvotes: 24

Related Questions