Android:placing the radio buttons horizontally
Asked Answered
O

4

78

Below is my Relative layout..i am trying to place the radio buttons above the edittext

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFF" >

    <ImageView
        android:id="@+id/fbreplycancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/fbcancel" />

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="80dp"
        android:layout_height="250dp"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/fbcancel" >

        <Spinner
            android:id="@+id/replyspinner"
            android:layout_width="50dp"
            android:layout_height="30dp"
            android:layout_alignLeft="@+id/fbshare"
            android:layout_alignRight="@+id/fbshare"
            android:layout_below="@+id/fbshare"
            android:layout_marginTop="16dp"
            android:drawSelectorOnTop="true"
            android:entries="@array/fbcommentlist"
            android:visibility="gone" />

        <Button
            android:id="@+id/fbshare"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:background="@drawable/fbbuttons"
            android:text="@string/share" />

        <ImageView
            android:id="@+id/fbpeople"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/fbshare"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="19dp"
            android:background="@drawable/people2"
            android:drawSelectorOnTop="true"
            android:paddingTop="20dp" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:text="RadioButton" />
    </RelativeLayout>

    <View
        android:layout_width="250dp"
        android:layout_height="0.7dip"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/imageView1"
        android:background="#3b5998" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/fbcancel"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:paddingRight="2dp"
        android:src="@drawable/askabud" />

    <TextView
        android:id="@+id/fbcommentpostedby"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/fbedittext"
        android:layout_alignRight="@+id/fbcommentdisplay"
        android:layout_below="@+id/imageView1"
        android:layout_marginTop="15dp"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/fbcommentdisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/fbedittext"
        android:layout_below="@+id/fbcommentpostedby"
        android:layout_toLeftOf="@+id/relativeLayout1"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/fbtextview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView1"
        android:layout_toLeftOf="@+id/fbreplycancel"
        android:layout_toRightOf="@+id/imageView1"
         android:text="@string/replyrecommend" 
          android:textSize="18sp"
          android:textColor="#000000"/>

    <TextView
        android:id="@+id/fbplacename"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/fbcommentdisplay"
        android:layout_alignRight="@+id/fbcommentdisplay"
        android:layout_below="@+id/fbcommentdisplay"
        android:textColor="#000000" />

    <EditText
        android:id="@+id/fbedittext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/fbplacename"
        android:layout_marginLeft="19dp"
        android:layout_marginTop="45dp"
        android:layout_toLeftOf="@+id/relativeLayout1"
        android:background="@drawable/roundcorners"
        android:ems="10"
        android:hint="@string/fbhint"
        android:lines="6"
        android:scrollHorizontally="true"
        android:textSize="14sp"
        android:windowSoftInputMode="stateHidden" />

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/fbplacename" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:checked="true"
            android:text="1"
             />
    </RadioGroup>

    <RadioGroup
        android:id="@+id/radioGroup2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/radioGroup1"
        android:layout_toLeftOf="@+id/relativeLayout1" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="26dp"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="3" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton" />
    </RadioGroup>

    <RadioButton
        android:id="@+id/radio1"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignTop="@+id/radioGroup2"
        android:layout_toLeftOf="@+id/radioGroup2"
        android:text="2" />

</RelativeLayout>

Iam facing a hard time to arrange them horizontally.Any help is appreciated.

Oid answered 21/2, 2013 at 15:6 Comment(2)
orientation="horizontal" in radiogroup ?Ecto
how do i give equal spacing between themOid
M
177

To place a radiogroup (or any other view) above other just do:

android:layout_above="@+id/view_below"

To change the orientation just set:

android:orientation="horizontal"

And to give equal width to items make use of layout_weight:

<RadioGroup
    android:id="@+id/radio_group"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_above="@+id/view_below" >

    <RadioButton
        android:id="@+id/radio1"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="First" />

    <RadioButton
        android:id="@+id/radio2"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="Second" />
</RadioGroup>
Monitory answered 21/2, 2013 at 15:17 Comment(6)
I'll edit the answer and put the code all together. Check if is the same as yours.Euclid
@Sergio...thnk you..perfectOid
Is the layout_alignParentLeft="true" strictly necessary? I haven't been able to get this to work, but am also working from the context of a cross-platform Xamarin app where things might be idfferent.Skintight
No @FrankSchwieterman, that property is not necessary and I've removed it, thanks. As for your problem, what are you getting? Vertical radiobuttons or bad distributed width values? Like you say, that may be a problem with Xamarin's capabilitiesEuclid
The result I'm getting is that the buttons are not stretching to fill the parent space. Are there any requirements for the container I might check? Thank you.Skintight
The weightSum is implicitly set to 2, you can try declare it in the container and see if it works. But the value will depend on how much buttons you have, this case is 2.Euclid
G
6

You can use table layout instead of relative layout. Insert row into into table layout and place radio buttons inside table row..

For equal spacing follow following procedure

<TableRow
        android:id="@+id/tableRow2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp" >

        <RadioButton
            android:id="@+id/button2"
            android:layout_width="5dp"
            android:layout_height="60dp"
            android:layout_weight="1"
            android:text="@string/btnReject"
            android:onClick="onCallRejectButton" />

        <RadioButton
            android:id="@+id/button1"
            android:layout_width="5dp"
            android:layout_height="60dp"
            android:layout_weight="1
            android:onClick="onCallAcceptButton"
            android:text="@string/btnAccept" />

    </TableRow>
Galla answered 21/2, 2013 at 15:22 Comment(1)
It's not good Idea because user can select both of them and can not deselect them. By radio group user can select just one of them and by select new radio button, old selected radio button will be deselect.Quintero
L
1

android:orientation="horizontal" just do this. Type the orientation horizontal property to the radiogroup tag

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

    <RadioButton
        android:id="@+id/radioMale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Male"
        android:checked="true" />

    <RadioButton
        android:id="@+id/radioFemale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Female" />

</RadioGroup>
Lithoid answered 16/2, 2021 at 15:51 Comment(0)
C
0

You just need to put Radio Group orientation to horizontal.

 <RadioGroup                                    
android:id="@+id/radio_group_forgot_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
**android:orientation="horizontal"**                               
>
Compulsory answered 8/6, 2021 at 10:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.