how to create radio button group view in that button like horizontal in pic, i tried xml file but does found proper tag in that....
this is my xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="RadioButton" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
</RadioGroup>
In your RadioGroup, set:
android:orientation="horizontal"
Because RadioGroup inherits from LinearLayout
The Android Documentation for Radio Groups mentions that it inherits from Linear Layout. Searching for 'orientation' turns up the android:orientation property.
The default is horizontal.
Must be one of the following constant values.
Constant Value Description
horizontal 0 Defines an horizontal widget.
vertical 1 Defines a vertical widget.
This corresponds to the global attribute resource symbol orientation.
So simply add this property to Radio Group:
android:orientation="horizontal"
Related
I am using two linear layouts to display two rows of radio button 3 in one row and 3 in other row.I have searched and found that we should use radio group. But after using radio group also I am not able to uncheck the radio button when I select a radio button
<RadioGroup
android:id="#+id/payment_mode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
style="#style/LinkButton"
android:id="#+id/cash"
android:text="CASH" />
<RadioButton
style="#style/LinkButton"
android:id="#+id/card"
android:text="CARD" />
<RadioButton
style="#style/LinkButton"
android:id="#+id/pay_tm"
android:text="PAYTM" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<RadioButton
style="#style/LinkButton"
android:id="#+id/bank"
android:text="BANK" />
<RadioButton
style="#style/LinkButton"
android:id="#+id/cc_av"
android:text="CC-AV" />
<RadioButton
style="#style/LinkButton"
android:id="#+id/credit"
android:text="CREDIT" />
</LinearLayout>
</RadioGroup>
From the developers website
To create each radio button option, create a RadioButton in your
layout. However, because radio buttons are mutually exclusive, you
must group them together inside a RadioGroup. By grouping them
together, the system ensures that only one radio button can be
selected at a time.
So the direct parent of the radio button has to be the RadioGroup , in you layout file the direct parent is the LinearLayout. Remove the LinearLayout and place the radiobuttons directly under the RadioGroup.
You can't use any layout inside RadioGroup. You can use only
RadioButton, otherwise RadioButton won't work. RadioButton
should be immediate child of RadioGroup.
Your problem will be solved by changing the orientation of the radioGroup to horizontal.
More info here
For my answer you need not to use the radio group. You just have to set click listener on each radio button. For eg.
RadioButton tempRadioButton; //global variable.
radioButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (tempRadioButton != null){
tempRadioButton.setChecked(false);
}
tempRadioButton = (RadioButton) view;
}
});
You can also use this method to set the radio buttons on each item of list view or recyclerview.
Try to change your layout like this and do on click action refer this link:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioGroup
android:id="#+id/payment_mode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:orientation="vertical">
<RadioButton
android:id="#+id/cash"
style="#style/LinkButton"
android:text="CASH" />
<RadioButton
android:id="#+id/card"
style="#style/LinkButton"
android:text="CARD" />
<RadioButton
android:id="#+id/pay_tm"
style="#style/LinkButton"
android:text="PAYTM" />
<RadioButton
android:id="#+id/bank"
style="#style/LinkButton"
android:text="BANK" />
<RadioButton
android:id="#+id/cc_av"
style="#style/LinkButton"
android:text="CC-AV" />
<RadioButton
android:id="#+id/credit"
style="#style/LinkButton"
android:text="CREDIT" />
</RadioGroup>
</LinearLayout>
Is it possible to create a RadioGroup that will hold radio buttons by 3 in a row? I've checked the internet for an implementation and I found one, but it was not working. The thing is that I need to add them programatically, and they should be grouped 3 per row.
Can someone tell me how can I populate a radioGroup with RadioButtons so they are aligned 3 per row.
Thanks
Radiogroup inherits from LinearLayout.
So, set the RadioGroup orientation attribute to "horizontal"
Radio Group inherits from the LinearLayout ViewGroup so you can make this possible by setting orientation on the viewgroup android:orientation="horizontal"
<RadioGroup
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RadioButton
android:text="Radio 1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:text="Radio 2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:text="Radio 3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
I have a RadioGroup with radio buttons:
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/radioGroup1"
>
<RadioButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:checked="true"
android:text="AAA"
android:button="#null"
android:drawableLeft="#drawable/flag_icon"
android:drawableRight="#android:drawable/btn_radio"
android:drawablePadding="60dp"
android:id="#+id/radio2"/>
<RadioButton.../>
</RadioGroup>
As you see above, for the RadioButton I defined the following attributes:
...
android:button="#null"
android:drawableLeft="#drawable/flag_icon"
android:drawableRight="#android:drawable/btn_radio"
...
So that the radio button is located on the right side of the radio button text. The left side of button text is a flag icon.
In order to have spaces between button text and flag icon and button icon, I use android:drawablePadding="60dp" to space them.
However, with above code, the 60dp padding space is applied to both left drawable (flag icon) and right drawable (radio button), which ends up with radio text located in the middle between the two drawables.
I would like to have radio button text and left drawable(flag icon) has smaller space between, while radio button text and right drawable (radio button) has larger space between. How to define the different spaces for left drawable and right drawable of radio button ?
Simply check this:
<RadioButton
android:id="#+id/radio0"
style="#style/my_recording_title_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#null"
android:button="#null"
android:checked="true"
android:drawableRight="#drawable/presets_sel"
android:padding="12dp"
android:text="Play with EarPhones" />
<View
android:layout_width="fill_parent"
android:layout_height="3dp"
android:background="#drawable/line" />
<RadioButton
android:id="#+id/radio1"
style="#style/my_recording_title_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#null"
android:button="#null"
android:drawableRight="#drawable/presets_sel"
android:padding="12dp"
android:text="Play with internal speakers" />
</RadioGroup>
try this xml file or add Property android:gravity="center_horizontal|center_vertical" or android:gravity="center" in your Radio Button and remove padding
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/logo"
android:button="#null"
android:drawableRight="#drawable/ic_launcher"
android:gravity="center_horizontal|center_vertical"
android:text="RadioButton" />
</RadioGroup>
</LinearLayout>
This sort of thing comes up often. And the sad truth is that the limitations of using RadioGroup with RadioButtons severely limit your ability to make your layouts look right.
I posted a very detailed solution here that involves abandoning RadioGroups. I know it seems like a lot of work, but the code is very straight-forward. Until RadioGroups are implemented in a better fashion, this is the best solution I've found.
Good luck!
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"
Greetings,
I'm trying to create a single-choice android control, in a horizontal layout, by making use of the RadioGroup behaviour. I can assign the drawable just fine, but i would like to position the label of each RadioButton inside the drawable, is this possible using the standard APIs?
<RadioGroup
android:id="#+id/switchcontainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:checkedButton="#+id/RadioButton02"
android:padding="3dip">
<RadioButton
android:text="id RadioButton02"
android:id="#+id/RadioButton02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/radio_button"
android:paddingRight="2dip" />
<RadioButton
android:text="#+id/RadioButton03"
android:id="#+id/RadioButton03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/radio_button"
android:paddingRight="2dip" />>
</RadioGroup>
Okay, i found a way.
Just use #null in the attribute button, and move the drawable reference to the attribute background like this:
<RadioGroup
android:id="#+id/switchcontainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:checkedButton="#+id/RadioButton02">
<RadioButton
android:text="id RadioButton02"
android:id="#+id/RadioButton02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/radio_button"
android:button="#null"/>
<RadioButton
android:text="#+id/RadioButton03"
android:id="#+id/RadioButton03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/radio_button"
android:button="#null"/>
</RadioGroup>
I'm not sure about with the RadioGroup; however, it should be possible to accomplish this with a ListView.
Set the ListView choiceMode to singleChoice.
Provide an ArrayAdapter or SimpleAdapter with a custom layout file for the row. This allows you to configure the position of the label.
For a quick intro to ListView's, see Hello ListView