030
030

Reputation: 11669

How to align text left to RadioGroup in Android?

Although this Q&A has been checked, the text cannot be aligned left to a RadioGroup in Android.

Current Outcome

enter image description here

Expected Outcome

enter image description here

Code

C:\workspace-adt\Helloworld\res\layout\activity_main.xml

<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.helloworld.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:text="@string/fractions" />

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

        <RadioButton
            android:id="@+id/fraction_true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:paddingRight="15dip"
            android:text="@string/fraction_true" />

        <RadioButton
            android:id="@+id/fraction_false"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingRight="15dip"
            android:text="@string/fraction_false" />
    </RadioGroup>

</RelativeLayout>

C:\workspace-adt\Helloworld\res\values\strings.xml

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

    <string name="app_name">Helloworld</string>
    <string name="fractions">Fractions</string>
    <string name="action_settings">Settings</string>
    <string name="fraction_true">True</string>
    <string name="fraction_false">False</string>

</resources>

Upvotes: 0

Views: 96

Answers (2)

Sajad Karuthedath
Sajad Karuthedath

Reputation: 15767

use Linear Layouts with weight.which will make it simple

check docs

Layouts

or with just LinearLayout

<LinearLayout
           android:id="@+id/linearLayout5" 
           android:layout_width= "fill_parent"
           android:orientation="horizontal"
           android:layout_gravity="center"
          >
 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:text="@string/fractions" />
           <RadioGroup
               android:id="@+id/radioGroup1"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:orientation="horizontal" >

               <RadioButton
        android:id="@+id/fraction_true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:paddingRight="15dip"
        android:gravity="center"
        android:layout_gravity="center"
        android:text="lalalal" />

    <RadioButton
        android:id="@+id/fraction_false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingRight="15dip"
        android:gravity="center" 
        android:layout_gravity="center"
        android:text="lalalal" />

           </RadioGroup>



        </LinearLayout> 

Upvotes: 1

FeleMed
FeleMed

Reputation: 621

Seems like a case for an horizontal linear layout.

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:text="lalalal" />

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

        <RadioButton
            android:id="@+id/fraction_true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:paddingRight="15dip"
            android:text="lalalal" />

        <RadioButton
            android:id="@+id/fraction_false"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingRight="15dip"
            android:text="lalalal" />
    </RadioGroup>

</LinearLayout>

Upvotes: 2

Related Questions