Radiobuttons, horizontal and vertical layout

Asked

Viewed 1,707 times

0

I have a layout with 6 Radiobuttons.

They are both horizontal and vertical.

How can I solve this problem? Since using 3 Radiogroups, I can’t uncheck the radio button outside the group. As such, I can score up to 3 Radiobuttons, but it should only be one marked.

How to resolve in layout or programmatically. Thank you

Follows 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>
  • Do you want a Radiogroup with 6 Radiobuttons arranged in 3 lines of 3? If yes see this reply

  • @ramaral would be this, I managed using several groups, but using several groups, I can not leave only 1 radiobutton marked, can be up to 3, need that only 1 is marked. I’ll see the answer

  • You are doing it the hard way, just use the margin features of each Radio Button and you get this effect ;)

3 answers

1


Solved as follows the Layout:

            <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;
        }
    }
}

0

Put all 6 inside a single radiogroup and in the xml point android:orientation="horizontal". This is essential. Their separation will be with the distance or margin between them, so that only two fits in each "line" of the interface.

-1

You have to leave all Radiobuttons inside a single Radiogroup and use a Viewgroup (Relativelayout, for example) to orgarnize the Radiobuttons the way you want them.

Ficaria algo assim:
<RadioGroup>
   <RelativeLayout>
       <RadioButton />
       <RadioButton />
       <RadioButton />
       <RadioButton />
       <RadioButton />
       <RadioButton />
   </RelativeLayout>
</RadioGroup>
  • Make sure it works?

  • You’re right, it doesn’t work. Although Radiogroup is a subclass of a Linearlayout, the behavior of Radiobuttons only works when they are direct children of Radiogroup. Another alternative I can think of is to forget about Radiogroup and treat the marking/unchecking of Radiobuttons in the same code. It’s not that complicated to do.

  • Try this library: https://android-arsenal.com/details/1/4992

  • The layout with linear layout, works. I will test here the coding, in Activity and place. If it works

Browser other questions tagged

You are not signed in. Login or sign up in order to post.