How to provide numberings to the radio-buttons in android? - android

I have a RadioGroup in which, I have provided four different radio button with text as :
<RadioGroup
android:id="#+id/radio_group"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="1dp"
android:background="#color/textbackgroundcolor"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:soundEffectsEnabled="true" >
<RadioButton
android:id="#+id/radio_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:checked="false"
android:textColor="#color/black"
android:textStyle="bold"
android:textSize="12dp"/>
<RadioButton
android:id="#+id/radio_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:checked="false"
android:textColor="#color/black"
android:textStyle="bold"
android:textSize="12dp"/>
<RadioButton
android:id="#+id/radio_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:checked="false"
android:textColor="#color/black"
android:textStyle="bold"
android:textSize="12dp"/>
<RadioButton
android:id="#+id/radio_four"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:checked="false"
android:textColor="#color/black"
android:textStyle="bold"
android:textSize="12dp"/>
</RadioGroup>
As of now, it display radio buttons with their text. But I want to display some labels/numbers before each radio button.
How can I provide numbers like 1, 2, 3, 4 for radio button?

There are different ways to do this. But the easiest way that comes to mind is just to add a TextView before each and place it in a LinearLayout together with each RadioButten
<RadioGroup
android:id="#+id/radio_group"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="1dp"
android:background="#color/textbackgroundcolor"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:soundEffectsEnabled="true" >
<LinearLayout >
<TextView
android:layout_width="fill_parent"
android:minLines="2"
android:gravity="center"
android:textSize="15dp"
android:text="1"
android:layout_height="wrap_content"/>
<RadioButton
android:id="#+id/radio_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:layout_marginLeft="5dp"
android:checked="false"
android:textStyle="bold"
android:textSize="12dp"/>
</LinearLayout>
</RadioGroup>
This is the general idea

Related

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>

android radio Button is not deselection when another radio button is selected

I have a layout that has multiple radio buttons in different rows ( two columns of radio buttons separated by table rows) and when i select one of them,the previous selected doesn't get deselected.I keep selecting all the other's and none gets deselected . This only happens because of the rows as the straight layout works just fine .what am i doing wrong ?
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginLeft="17dp"
android:layout_marginStart="17dp"
android:orientation="vertical"/>
<TableRow>
<RadioButton
android:id="#+id/miliToCentimeters"
android:layout_width="258dp"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:checked="true"
android:clickable="false"
android:text="#string/milimetersToCentimeters"
android:textSize="13sp" />
<RadioButton
android:id="#+id/inchesToFoothesToFoot"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/inchesToFoot"
android:textSize="13sp" />
</TableRow>
<TableRow>
<RadioButton
android:id="#+id/centiToMeters"
android:layout_width="255dp"
android:layout_height="wrap_content"
android:layout_marginTop="-5dp"
android:text="#string/centemetersToMeters"
android:textSize="13sp" />
<RadioButton
android:id="#+id/inchesToYardsesToYards"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="-5dp"
android:text="#string/inchesToYards"
android:textSize="13sp" />
</TableRow>
<TableRow>
<RadioButton
android:id="#+id/miliToMetersiToMeters"
android:layout_width="255dp"
android:layout_height="wrap_content"
android:layout_marginTop="-5dp"
android:text="#string/milimetersToMeters"
android:textSize="13sp" />
<RadioButton
android:id="#+id/feetToYardsetToYards"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="-5dp"
android:text="#string/feetToYards"
android:textSize="13sp" />
</TableRow>
<TableRow>
<RadioButton
android:id="#+id/metersToKiloersToKilo"
android:layout_width="255dp"
android:layout_height="wrap_content"
android:layout_marginTop="-5dp"
android:text="#string/metersToKilometers"
android:textSize="13sp" />
<RadioButton
android:id="#+id/feetToMilesetToMiles"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="-5dp"
android:text="#string/feetToMiles"
android:textSize="13sp"/>
</TableRow>
<TableRow>
<RadioButton
android:id="#+id/yardsToMilesdsToMiles"
android:layout_width="255dp"
android:layout_height="match_parent"
android:layout_marginTop="-5dp"
android:text="#string/yardsToMiles"
android:textSize="13sp" />
</TableRow>
</RadioGroup>
Use the below code for selecting single radio button at a time. You should close the radio group only after all the radio button declared inside of the radio group.
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginLeft="17dp"
android:layout_marginStart="17dp"
android:orientation="vertical">
<RadioButton
android:id="#+id/miliToCentimeters"
android:layout_width="258dp"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:checked="true"
android:clickable="false"
android:text="#string/milimetersToCentimeters"
android:textSize="13sp" />
<RadioButton
android:id="#+id/inchesToFoothesToFoot"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/inchesToFoot"
android:textSize="13sp" />
<RadioButton
android:id="#+id/centiToMeters"
android:layout_width="255dp"
android:layout_height="wrap_content"
android:layout_marginTop="-5dp"
android:text="#string/centemetersToMeters"
android:textSize="13sp" />
<RadioButton
android:id="#+id/inchesToYardsesToYards"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="-5dp"
android:text="#string/inchesToYards"
android:textSize="13sp" />
<RadioButton
android:id="#+id/miliToMetersiToMeters"
android:layout_width="255dp"
android:layout_height="wrap_content"
android:layout_marginTop="-5dp"
android:text="#string/milimetersToMeters"
android:textSize="13sp" />
<RadioButton
android:id="#+id/feetToYardsetToYards"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="-5dp"
android:text="#string/feetToYards"
android:textSize="13sp" />
<RadioButton
android:id="#+id/metersToKiloersToKilo"
android:layout_width="255dp"
android:layout_height="wrap_content"
android:layout_marginTop="-5dp"
android:text="#string/metersToKilometers"
android:textSize="13sp" />
<RadioButton
android:id="#+id/feetToMilesetToMiles"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="-5dp"
android:text="#string/feetToMiles"
android:textSize="13sp"/>
<RadioButton
android:id="#+id/yardsToMilesdsToMiles"
android:layout_width="255dp"
android:layout_height="match_parent"
android:layout_marginTop="-5dp"
android:text="#string/yardsToMiles"
android:textSize="13sp" />
</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.

