PeakGen
PeakGen

Reputation: 23025

Radio Buttons are aligned incorrectly

Please have a look at the following code

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".Form" >

        <RelativeLayout 
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >




            <TextView
               android:id="@+id/gender"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="Text To Be Displayed"
               android:layout_below="@+id/hipsTxt"
               /> 

            <RadioGroup 
                android:id="@+id/genderRadio"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                    android:layout_toRightOf="@+id/gender"
                    android:layout_alignBaseline="@+id/gender"
                >

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

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

            </RadioGroup>


        </RelativeLayout>

</ScrollView>

Here, I need the radio buttons to be displayed right next to the TextView. But instead, it is getting displayed below the textview!! What is wrong here? Please help!

PS: Android Version 2.3.3

Upvotes: 0

Views: 285

Answers (3)

Zoombie
Zoombie

Reputation: 3610

<?xml version="1.0" encoding="utf-8"?>

    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        > 

        <RadioGroup 
            android:id="@+id/genderRadio"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
                android:layout_toRightOf="@+id/gender"

            >

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

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

        </RadioGroup>

        <TextView
            android:id="@+id/gender"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:text="Text To Be Displayed" />

    </RelativeLayout>

Tried and changed your xml content, now it is properly aligned, try with this one.

Upvotes: 1

Pratik Sharma
Pratik Sharma

Reputation: 13415

Try this :

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Form" >

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

        <TextView
            android:id="@+id/gender"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:text="Text To Be Displayed" />

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

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

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

</ScrollView>

Upvotes: 1

Kumar Bibek
Kumar Bibek

Reputation: 9117

Instead of

android:layout_alignBaseline="@+id/gender"

Use

android:layout_alignParentTop="true"

Upvotes: 1

Related Questions