aligning radio buttons in android - android

I am trying to re arrange the radio buttons
I have a output
search_page.xml
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
android:background="#E1E1E1"
android:weightSum="1" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:text="City" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:background="#drawable/rounded_edittext"
android:layout_weight=".75" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#android:color/black" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="selectDate"
android:orientation="horizontal"
android:padding="10dp"
android:background="#E1E1E1"
android:weightSum="1" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:text="date" />
<EditText
android:id="#+id/DATE_EDIT_TEXT_ID"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight=".75"
android:background="#drawable/rounded_edittext"
android:onClick="selectDate" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#android:color/black" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
android:background="#E1E1E1"
android:weightSum="1" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:text="type" />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/radio0"
android:background="#drawable/yourbuttonbackground"
android:button="#android:color/transparent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Breakfast" />
<RadioButton
android:id="#+id/radio1"
android:background="#drawable/yourbuttonbackground"
android:button="#android:color/transparent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lunch" />
</RadioGroup>
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/yourbuttonbackground"
android:button="#android:color/transparent"
android:text="Dinner" />
</LinearLayout>
to space the radio buttons in order as below !
Any ideas !

Copy the below code in your xml.I have changed weight distribution in layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#E1E1E1"
android:orientation="horizontal"
android:padding="10dp"
android:weightSum="2.5" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="type" />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="2dp"
android:layout_weight=".5"
android:background="#drawable/yourbuttonbackground"
android:button="#android:color/transparent"
android:checked="true"
android:padding="5dp"
android:text="Breakfast" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="2dp"
android:layout_weight=".5"
android:background="#drawable/yourbuttonbackground"
android:button="#android:color/transparent"
android:padding="5dp"
android:text="Lunch" />
</RadioGroup>
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="2dp"
android:layout_weight=".5"
android:background="#drawable/yourbuttonbackground"
android:button="#android:color/transparent"
android:padding="5dp"
android:text="Dinner" />
</LinearLayout>
Hope this works

I would suggest not to use radioGroup for horizontal. i have found that there are issues on different devices when doing this (Galaxy Note 2 was one of these devices more recently).
Just use a horizontal linear layout, push it left with margins to align it and put buttons inside it which will have text and your background shape.
Then in code add click listeners to them, and have a way to save which button was last clicked.
you can have the state drawable contain a selected="true" state and call button.setSelected(true or false) to get it to show up as selected or deselected.

Related

Radio group in table layout with radio buttons aligned to columns

