RadioButtons with TextView in RadioGroup
Asked Answered
A

3

9

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

Anaclitic answered 24/4, 2012 at 13:2 Comment(0)
P
6

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

Pincenez answered 24/4, 2012 at 13:24 Comment(4)
The problem begins though, when I need to put TextView between two RadioButtons - it is not possible to have it under RadioGroup, because the selection will be disabled and it starts doing funky things.Anaclitic
Could you please show screen of text between and in the bottom? Can't make it work :(Anaclitic
I mean, sure the layout works just great, but functionality loses precision - if I check third RadioButton, it does weird things...Anaclitic
just move the TextView between two CheckBoxes. thats what i did and it workedPincenez
O
0

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>
Osteogenesis answered 24/4, 2012 at 13:21 Comment(1)
Yes, but then I can't work with RadioButtons like with group - which involves lot of coding with own RadioButton mechanism...Anaclitic
O
0

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:

Osteogenesis answered 24/4, 2012 at 15:2 Comment(1)
nope, not working, try removing center_horizontal...aligns itself with top of the screen...Anaclitic

© 2022 - 2024 — McMap. All rights reserved.