I started to develop for Android few days ago and I got stuck trying to use Radio Group. In my code for some reason all of the radio buttons can be selected together.
Any suggestion?
xml:
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rG">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="89dp"
android:layout_below="#id/title"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="95"
android:layout_marginLeft="75sp"
android:layout_marginTop="20sp"
android:id="#+id/rb95"
android:layout_gravity="left|top"
android:checked="true"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="98"
android:id="#+id/rb98"
android:layout_gravity="right|top"
android:layout_alignTop="#+id/rb95"
android:layout_alignLeft="#+id/rbs"
android:layout_alignStart="#+id/rbs"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="96"
android:id="#+id/rb96"
android:layout_gravity="left"
android:layout_below="#id/rb95"
android:layout_alignLeft="#id/rb95"
android:layout_alignStart="#id/rb95"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="soler"
android:id="#+id/rbs"
android:layout_gravity="right"
android:layout_marginRight="75dp"
android:layout_alignTop="#id/rb96"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
</RadioGroup>
Pull the RadioButtons out of the RelativeLayout (get completely rid of it: it's not only misplaced, but also useless) and they will work as expected.
If you really need the RelativeLayout as a container for the RadioGroup, then swap the RelativeLayout and RadioGroup (RelativeLayout on the outside)
I think your problem is that you have a relativeLayout inside your RadioGroup.
Here is the Google Developer page on radio buttons.
Related
I have radiogroup :
<RadioGroup android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:orientation="horizontal"
android:layout_width="match_parent"
android:background="#drawable/overlay_2"
android:id="#+id/rgSettings">`
<RadioButton android:layout_width="140dp"
android:layout_height="100dp"
android:id="#+id/s_mph"
android:background="#drawable/settings_speed_mph"
android:button="#android:color/transparent"
android:layout_gravity="center_vertical|left"/>
<RadioButton android:layout_width="140dp"
android:layout_height="100dp"
android:id="#+id/s_kph"
android:checked="false"
android:background="#drawable/settings_speed_kph"
android:button="#android:color/transparent"
android:layout_gravity="center"
android:gravity="right"/>
</RadioGroup>
Link in screenshot : http://joxi.ru/A5GhUxjKTJBQH7uRaK4
(1) radiogroup
(2) - (3) its radio buttons.
I need radibutton (2) set in position - left, radiobutton (3) set in right position. What do I need for this?
Add:
i need set first button set left position in radiogroup, secondary button set in right position radiogroup. Like This: joxi.ru/FqKhUxjKTJBsH5OWteQ
Why You are setting the button that You need on the left side as first? Just switch it:
<RadioGroup android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_width="match_parent"
android:background="#drawable/overlay_2"
android:id="#+id/rgSettings">
<RadioButton android:layout_width="140dp"
android:layout_height="100dp"
android:id="#+id/s_kph"
android:checked="false"
android:background="#drawable/settings_speed_kph"
android:button="#android:color/transparent"
android:layout_gravity="center_vertical|left"
android:gravity="right"/>
<RadioButton android:layout_width="140dp"
android:layout_height="100dp"
android:id="#+id/s_mph"
android:background="#drawable/settings_speed_mph"
android:button="#android:color/transparent"
android:layout_gravity="center_vertical|right"/>
</RadioGroup>
Also, You can play a little bit with the layout_margin attributes to make space between the buttons.
My idea is to have pairs ImageView - RadioButton, each pair enclosed in LinearLayout (horizontal orientation) and ImageButtons have their background color changed when an option is selected and confirmed. So, it would look like:
[LinearLayout1]: [ImageView1] [RadioButton1]
[LinearLayout2]: [ImageView2] [RadioButton2]
[LinearLayout3]: [ImageView3] [RadioButton3]
Is it possible to store this hierarchy within RadioGroup? I tried to apply this but see no ImageViews in my app. Or is RadioGroup built only for RadioButtons and all other views are ignored?
Yes, it's possible. If it's not working, there's something else wrong with your code.
A proof of concept layout based on the ApiDemos sample Android app included in the SDK:
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:checkedButton="#+id/lunch"
android:id="#+id/menu">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_group_1_breakfast"
android:id="#+id/breakfast"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/btn_star"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_group_1_lunch"
android:id="#id/lunch"/>
</LinearLayout>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_group_1_dinner"
android:id="#+id/dinner"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_group_1_all"
android:id="#+id/all"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_group_1_selection"
android:id="#+id/choice"/>
</RadioGroup>
and the layout it produces:
In my app, I have a dialog that contains a radiogroup with four radio buttons. My issue is that it looks good in emulator but when I install and run my app in phone, the radiobuttons get more space between each one of them. This makes the radiogroup stretch outside of my Dialog.
Please help me with this.
Thank you.
<RadioGroup
android:id="#+id/radio_RemindAtDlg"
android:layout_width="wrap_content"
android:layout_height="130dp"
android:layout_marginTop="110dp"
android:layout_marginLeft="20dp" >
<RadioButton
android:id="#+id/radio_OnceDlg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Once"
android:button="#drawable/radio_button"
android:textSize="12dp" />
<RadioButton
android:id="#+id/radio_WeekDlg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Every Week"
android:button="#drawable/radio_button"
android:textSize="12dp" />
<RadioButton
android:id="#+id/radio_MonthDlg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Every Month"
android:button="#drawable/radio_button"
android:textSize="12dp" />
<RadioButton
android:id="#+id/radio_YearDlg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Every Year"
android:button="#drawable/radio_button"
android:textSize="12dp" />
</RadioGroup>
Define your own radio buttons with your own sizes, shapes and spaces between components:
http://blog.devminded.com/posts/custom-android-radiobutton
another example
https://stackoverflow.com/a/17693303/1276374
Without code hard to tell but I would guess that you have the xml height attribute set to fill_parent instead of wrap_content
So I have the following radiobuttons. I want to have them display like this:
However this occurs:
How I can get it to display like above? I can move in the GUI editor in Eclipse it but it removes the RadioButton from the RadioGroup!Within the group, it ignores all other layout parameters.
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/timeBar"
android:layout_marginTop="43dp"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/privRadio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Everyone" />
<RadioButton
android:id="#+id/privRadio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="FriendOfFriends" />
<RadioButton
android:id="#+id/privRadio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Friends" />
</RadioGroup>
You can simply copy this class:
https://github.com/jevonbeck/AbstractMachine/blob/jevon_dev/app/src/main/java/org/ricts/abstractmachine/ui/utils/MultiLineRadioGroup.java
into an appropriate package in your project and instantiate in XML like so:
<view
class="mypackage.packagepath.MultiLineRadioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"/>
What you are asking for is a FlowLayout. Such a layout has the benefit of only wrapping when it's needed, as opposed to 0gravity's more "static" solution.
I have a textview on the left side and RadioGroup on the right side. Before I put my buttons in a radio group I had the buttons on the right side of the screen. What I am doing wrong after putting it in a RadioGroup?
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.25" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/radioButton1"
android:layout_alignParentLeft="true"
android:text="#string/hair"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="#string/Yes" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/radioButton1"
android:layout_below="#+id/radioButton1"
android:text="#string/No" />
</RadioGroup>
</RelativeLayout>
All of the "align" parameters that you have set on the buttons individually (like layout_alignParentRight) are no longer valid because the buttons are inside of a RadioGroup, which is a subclass of LinearLayout. In order to right-align the group as a whole, you need to add the proper parameters to the RadioGroup itself.
Also, you may want the RadioGroup width to wrap_content instead of fill_parent. Otherwise any horizontal layout alignments you do will likely not be visible with the container trying to fill up all available space.
HTH
you can also add this to your RadioGroup and it will work fine aswell!
android:gravity="center|left"