android - How to add the view beside the radio button
Asked Answered
T

3

5

I want to set the view beside the radio button in the radio group. I tried but i am not getting. please can anybody help me.

Source Code

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

    <RadioGroup
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:checkedButton="@+id/view_completehistroy_rb"
            android:id="@+id/timeframe_group">

           <!-- View complete history Radio Button -->
           <RadioButton
                android:text="Complete History"
                android:textSize="16sp"
                android:layout_marginLeft="5dp"
                android:id="@+id/view_completehistroy_rb"
                android:textColor="#000000"
                />

            <!-- View history using date range Radio Button -->
            <RadioButton
                android:text="From"
                android:textSize="16sp"
                android:layout_marginLeft="5dp"
                 android:textColor="#000000"
                android:id="@+id/view_daterange_history_rb"/>

        </RadioGroup>

    <RelativeLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
          <!--Layout for Time selection  -->
          <LinearLayout 
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              android:layout_marginLeft="5dp"
              android:layout_toRightOf="@id/view_daterange_history_rb">                      

               <TextView
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_marginLeft="5dip"
                 android:layout_marginRight="5dip"
                 android:text="test1" 
                  android:textColor="#000000"
                 android:textSize="16sp"/>  

                <TextView
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_marginLeft="5dip"
                 android:layout_marginRight="5dip"
                 android:text="test2" 
                  android:textColor="#000000"
                 android:textSize="16sp"/>                  
          </LinearLayout>
    </RelativeLayout>
</LinearLayout>

enter image description here

Tapp answered 9/8, 2012 at 12:35 Comment(3)
I thinks its not possible. But if you explain why you need to place it so, we can find out some other method. I thinks you want to display two dates there in those textviews. right?Sediment
Here i want to use the daterange option like "from Date picker to DatePicker" thats whyTapp
Based on this comment, the answer by @RahmathullahMPulikkal should be the best solution rather than trying to place a new view next to itBarncard
S
7

So what about that:

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

    <RadioGroup
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:checkedButton="@+id/view_completehistroy_rb"
            android:id="@+id/timeframe_group">

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

            <RadioButton
                android:id="@+id/view_completehistroy_rb"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:text="Complete History"
                android:textColor="#000000"
                android:textSize="16sp" />

            <RadioButton
                android:id="@+id/view_daterange_history_rb"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/view_completehistroy_rb"
                android:layout_marginLeft="5dp"
                android:text="From"
                android:textColor="#000000"
                android:textSize="16sp" />

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBaseline="@+id/view_daterange_history_rb"
                android:layout_marginLeft="5dip"
                android:layout_marginRight="5dip"
                android:layout_toRightOf="@+id/view_daterange_history_rb"
                android:text="test1"
                android:textColor="#000000"
                android:textSize="16sp" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBaseline="@+id/textView1"
                android:layout_marginLeft="5dip"
                android:layout_marginRight="5dip"
                android:layout_toRightOf="@+id/textView1"
                android:text="test2"
                android:textColor="#000000"
                android:textSize="16sp" />

        </RelativeLayout>

        </RadioGroup>
</LinearLayout>

On my phone it looks like this:

On my phone it looks like this

--- some more words to my answer ---

So well, I think the point is a misunderstanding with the LinearLayout. That one does only support placing view Elements in a row. If you want to have View-Elements that are related to eachother you need the Relative Layout. The RelativeLayout has a attributes like

android:layout_below="@+id/XXXXX"

which is a setting for a relative position to other view elements. So using this you can say that one element (like the TextView) is supposed to be below/above/leftTo/rightTo another view. That was just the point. I hope that makes it easier to understand.

Subjectivism answered 9/8, 2012 at 12:59 Comment(4)
Could you perhaps expand a little bit to explain why this is an answer to the question? I know it probably seems obvious to you, but it's often helpful to explain some code to show what exactly you did.Riordan
Here alignment is working but functionality of radio group is not working means these 2 radio buttons are selected at a time. can u check it once u get clear idea.Tapp
If RadioButtons are not first children of RadioGroup, RadioGroup doesn't work correctly.Biscay
This is not a correct answer. This method ruins the RadioGroups purpose... Selecting the other radio button doesn't deselect the previously selected one.Sanity
S
2

Here you go. Its not an answer to the question. But it can solve your issue. YOu can set text of a radio button as follows...

RadioButton radBtn=(RadioButton)findViewById(R.id.view_daterange_history_rb);        
radBtn.setText("From 20/12/2000 to 21/12/2012");

XML

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

<RadioGroup
    android:id="@+id/timeframe_group"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:checkedButton="@+id/view_completehistroy_rb"
    android:orientation="vertical" >

    <!-- View complete history Radio Button -->

    <RadioButton
        android:id="@+id/view_completehistroy_rb"
        android:layout_marginLeft="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Complete History"
        android:textColor="#000000"
        android:textSize="16sp" />

    <!-- View history using date range Radio Button -->

    <RadioButton
        android:id="@+id/view_daterange_history_rb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:text="From"
        android:textColor="#000000"
        android:textSize="16sp" />
</RadioGroup>

Sediment answered 9/8, 2012 at 13:4 Comment(0)
D
0

You can design a layout in such a way as i have done. Here is the sample layout part :

    <RelativeLayout
            android:id="@+id/faxPannel"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/emailPannel" >

            <RadioButton
                android:id="@+id/radioButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_marginRight="10dp"
                android:layout_marginTop="30dp" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dip"
                android:layout_marginRight="5dip"
                android:layout_marginTop="10dp"
                android:layout_toRightOf="@+id/radioButton"
                android:layout_weight="1"
                android:singleLine="true"
                android:text="Label"
                android:textColor="@color/Black"
                android:textSize="16dp"
                android:textStyle="bold" />

        </RelativeLayout>

And in activity handle the oncheckChanged() event to perform the related functionality. but if you will use RadioGroup in layout, i think it won't be possible for you to achieve what you want.

Note : In above sample where i used TextView you can use any view you want.

Dehart answered 9/8, 2012 at 13:12 Comment(1)
I think Tobias Reich's post can also work but i haven't tried in that way. You can try both.Dehart

© 2022 - 2024 — McMap. All rights reserved.