I am developing a feedback application which should has similar layout to this screen. [1]: https://i.stack.imgur.com/xn3kh.jpg
I have designed the xml for layout.
<LinearLayout 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:background="#000"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.zankrutparmar.feedback.Frag_one">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/frag_one_title"
android:textColor="#fff"
android:textStyle="bold|italic"
android:textSize="35dp"
android:textAlignment="center"/>
<LinearLayout
android:layout_width="819dp"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:layout_marginEnd="#dimen/activity_vertical_margin"
android:text="Poor"
android:textAlignment="center"
android:textColor="#fff"
android:textSize="35dp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_vertical_margin"
android:text="Average"
android:textAlignment="center"
android:textColor="#fff"
android:textSize="35dp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_vertical_margin"
android:text="Good"
android:textAlignment="center"
android:textColor="#fff"
android:textSize="35dp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_vertical_margin"
android:text="Very Good"
android:textAlignment="center"
android:textColor="#fff"
android:textSize="35dp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_vertical_margin"
android:text="Excellent"
android:textAlignment="center"
android:textColor="#fff"
android:textSize="35dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="6">
<TextView
android:layout_width="58dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_vertical_margin"
android:layout_weight="1"
android:text="Theme & Decor"
android:textAlignment="center"
android:textColor="#fff"
android:textSize="25dp"
android:textStyle="bold|italic" />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_vertical_margin"
android:layout_weight="5"
android:orientation="horizontal">
<RadioButton
android:id="#+id/poor1"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/custom_btn_radio_poor"
android:button="#null" />
<RadioButton
android:id="#+id/average1"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/custom_btn_radio_average"
android:button="#null" />
<RadioButton
android:id="#+id/good1"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/custom_btn_radio_good"
android:button="#null" />
<RadioButton
android:id="#+id/very1"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/custom_btn_radio_very_good"
android:button="#null" />
<RadioButton
android:id="#+id/excellent1"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/custom_btn_radio_excellent"
android:button="#null" />
</RadioGroup>
</LinearLayout>
Now whenever I want to apply margin to these radio buttons the center one remains in center and pushes all other buttons far.
I also tried the answers on this question [1]: TableLayout of radiogroup(s) with respective label(s) aligned in android
But in that layout the problem is all buttons in single row are getting selected. It means Radiogroup can't be placed in that layout.
And I have to fetch questions from a database and place them in my application.
So in Future I will have to add rows dynamically.
Any help will be very appreciated. Thank you in advance.
Okay I designed a new layout and fixed those previous issues. I am uploading that code.
<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:background="#8bae3a"
tools:context="com.zankrutparmar.feedback.Frag_two">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView5"
android:layout_column="1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/textView10"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/textView9"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/textView6"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
<TableRow
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<RadioGroup
android:layout_marginLeft="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:orientation="horizontal">
<RadioButton
style="#style/MyRadioButtonStyle"
android:gravity="center"
android:padding="8dp"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<RadioButton
style="#style/MyRadioButtonStyle"
android:gravity="center"
android:paddingLeft="8dp"
android:layout_marginLeft="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
style="#style/MyRadioButtonStyle"
android:gravity="center"
android:paddingLeft="8dp"
android:layout_marginLeft="5dp"
/>
<RadioButton
style="#style/MyRadioButtonStyle"
android:gravity="center"
android:paddingLeft="8dp"
android:layout_marginLeft="7dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</RadioGroup>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<RadioGroup
android:layout_marginLeft="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:orientation="horizontal">
<RadioButton
style="#style/MyRadioButtonStyle"
android:gravity="center"
android:padding="8dp"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<RadioButton
style="#style/MyRadioButtonStyle"
android:gravity="center"
android:paddingLeft="8dp"
android:layout_marginLeft="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
style="#style/MyRadioButtonStyle"
android:gravity="center"
android:paddingLeft="8dp"
android:layout_marginLeft="5dp"
/>
<RadioButton
style="#style/MyRadioButtonStyle"
android:gravity="center"
android:paddingLeft="8dp"
android:layout_marginLeft="7dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</RadioGroup>
</TableRow>
</TableLayout>

Android vertically align text and widget

I'm using a text label and a radio group in a horizontal linear layout:
How do I set it so that the label (Sex) appears in the vertical center of the radio group? Currently it appears too high
The code for this portion of my layout is:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:text="Sex:" />
<RadioGroup
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"
android:id="#+id/input_button_male"
android:paddingRight="10dp"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:id="#+id/input_button_female"
android:paddingRight="10dp"/>
</RadioGroup>
</LinearLayout>
I think you could use android:layout_marginTop="??" within the Textview.
Alternately you could use android:gravity="center_vertical" within the Textview. Probably the latter.
i.e.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:layout_gravity="center_vertical"
android:text="Sex:" />
<RadioGroup
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"
android:id="#+id/input_button_male"
android:paddingRight="10dp"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:id="#+id/input_button_female"
android:paddingRight="10dp"/>
</RadioGroup>
</LinearLayout>
Do you want like this then put in your textview below code
android:layout_gravity="center_vertical"
Check this out!
android:layout-width="0dp" not recommended! Prefer androd:layout-width="wrap_content"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_marginLeft="48dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_weight="0.4"
android:text="Sex :"
android:textSize="20sp" />
<RadioGroup
android:layout_marginRight="72dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:orientation="horizontal">
<RadioButton
android:id="#+id/input_button_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:text="Male" />
<RadioButton
android:id="#+id/input_button_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:text="Female" />
</RadioGroup>

How to adjust space between radio button in android

