Centered gravity moves radio button description, not button itself - android

In the XML to follow, each RadioButton is set to android:gravity="center".
All three buttons show up, but the button descriptions are centered. The buttons themselves are not. How do I center the buttons as well as their descriptions?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".3"
android:gravity="center"
android:text="RadioButton" />
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".3"
android:gravity="center"
android:checked="true"
android:text="RadioButton" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".3"
android:gravity="center"
android:text="RadioButton" />
</RadioGroup>
</LinearLayout>
In this picture, you can ignore the top bar. That's already been coded in. What I'm seeing is the RadioButtons far to the right of the "RadioButton" text (the button itself is up against the layout bounds). The desired end result is, since I can't post images: Here it is.

I have tried editing your layout to do what is in your screenshot. I don't think this is an efficient way to do this but this is the only way that worked for me.
I inserted every radiobutton inside a parent linerlayout, and then used gravity center.
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".3"
android:gravity="center" >
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".3"
android:gravity="center" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="RadioButton" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".3"
android:gravity="center" >
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
</LinearLayout>
</RadioGroup>
I really hope there's a better solution than this. But here, hope it helps. :)

Just add widget.Space in between, so the entire widget.RadioButton will get shifted to right...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</RadioGroup>

Related

Aligning Radio Buttons to left in ConstraintLayout?

I can align the Button elements to the left to left using layout_constraintStart_toStartOf and layout_constraintEnd_toStartOf. However I'm unable to do that to the radio buttons.
How can I align the radio buttons to left to achieve something like this:
Here's the layout:
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent" >
<RadioButton
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
<RadioButton
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2" />
</RadioGroup>
Try using horizontal orientation in your RadioGroup like below:
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#C8E585"
app:layout_constraintTop_toTopOf="parent" >
<RadioButton
android:id="#+id/btn1"
android:textSize="18sp"
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ColdFusion" />
<RadioButton
android:id="#+id/btn2"
android:textSize="18sp"
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Flex" />
<RadioButton
android:id="#+id/btn3"
android:textSize="18sp"
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Flash" />
</RadioGroup>
Output:

Style Radio Group Android

I want ask how to we can design Radio Group in Android like this picture
"My Style"
I'm design XML like this :
<RadioGroup
android:layout_below="#+id/btnGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/rdiEasy"
android:text="EASY"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="#+id/rdiMedium"
android:text="MEDIUM"
android:layout_marginRight="8dp"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/rdiHard"
android:text="HARD"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="#+id/rdiHardest"
android:text="HARDEST"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</RadioGroup>
I'm use RadioButtonGroup wrap 2 RelativeLayout , each RelativeLayout contain two Radio Button. But with this design , i can't choose only one Radio Button , still can choose multi radio button. So anyone can help me how to design Radio Button with this design but just only choose one option.
I think this will work for you
https://gist.github.com/ishitcno1/9544243
Use this code it work perfect.
<RelativeLayout
android:id="#+id/rl1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="horizontal"
android:transitionGroup="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="30dp">
<RadioButton
android:id="#+id/rdiEasy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Easy" />
<RadioButton
android:id="#+id/rdiHard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hard" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="30dp">
<RadioButton
android:id="#+id/rdiMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/rdiEasy"
android:text="Medium" />
<RadioButton
android:id="#+id/rdiHardest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/rdiHard"
android:text="Hardest" />
</LinearLayout>
</RadioGroup>
</RelativeLayout>
Try this code without using layouts inside radiogroup.and try to achieve the desired pattern by setting gravity,like this and check if radio button is selected only one or multiple
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="500dp"
android:layout_height="250dp"
android:layout_weight=".07"
android:gravity="center">
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|top"
android:layout_marginLeft="5dp"
android:text="New RadioButton" />
<RadioButton
android:id="#+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|top"
android:layout_marginRight="5dp"
android:text="New RadioButton" />
<RadioButton
android:id="#+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|left"
android:layout_marginLeft="5dp"
android:text="New RadioButton" />
<RadioButton
android:id="#+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginRight="5dp"
android:text="New RadioButton" />
</RadioGroup>

Scrollview can only host one child

