Liondancer Liondancer - 5 months ago 16
Java Question

TextView and RadioGroup alignment

I am having trouble getting my

TextView
and
RadioGroup
in the right position.

I want the
TextView
to have its own row and the
RadioGroup
to have its own as well. Perhaps also make it easily adjustable so that I can change the spacing between the two.

The
TextView
and
RadioGroup
are currently on top of each other.

enter image description here

xml:

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/dueDateSection"
android:id="@+id/relativeLayout2"
android:paddingBottom="@dimen/activity_edit_item_layout_vertical_margin"
android:paddingTop="@dimen/activity_edit_item_layout_vertical_margin"
android:gravity="center_vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/tvPriorityLabel"
android:textSize="22sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:layout_alignTop="@id/priorityChoice"/>

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

<RadioButton
android:id="@+id/lowPriority"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/priorityLow"/>
<RadioButton
android:id="@+id/medPriority"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/priorityMedium" />
<RadioButton
android:id="@+id/highPriority"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/priorityHigh" />
</RadioGroup>
</RelativeLayout>

Answer

You need to align your RadioGroup under your TextView. You should add the id attribute to the TextView and then set the RadioGroup to be below it:

    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/tvPriorityLabel"
        android:textSize="22sp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_alignTop="@id/priorityChoice"/>

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

Also note that you do not need the android:gravity attribute for positioning individual views.

Make sure to checkout the docs on RelativeLayouts and LinearLayouts.

Comments