In my android application using radio buttons to select screen view. I want radio button equally aligned horizontal. I tried layout xml shown below.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Views"
android:paddingLeft="10dp"
android:textSize="20sp"
/>
<RadioGroup
android:id="#+id/radioView"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:measureWithLargestChild="true"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radioViewSingle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:drawableRight="#drawable/single"
android:layout_weight="1"
android:checked="true" />
<RadioButton
android:id="#+id/radioView2by2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableRight="#drawable/view2x2" />
<RadioButton
android:id="#+id/radioView3by3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableRight="#drawable/view3x3" />
<RadioButton
android:id="#+id/radioView4by4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableRight="#drawable/view4x4" />
</RadioGroup>
</LinearLayout>
output obtained using above xml code
But i expecting below output
Thanks in advance
Instead of using android:drawableRight, use android:drawableLeft.
I just tested this and it is working as expected
Remove android:layout_weight="1" for all RadioButtons and set android:layout_width="0dp" to android:layout_width="wrap_content"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="Views"
android:textSize="20sp" />
<RadioGroup
android:id="#+id/radioView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:measureWithLargestChild="true"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left">
<RadioButton
android:id="#+id/radioViewSingle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:drawableRight="#android:drawable/ic_menu_search" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left">
<RadioButton
android:id="#+id/radioView2by2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="25dp"
android:drawableRight="#android:drawable/ic_menu_camera" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left">
<RadioButton
android:id="#+id/radioView3by3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableRight="#android:drawable/ic_menu_always_landscape_portrait" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left">
<RadioButton
android:id="#+id/radioView4by4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="25dp"
android:drawableRight="#android:drawable/ic_menu_call" />
</LinearLayout>
</RadioGroup>
</LinearLayout>
You can put each RadioButton to one layout and give all layout_width="0dp" and give layout_weight="1" as showen below:
<RadioGroup
android:id="#+id/radioView"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:measureWithLargestChild="true"
android:orientation="horizontal">
<LinearLayout
android:gravity="left"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp">
<RadioButton
android:checked="true"
android:drawableRight="#drawable/single"
android:id="#+id/radioViewSingle"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</LinearLayout>
<LinearLayout
android:gravity="left"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp">
<RadioButton
android:drawableRight="#drawable/view2x2"
android:id="#+id/radioView2by2"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</LinearLayout>
<LinearLayout
android:gravity="left"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp">
<RadioButton
android:drawableRight="#drawable/view3x3"
android:id="#+id/radioView3by3"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</LinearLayout>
<LinearLayout
android:gravity="left"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp">
<RadioButton
android:drawableRight="#drawable/view4x4"
android:id="#+id/radioView4by4"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</LinearLayout>
</RadioGroup>

Can i have a Linear layout inside RadioGroup to hold radio button?

I want to have a Customized Radio Group view. So i have created xml view as below
<RadioGroup
android:id="#+id/selectUserRadioBtnContainer"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingRight="30dp"
android:gravity="center_vertical"
android:orientation="horizontal" >
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:textSize="14sp"
android:fontFamily="normal"
android:layout_gravity="left|center_vertical"
android:text="#string/btnTextAddPic"
android:cropToPadding="true"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:textColor="#ffffff"
android:background="#drawable/profile_pic_rounded_background" />
<TextView
android:id="#+id/txtUserFullName"
style="#style/reviewInputLabels"
android:layout_width="0dip"
android:layout_height="30dp"
android:textColor="#33B5E5"
android:gravity="center_vertical|left"
android:textSize="16sp"
android:layout_weight="1"
android:text="Patient Name"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:tag="MAINUSER"
style="#style/radLabel"
android:button="#drawable/button_radion_default_custom" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingRight="30dp"
android:gravity="center_vertical"
android:orientation="horizontal" >
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:textSize="14sp"
android:fontFamily="normal"
android:layout_gravity="left|center_vertical"
android:text="#string/btnTextAddPic"
android:cropToPadding="true"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:textColor="#ffffff"
android:background="#drawable/profile_pic_rounded_background" />
<TextView
android:id="#+id/txtUserFullName"
style="#style/reviewInputLabels"
android:layout_width="0dip"
android:layout_height="30dp"
android:textColor="#33B5E5"
android:gravity="center_vertical|left"
android:textSize="16sp"
android:layout_weight="1"
android:text="Patient Name"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:tag="MAINUSER"
style="#style/radLabel"
android:button="#drawable/button_radion_default_custom" />
</LinearLayout>
</RadioGroup>
Is this a right way to do?. The problem that I am facing is Radio buttons are not considered as children of Radio Group. Can anybody help me out with this ?
Try this for placing text and image on left of radio button
<RadioButton
android:id="#+id/rbtnid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="#color/white"
android:button="#null"
android:drawablePadding="30dp"
android:drawableRight="#android:drawable/btn_radio"
android:text="rtext"
android:gravity="center|right"/>
Hope it helps.

Android:placing the radio buttons horizontally