I can't figure out how to get these radio groups/text views into a scrollview. Can anyone help me? Heres what i've tried but I keep getting the error scrollview can only host one child and I don't know how to fix it.
<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"
tools:context=".ContactSettingsActivity" >
<RelativeLayout
android:id="#+id/navbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/navbar_background" >
<ImageButton
android:id="#+id/imageButtonList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:layout_toLeftOf="#+id/imageButtonMap"
android:src="#drawable/contactlisticon" />
<ImageButton
android:id="#+id/imageButtonMap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/mapicon" />
<ImageButton
android:id="#+id/imageButtonSettings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#+id/imageButtonMap"
android:src="#drawable/settingsicon" />
</RelativeLayout>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/navbar">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:text="Sort Contact By:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginLeft="35dp"
android:layout_marginTop="10dp"
>
<RadioButton
android:id="#+id/radioName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Name" />
<RadioButton
android:id="#+id/radioCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="City" />
<RadioButton
android:id="#+id/radioBirthday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Birthday" />
</RadioGroup>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/radioGroup1"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:text="Sort Order:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RadioGroup
android:id="#+id/radioGroup2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="35dp"
android:layout_marginTop="10dp"
android:layout_below="#+id/textView2" >
<RadioButton
android:id="#+id/radioAscending"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Ascending" />
<RadioButton
android:id="#+id/radioDescending"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Descending" />
</RadioGroup>
</ScrollView>
</RelativeLayout>
Choose a single container layout for all the child items you want to appear in a ScrollView. Most of the time this will be a LinearLayout to get them stacked vertically, but it could really be any view you want. You just need to choose one and arrange the children in it manually.
Add a layout inside scroll view and move the ui widgets(textview and radiogroup) inside that layout.

Distance between two radiobutton (horizontal radio group)

I want to set distance between two radio buttons.
Like
How i can achieve this? My code is like
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/textView_your_order"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Delivery" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PickUp" />
</RadioGroup>
</LinearLayout>
Above code output looks like below picture
you can achieve this result with :
using layout_weigth and matching radio group to fill the screen
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/textView_your_order"
android:orientation="horizontal">
<RadioButton
android:layout_weight="1"
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Delivery" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PickUp" />
</RadioGroup>
or
even adding a dummy view that pushes both radio button to left end and right end like this:
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/textView_your_order"
android:orientation="horizontal">
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Delivery" />
<TextView
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RadioButton
android:gravity="end"
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PickUp" />
</RadioGroup>
Try android:gravity="right" in the xml for the RadioButton which you want it to be right.
This should do want you want.
I have done after changing my code to this one.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/SkyBlue"
android:orientation="vertical"
android:padding="8dp" >
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/textView_your_order"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Delivery" />
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PickUp" />
</RadioGroup>
</LinearLayout>
Just change layout width in the first radio button, Put :
android:layout_width = "200dp"

android radiogroup not wrap content if there are much radio buttons in group

What i want is my radio list auto break to next line if it meets the border of screen.
but the result came to be like that (i had 8 radio btns):
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top|center"
android:orientation="vertical"
android:padding="10dp" >
<EditText
android:id="#+id/card_no_claim"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:hint="#string/hint_card_number"
android:inputType="text" />
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/radio50plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:button="#android:color/transparent"
android:drawableTop="#drawable/custom_radio_button" />
<RadioButton
android:id="#+id/radio50palus"
android:background="#android:color/transparent"
android:button="#android:color/transparent"
android:drawableTop="#drawable/custom_radio_button" />
<RadioButton
android:id="#+id/radio5f0plus"
android:background="#android:color/transparent"
android:button="#android:color/transparent"
android:drawableTop="#drawable/custom_radio_button" />
<RadioButton
android:id="#+id/radio50pluas"
android:background="#android:color/transparent"
android:button="#android:color/transparent"
android:drawableTop="#drawable/custom_radio_button" />
<RadioButton
android:id="#+id/rahdio50plus"
android:background="#android:color/transparent"
android:button="#android:color/transparent"
android:drawableTop="#drawable/custom_radio_button" />
<RadioButton
android:id="#+id/radieo50plus"
android:background="#android:color/transparent"
android:button="#android:color/transparent"
android:drawableTop="#drawable/custom_radio_button" />
<RadioButton
android:id="#+id/radio50plaus"
android:background="#android:color/transparent"
android:button="#android:color/transparent"
android:drawableTop="#drawable/custom_radio_button" />
<RadioButton
android:id="#+id/radio50palaus"
android:background="#android:color/transparent"
android:button="#android:color/transparent"
android:drawableTop="#drawable/custom_radio_button" />
</RadioGroup>
</LinearLayout>
</ScrollView>
im an android newbie please help. thank you so much. sorry for my bad English.

Categories

Resources