aligning radio buttons in 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.

Great distribution of horizontal radio buttons

I have 3 RadioButton in one line (linearLayout). I want to distribute them properly in the line, and not align all of them to the left, just as in this screenshot.
Currently it is displayed like this:
XML:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_below="#+id/linearLayout2"
android:layout_alignLeft="#+id/linearLayout2" android:id="#+id/linearLayout3">
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yes"
android:id="#+id/radioButton3" android:layout_gravity="center_horizontal|top" android:checked="false"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Maybe"
android:id="#+id/radioButton" android:layout_gravity="center_horizontal|top" android:checked="true"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No"
android:id="#+id/radioButton2" android:checked="false"/>
</RadioGroup>
</LinearLayout>
It is possible for classic buttons with 0dp (see here), but RadioButtons are invisible with this setting.
Is there an easy way to do that ?
Try this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linearLayout2"
android:layout_below="#+id/linearLayout2" >
<RadioGroup
android:layout_width="fill_parent"
android:weightSum="3"
android:layout_height="fill_parent" android:orientation="horizontal">
<RadioButton
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Yes"
android:id="#+id/radioButton3" android:checked="false"/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Maybe"
android:layout_weight="1"
android:id="#+id/radioButton" android:checked="true"/>
<RadioButton
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="No"
android:id="#+id/radioButton2" android:checked="false"/>
</RadioGroup>
</LinearLayout>
Then you must have to use layout weight on you radio button:-
android:layout_weight beginner's question
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_below="#+id/linearLayout2"
android:layout_alignLeft="#+id/linearLayout2" android:id="#+id/linearLayout3">
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="horizontal">
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Yes"
android:id="#+id/radioButton3" android:layout_gravity="center_horizontal|top" android:checked="false"/>
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Maybe"
android:id="#+id/radioButton" android:layout_gravity="center_horizontal|top" android:checked="true"/>
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="No"
android:id="#+id/radioButton2" android:checked="false"/>
</RadioGroup>
</LinearLayout>

Categories

Resources