Below is my Relative layout..i am trying to place the radio buttons above the edittext
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF" >
<ImageView
android:id="#+id/fbreplycancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/fbcancel" />
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="80dp"
android:layout_height="250dp"
android:layout_alignParentRight="true"
android:layout_below="#+id/fbcancel" >
<Spinner
android:id="#+id/replyspinner"
android:layout_width="50dp"
android:layout_height="30dp"
android:layout_alignLeft="#+id/fbshare"
android:layout_alignRight="#+id/fbshare"
android:layout_below="#+id/fbshare"
android:layout_marginTop="16dp"
android:drawSelectorOnTop="true"
android:entries="#array/fbcommentlist"
android:visibility="gone" />
<Button
android:id="#+id/fbshare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#drawable/fbbuttons"
android:text="#string/share" />
<ImageView
android:id="#+id/fbpeople"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/fbshare"
android:layout_centerHorizontal="true"
android:layout_marginBottom="19dp"
android:background="#drawable/people2"
android:drawSelectorOnTop="true"
android:paddingTop="20dp" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="RadioButton" />
</RelativeLayout>
<View
android:layout_width="250dp"
android:layout_height="0.7dip"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/imageView1"
android:background="#3b5998" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/fbcancel"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:paddingRight="2dp"
android:src="#drawable/askabud" />
<TextView
android:id="#+id/fbcommentpostedby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/fbedittext"
android:layout_alignRight="#+id/fbcommentdisplay"
android:layout_below="#+id/imageView1"
android:layout_marginTop="15dp"
android:textColor="#000000" />
<TextView
android:id="#+id/fbcommentdisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/fbedittext"
android:layout_below="#+id/fbcommentpostedby"
android:layout_toLeftOf="#+id/relativeLayout1"
android:textColor="#000000" />
<TextView
android:id="#+id/fbtextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView1"
android:layout_toLeftOf="#+id/fbreplycancel"
android:layout_toRightOf="#+id/imageView1"
android:text="#string/replyrecommend"
android:textSize="18sp"
android:textColor="#000000"/>
<TextView
android:id="#+id/fbplacename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/fbcommentdisplay"
android:layout_alignRight="#+id/fbcommentdisplay"
android:layout_below="#+id/fbcommentdisplay"
android:textColor="#000000" />
<EditText
android:id="#+id/fbedittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/fbplacename"
android:layout_marginLeft="19dp"
android:layout_marginTop="45dp"
android:layout_toLeftOf="#+id/relativeLayout1"
android:background="#drawable/roundcorners"
android:ems="10"
android:hint="#string/fbhint"
android:lines="6"
android:scrollHorizontally="true"
android:textSize="14sp"
android:windowSoftInputMode="stateHidden" />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/fbplacename" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="30dp"
android:layout_height="30dp"
android:checked="true"
android:text="1"
/>
</RadioGroup>
<RadioGroup
android:id="#+id/radioGroup2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/radioGroup1"
android:layout_toLeftOf="#+id/relativeLayout1" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="26dp"
android:layout_height="wrap_content"
android:checked="true"
android:text="3" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
</RadioGroup>
<RadioButton
android:id="#+id/radio1"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignTop="#+id/radioGroup2"
android:layout_toLeftOf="#+id/radioGroup2"
android:text="2" />
</RelativeLayout>
Iam facing a hard time to arrange them horizontally.Any help is appreciated.
To place a radiogroup (or any other view) above other just do:
android:layout_above="#+id/view_below"
To change the orientation just set:
android:orientation="horizontal"
And to give equal width to items make use of layout_weight:
<RadioGroup
android:id="#+id/radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_above="#+id/view_below" >
<RadioButton
android:id="#+id/radio1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:checked="true"
android:text="First" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Second" />
</RadioGroup>
You can use table layout instead of relative layout. Insert row into into table layout and place radio buttons inside table row..
For equal spacing follow following procedure
<TableRow
android:id="#+id/tableRow2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="50dp" >
<RadioButton
android:id="#+id/button2"
android:layout_width="5dp"
android:layout_height="60dp"
android:layout_weight="1"
android:text="#string/btnReject"
android:onClick="onCallRejectButton" />
<RadioButton
android:id="#+id/button1"
android:layout_width="5dp"
android:layout_height="60dp"
android:layout_weight="1
android:onClick="onCallAcceptButton"
android:text="#string/btnAccept" />
</TableRow>
android:orientation="horizontal" just do this. Type the orientation horizontal property to the radiogroup tag
<RadioGroup
android:id="#+id/radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"
android:checked="true" />
<RadioButton
android:id="#+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female" />
</RadioGroup>
You just need to put Radio Group orientation to horizontal.
<RadioGroup
android:id="#+id/radio_group_forgot_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
**android:orientation="horizontal"**
>

Categories

Resources