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

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>

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>

Can't centralize side-by-side pair of radioGroups in Android Studio

I cannot get the layout below such that the two side-by-side radioGroups centralize. At the moment, they are a little to the right. As soon as I try to adjust anything (either by clicking and dragging or by modifying the xml, all hell breaks loose and either the entire display vanishes or it gets corrupted.
I want to say, basically:
Center the top textfield and the Pass button.
Center the boundary between the two radioGroups.
Align the left hand side of the Double button to the left hand side of the left hand radio group.
Align the right hand side of the Redouble button to the right hand side of the right hand radioGRoup.
Align the "Small text" text field in the center, not to the left.
Align the Next button in the center.
Any pointers would be much appreciated. Code and image follow.
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.owner.bidmate.WhatBid"
android:background="#29ff3a">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="very long text so it widens the field"
android:id="#+id/bidtitle"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pass"
android:id="#+id/passButton"
android:checked="false"
android:textOff="Pass"
android:textOn="Pass"
android:background="#080dff"
android:textColor="#fafff8"
android:layout_below="#+id/bidtitle"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:layout_marginBottom="10dp"/>
<RadioGroup
android:layout_width="120dp"
android:layout_height="220dp"
android:id="#+id/radioGroup"
android:background="#befffc"
android:gravity="center_vertical"
android:layout_below="#+id/passButton"
android:layout_toLeftOf="#+id/radioGroup2"
android:layout_toStartOf="#+id/radioGroup2"
android:layout_marginBottom="10dp">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="One"
android:id="#+id/radioButton"
android:checked="false"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Two"
android:id="#+id/radioButton2"
android:checked="false"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Three"
android:id="#+id/radioButton3"
android:checked="false"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Four"
android:id="#+id/radioButton4"
android:checked="false"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Five"
android:id="#+id/radioButton5"
android:checked="false"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Six"
android:id="#+id/radioButton6"
android:checked="false"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Seven"
android:id="#+id/radioButton7"
android:checked="false"/>
</RadioGroup>
<RadioGroup
android:layout_width="120dp"
android:layout_height="220dp"
android:id="#+id/radioGroup2"
android:background="#c7fffd"
android:gravity="center_vertical"
android:layout_marginRight="0dp"
android:layout_marginEnd="22dp"
android:layout_alignTop="#+id/radioGroup"
android:layout_alignRight="#+id/redoubleButton"
android:layout_alignEnd="#+id/redoubleButton"
android:layout_marginBottom="10dp">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Club"
android:id="#+id/radioButton8"
android:checked="false"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Diamond"
android:id="#+id/radioButton9"
android:checked="false"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Heart"
android:id="#+id/radioButton10"
android:checked="false"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Spade"
android:id="#+id/radioButton11"
android:checked="false"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No Trump"
android:id="#+id/radioButton12"
android:checked="false"/>
</RadioGroup>
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Double"
android:id="#+id/doubleButton"
android:checked="false"
android:textOff="Double"
android:textOn="Double"
android:background="#ff151a"
android:textColor="#fefffd"
android:layout_below="#+id/radioGroup"
android:layout_alignLeft="#+id/radioGroup"
android:layout_alignStart="#+id/radioGroup"/>
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Redouble"
android:id="#+id/redoubleButton"
android:checked="false"
android:textOff="Redouble"
android:textOn="Redouble"
android:background="#ff1521"
android:textColor="#fbfffb"
android:layout_toRightOf="#+id/wbnextbutton"
android:layout_below="#+id/radioGroup2"
/>
<TextView
android:layout_width="200dp"
android:layout_height="50dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/textView"
android:layout_below="#+id/doubleButton"
android:layout_centerHorizontal="true"
android:layout_alignParentEnd="false"
android:paddingTop="20dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:id="#+id/wbnextbutton"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/passButton"
android:layout_alignEnd="#+id/passButton"/>
</RelativeLayout>
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.owner.bidmate.WhatBid"
android:background="#29ff3a">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="very long text so it widens the field"
android:id="#+id/bidtitle"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pass"
android:id="#+id/passButton"
android:checked="false"
android:textOff="Pass"
android:textOn="Pass"
android:background="#080dff"
android:textColor="#fafff8"
android:layout_below="#+id/bidtitle"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:layout_marginBottom="10dp"/>
<RadioGroup
android:layout_width="120dp"
android:layout_height="220dp"
android:id="#+id/radioGroup"
android:background="#befffc"
android:gravity="center_vertical"
android:layout_below="#+id/passButton"
android:layout_toLeftOf="#+id/temp"
android:layout_marginBottom="10dp">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="One"
android:id="#+id/radioButton"
android:checked="false"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Two"
android:id="#+id/radioButton2"
android:checked="false"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Three"
android:id="#+id/radioButton3"
android:checked="false"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Four"
android:id="#+id/radioButton4"
android:checked="false"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Five"
android:id="#+id/radioButton5"
android:checked="false"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Six"
android:id="#+id/radioButton6"
android:checked="false"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Seven"
android:id="#+id/radioButton7"
android:checked="false"/>
</RadioGroup>
<View android:layout_below="#+id/passButton" android:layout_centerHorizontal="true" android:layout_width="0dp" android:layout_height="1dp" android:id="#+id/temp"></View>
<RadioGroup
android:layout_width="120dp"
android:layout_height="220dp"
android:id="#+id/radioGroup2"
android:background="#c7fffd"
android:gravity="center_vertical"
android:layout_marginRight="0dp"
android:layout_marginEnd="22dp"
android:layout_below="#+id/passButton"
android:layout_toRightOf="#+id/temp"
android:layout_marginBottom="10dp">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Club"
android:id="#+id/radioButton8"
android:checked="false"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Diamond"
android:id="#+id/radioButton9"
android:checked="false"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Heart"
android:id="#+id/radioButton10"
android:checked="false"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Spade"
android:id="#+id/radioButton11"
android:checked="false"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No Trump"
android:id="#+id/radioButton12"
android:checked="false"/>
</RadioGroup>
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Double"
android:id="#+id/doubleButton"
android:checked="false"
android:textOff="Double"
android:textOn="Double"
android:background="#ff151a"
android:textColor="#fefffd"
android:layout_below="#+id/radioGroup"
android:layout_alignLeft="#+id/radioGroup"
android:layout_alignStart="#+id/radioGroup"/>
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Redouble"
android:id="#+id/redoubleButton"
android:checked="false"
android:textOff="Redouble"
android:textOn="Redouble"
android:background="#ff1521"
android:textColor="#fbfffb"
android:layout_toRightOf="#+id/wbnextbutton"
android:layout_alignRight="#+id/radioGroup2"
android:layout_below="#+id/radioGroup2"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:gravity="center"
android:id="#+id/textView"
android:layout_below="#+id/doubleButton"
android:layout_centerHorizontal="true"
android:layout_alignParentEnd="false"
android:paddingTop="20dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:id="#+id/wbnextbutton"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"/>
</RelativeLayout>

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.

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"**
>

How to provide numberings to the radio-buttons in 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

Categories

Resources