Michal
Michal

Reputation: 15669

RadioButtons with TextView in RadioGroup

Is it possible to add TextView below RadioButton in RadioGroup in any other way than extending and creating my custom RadioButton?

Upvotes: 9

Views: 10391

Answers (3)

Ali Behzadian Nejad
Ali Behzadian Nejad

Reputation: 9044

I think that you can use another tricky solution. Create a gap between two radio buttons with padding or margin and then put textView on the gap.

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

<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="78dp" >

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

    <RadioButton
        android:id="@+id/radio1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" 
        android:layout_marginBottom="100dp"/>

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

</RadioGroup>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/radioGroup1"
    android:layout_centerHorizontal="true"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:layout_alignParentTop="true"
    android:layout_marginTop="200dp"/>

</RelativeLayout>

Look at the image:

Upvotes: 0

waqaslam
waqaslam

Reputation: 68167

Yes, its possible. See below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <RadioGroup
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

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

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="sample text" />

    </RadioGroup>

</LinearLayout>

Sample output:

enter image description here

Upvotes: 6

Ali Behzadian Nejad
Ali Behzadian Nejad

Reputation: 9044

You can remove RadioGroup from your XML and add radio buttons one by one. Look at this:

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

  <RadioButton
    android:id="@+id/radioButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/radioGroup1"
    android:text="RadioButton" />

  <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/radioButton1"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

  <RadioButton
    android:id="@+id/radioButton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:text="RadioButton" />
</RelativeLayout>

Upvotes: 0

Related Questions