1

I have 7 radio buttons under one radio group. I rendered them horizontally using orientation = "horizontal". But only 4 can be visible at a time, rest are not.

Is there any way that I could show rest of them in second row while keeping all these 'Radiobuttons' in one 'RadioGroup`?

In case you need my code -

    <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="1 BHK" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="1.5 BHK" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2 BHK" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2.5 BHK" />
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2 BHK" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2.5 BHK" />
</RadioGroup>
Darpan
  • 5,623
  • 3
  • 48
  • 80
  • Why do you want to keep them all in one RadioGroup (within the layout file)? You can easily implement a radio group in source, i.e., keep a list of all RadioButtons of the group and set listeners which disable all other RadioButtons in the group when one button is checked. – cygery Apr 25 '15 at 18:22
  • Well that sounds like a solution, but not a clean one. I thought there has to be some solution to cater my need. – Darpan Apr 27 '15 at 03:08
  • Possible duplicate https://stackoverflow.com/questions/29554629/show-many-radio-buttons/44231154#44231154 – Hasina Sep 29 '18 at 16:36
  • @Hasina you have marked this question duplicate with this same question's link :) – Darpan Oct 09 '18 at 11:47

7 Answers7

1

You can't place RadioButtons at different places on the screen while keeping them in the same RadioGroup. You can't keep them in different rows either (like you want to achieve). Yes, I hate it too.

You can make checkboxes intead, place them the way you want, and then use setOnCheckedChangedListener on each of them. So if one of them is checked, call setChecked(false) on the others.

Use custom drawable so that they look like radio buttons and not checkboxes.

Kartik
  • 7,677
  • 4
  • 28
  • 50
  • Don't want to keep them in at different places, they are not fitting in a row, so wanted to keep them in second row, more like 'text reflow' thing. – Darpan Apr 10 '15 at 06:24
  • I understand that. Your case is a sub-case of what I said above. Having two different orientations in the same radio group is the same as placing them at different locations. – Kartik Apr 10 '15 at 06:26
  • i also try this but u can easily shown on screen without any RadioGroup, just simply drag the radio buttons – Sanjeev Sangral Apr 10 '15 at 07:01
0

You can use scrollview inside a radio group like this

                 <RadioGroup
                   android:layout_width="match_parent"
                   android:layout_height="wrap_content"
                   android:orientation="horizontal">
                <HorizontalScrollView
                   android:id="@+id/ScrlView"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content" >

                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1 BHK" />

                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1.5 BHK" />

                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="2 BHK" />

                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="2.5 BHK" />
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="2 BHK" />

                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="2.5 BHK" />
                     </ScrollView>
</RadioGroup>
Salmaan
  • 3,543
  • 8
  • 33
  • 59
  • It will make it scrollable, but still views will not be visible to user, so will not be a intuitive design. – Darpan Apr 10 '15 at 06:25
  • I think you can also use gridview instead of scrollview – Salmaan Apr 10 '15 at 06:26
  • Well @Darpan this also might not be a good approach, as android devices screen sizes vary from 3.5" to 45" so i will strongly discourage usage of radiobuttons horizontally, you never know will 4 options always fit in horizontally.. You must use vertical radio group. HIGHLY RECOMMENDED – Salmaan Apr 10 '15 at 06:37
0

I guess, there is not enough place for all of them. Try to set android:layout_width="0dp" and android:layout_weight="1" to all RadioButtons. But it can be not very beautiful.

Ircover
  • 2,406
  • 2
  • 22
  • 42
0

Try to set width for all Radiobutton

 android:layout_width="0dp"

And also add android:layout_weight="1" for every Radio Button.

ex

 <RadioButton
                 android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="1 BHK" 
android:layout_weight="1"/>
Amsheer
  • 7,046
  • 8
  • 47
  • 81
0

Replace below code from your xml code

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

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

        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="1 BHK" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="1.5 BHK" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2 BHK" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2.5 BHK" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2 BHK" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2.5 BHK" />
        </RadioGroup>
    </LinearLayout>
</HorizontalScrollView>
0

i also try this within RadioGroup and you can select only one at a time it works nice : enter image description here

<RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton android:layout_height="match_parent" >
        </RadioButton>

        <RadioButton android:layout_height="match_parent" >
        </RadioButton>

        <RadioButton android:layout_height="match_parent" >
        </RadioButton>

        <RadioButton android:layout_height="match_parent" >
        </RadioButton>

        <RadioButton android:layout_height="match_parent" >
        </RadioButton>

        <RadioButton android:layout_height="match_parent" >
        </RadioButton>

        <RadioButton android:layout_height="match_parent" >
        </RadioButton>
    </RadioGroup>
Sanjeev Sangral
  • 1,385
  • 2
  • 25
  • 37
-1

My solution to this is to add a LinearLayout inside your RadioGroup layout. I changed the orientation of the RadioGroup to vertical and added two LinearLayout with horizontal orientation which will server as rows.

<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"        
    >

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

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1 BHK" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1.5 BHK" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2 BHK" />

    </LinearLayout>


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

         <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2.5 BHK" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="3 BHK" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="3.5 BHK" />

    </LinearLayout>
</RadioGroup>
Christian Abella
  • 5,747
  • 2
  • 30
  • 42
  • 2
    This way you can select all the radio buttons at once! This is not what radio buttons are meant for. – Kartik Apr 10 '15 at 07:16