RadioButtons, horizontal and vertical layout

0

I have a layout with 6 RadioButtons.

They are both horizontally and vertically.

How can I solve this problem? Since using 3 RadioGroups, I can not unmark the radiobutton that is outside the group. So I can dial up to 3 RadioButtons, but it should be just a dial-up.

How to solve in layout or programmatically. Thanks

Following layout:

        <RadioGroup
            android:id="@+id/radiogroup_map1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/rb_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:buttonTint="@color/branco"
                android:text="Case 1"
                android:textColor="@color/branco" />

            <RadioButton
                android:id="@+id/rb_2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:buttonTint="@color/branco"
                android:text="Case 2"
                android:textColor="@color/branco" />

        </RadioGroup>

        <RadioGroup
            android:id="@+id/radiogroup_map2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/rb_3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:buttonTint="@color/branco"
                android:text="Case 3"
                android:textColor="@color/branco" />

            <RadioButton
                android:id="@+id/rb_4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:buttonTint="@color/branco"
                android:text="Case 4"
                android:textColor="@color/branco" />

        </RadioGroup>

        <RadioGroup
            android:id="@+id/radiogroup_map3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/rb_5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:buttonTint="@color/branco"
                android:text="Case 5"
                android:textColor="@color/branco" />

            <RadioButton
                android:id="@+id/rb_6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="35dp"
                android:buttonTint="@color/branco"
                android:text="Case 6"
                android:textColor="@color/branco" />

        </RadioGroup>
    
asked by anonymous 24.04.2017 / 02:23

2 answers

0

Resolved Layout as follows:

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:weightSum="3">

                    <RadioButton
                        android:id="@+id/rb_1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="@string/rb_rua_movimentada"
                        android:textSize="13dp"/>

                    <RadioButton
                        android:id="@+id/rb_2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="@string/rb_iluminacao"
                        android:textSize="13dp"
                        android:layout_marginRight="42dp"/>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:weightSum="3">

                    <RadioButton
                        android:id="@+id/rb_3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="@string/rb_comercio"
                        android:textSize="13dp"/>

                    <RadioButton
                        android:id="@+id/rb_4"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="@string/rb_assedio"
                        android:textSize="13dp"/>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:weightSum="3">

                    <RadioButton
                        android:id="@+id/rb_5"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="@string/rb_posto_policial"
                        android:textSize="13dp"/>

                    <RadioButton
                        android:id="@+id/rb_6"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="@string/rb_porteiro"
                        android:textSize="13dp"
                        android:layout_marginRight="25dp"/>
                </LinearLayout>

            </LinearLayout>
        </RadioGroup>

In the code:

implemented CompoundButton.OnCheckedChangeListener

onCreate
   rg1 = (RadioGroup) findViewById(R.id.radiogroup_Map1);

rb_1.setOnCheckedChangeListener(this);
rb_2.setOnCheckedChangeListener(this);
rb_3.setOnCheckedChangeListener(this);
rb_4.setOnCheckedChangeListener(this);
rb_5.setOnCheckedChangeListener(this);
rb_6.setOnCheckedChangeListener(this);

Method to change setChecked:

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (isChecked) {
        switch (buttonView.getId()) {
            case R.id.rb_1:
                rb_1.setChecked(true);
                rb_2.setChecked(false);
                rb_6.setChecked(false);
                rb_4.setChecked(false);
                rb_3.setChecked(false);
                rb_5.setChecked(false);
                break;
            case R.id.rb_2:
                rb_1.setChecked(false);
                rb_2.setChecked(true);
                rb_6.setChecked(false);
                rb_4.setChecked(false);
                rb_3.setChecked(false);
                rb_5.setChecked(false);
                break;
            case R.id.rb_3:
                rb_1.setChecked(false);
                rb_2.setChecked(false);
                rb_6.setChecked(false);
                rb_4.setChecked(false);
                rb_3.setChecked(true);
                rb_5.setChecked(false);
                break;
            case R.id.rb_4:
                rb_1.setChecked(false);
                rb_2.setChecked(false);
                rb_6.setChecked(false);
                rb_4.setChecked(true);
                rb_3.setChecked(false);
                rb_5.setChecked(false);
                break;
            case R.id.rb_5:
                rb_1.setChecked(false);
                rb_2.setChecked(false);
                rb_6.setChecked(false);
                rb_4.setChecked(false);
                rb_3.setChecked(false);
                rb_5.setChecked(true);
                break;
            case R.id.rb_6:
                rb_1.setChecked(false);
                rb_2.setChecked(false);
                rb_6.setChecked(true);
                rb_4.setChecked(false);
                rb_3.setChecked(false);
                rb_5.setChecked(false);
                break;
        }
    }
}
    
24.04.2017 / 23:56
-1

You have to leave all the RadioButtons within a single RadioGroup and use a ViewGroup (RelativeLayout, for example) to organize the RadioButtons the way you want.

Ficaria algo assim:
<RadioGroup>
   <RelativeLayout>
       <RadioButton />
       <RadioButton />
       <RadioButton />
       <RadioButton />
       <RadioButton />
       <RadioButton />
   </RelativeLayout>
</RadioGroup>
    
24.04.2017 / 19:55