Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android - How to add the view beside the radio button

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

like image 640
naresh Avatar asked Aug 09 '12 12:08

naresh


People also ask

How do I get radio buttons side by side on android?

Use the android:orientation="horizontal" -attribute of the RadioGroup.

What are the methods of radio button in android?

RadioButton is a two states button which is either checked or unchecked. If a single radio button is unchecked, we can click it to make checked radio button. Once a radio button is checked, it cannot be marked as unchecked by user. RadioButton is generally used with RadioGroup.

How do you check whether a RadioButton is checked or not in android?

You can check the current state of a radio button programmatically by using isChecked() method. This method returns a Boolean value either true or false. if it is checked then returns true otherwise returns false. Below is an example code with explanation in which we checked the current state of a radio button.

What is a radio button selection?

Radio buttons are a common way to allow users to make a single selection from a list of options. Since only one radio button can be selected at a time (within the same group), each available choice must be its own item and label.


2 Answers

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.

like image 109
Tobias Reich Avatar answered Oct 22 '22 07:10

Tobias Reich


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>

like image 31
Rahmathullah M Avatar answered Oct 22 '22 06:10

Rahmathullah M