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>
Related
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>
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"
I have two radio button in a radio group. I also have 2 androd:button checkbox- for when the radio button is deselected and checkbox_v for when he user selects the checkbox. I also implemnted that a method onRadioButtonClick in order to make sure that only one radio button had drawable:checkbox and the other has checkbox_v . How can I implemnt onRadioClick to do this? any idea?
public void onRadioButtonClick(View v)
{
RadioButton button = (RadioButton) v;
boolean checkBox1Selected;
boolean checkBox2Selected = false;
Toast.makeText(MainActivity.this,
button.getId()+ " was chosen."+R.id.wificheckBox,
Toast.LENGTH_SHORT).show();
if ( button.getId() ==R.id.wificheckBox) {
// Toggle status of checkbox selection
checkBox1Selected = radiowifiButton.isChecked();
// Ensure that other checkboxes are not selected
if (checkBox2Selected) {
radiomobileButton.setChecked(false);
checkBox2Selected = false;
}
else if (button.getId() ==R.id.wifimobilecheckBox) {
// Toggle status of checkbox selection
checkBox2Selected = radiomobileButton.isChecked();
// Ensure that other checkboxes are not selected
if (checkBox1Selected) {
radiowifiButton.setChecked(false);
checkBox1Selected = false;
}
}
}
main xml
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/ll_1"
android:layout_marginLeft="20dp">
<LinearLayout
android:id="#+id/ll_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/ll_1"
android:layout_marginLeft="20dp"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/wifimobilecheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/button_radio"
android:checked="true"
android:onClick="onRadioButtonClick" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/wificheckBox"
android:layout_toRightOf="#+id/wificheckBox"
android:paddingLeft="15dp"
android:text="WiFi or mobile network"
android:textColor="#333333"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/ll_2"
android:paddingTop="20dp"
android:layout_marginLeft="20dp"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/wificheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/button_radio"
android:onClick="onRadioButtonClick" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/wificheckBox"
android:layout_toRightOf="#+id/wificheckBox"
android:paddingLeft="15dp"
android:text="WiFi "
android:textColor="#333333"
android:textSize="20dp" />
</LinearLayout>
</RadioGroup>
Drawabe- button_radio
<item android:state_checked="true"android:state_pressed="false" android:drawable="#drawable/checkbox_v"/><item android:state_checked="false"android:state_pressed="false"'android:drawable="#drawable/checkbox"/>
If they are in a radiogroup, only one can be selected at a time. If you want both to be able to be selected, remove them from the radiogroup.
As the top voted answer didn't work for me as for many others. I am sharing the method that i used and its working perfectly.
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkedButton="#+id/rb_female"
android:orientation="horizontal">
<RadioButton
android:id="#+id/rb_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:button="#drawable/selector_radio_button"
android:text="Female" />
<RadioButton
android:id="#+id/rb_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/selector_radio_button"
android:text="Male" />
</RadioGroup>
Setting the android:checkedButton="#+id/rb_female" worked for me in RadioGroup.
You should remove your onRadioButtonClick method and add OnCheckedChangeListener to your radiogroup. http://developer.android.com/reference/android/widget/RadioGroup.OnCheckedChangeListener.html
I guess by implementing your own click handler your are overwriting some functionality of the radiogroup.
Your requirement is not clear. Would you mind to make it simpler. I tried my best to understand your requirements and these are the findings given below :(.
A radio button can have text. So you don't have to add another TextView in your layout. Please remove them and add a simple android:text="blah.. blah.." to your radiobutton.
With my understanding you have two doubts.
How to customize RadioButtons with custom drawables??
Checkout the answer here.
How to deselect the previously selected radiobutton??
The truth is you don't have to do it manually as you are keeping those radiobuttons into a single radiogroup.
Hope this will help you.
To check/uncheck radio buttons not part of a RadioGroup,
add a method in the android GUI designer under the
RadioButton Properties 'onClick' (In this example radio1_onClick).
In your activity.xml under the xml for the RadioButton
RadioButton
[blah blah blah]
android:id="#+id/radio1"
android:onClick="radio1_onClick"
In your activity_main.xml you would write the following method.
private void radio1_onClick(View v)
{
CompoundButton b=(CompoundButton)findViewById(R.id.radio1);
b.setChecked(true); /* light the button */
//b.setChecked(false); /* unlight the button */
}
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"
I have radio buttons in an application I am creating. When the user clicks on one option, I want the other radio buttons to be unclickable.
I'm not sure how to do this, the radio buttons are set in an onclick listener and the logic is set within on click.
What about calling setEnabled(false) on the rest of the buttons? If there are too many buttons you can consider putting in an array:
RadioButton[] buttons; //you put here all buttons..
// then, when you catch the click, call a method like this
private void disableOtherButtons(RadioButton buttonClicked)
for(RadioButton button : buttons){
if( button != buttonClicked ){
button.setEnabled(false);
}
}
}
If your using XML you can group them using RadioGroup, in the following example only one of the radio buttons is selectable at a time.
<RadioGroup
android:id="#+id/rg_configup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
>
<RadioButton
android:id="#+id/rb_configup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sales"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
>
</RadioButton>
<RadioButton
android:id="#+id/rb_configup2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Regional"
android:layout_above="#+id/rb_configup1"
android:layout_alignLeft="#+id/rb_configup1"
>
</RadioButton>
<RadioButton
android:id="#+id/rb_configup3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Global"
android:layout_above="#+id/rb_configup2"
android:layout_alignLeft="#+id/rb_configup1"
>
</RadioButton>
<RadioButton
android:id="#+id/rb_configup4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ad-hoc"
android:layout_above="#+id/rb_configup3"
android:layout_alignLeft="#+id/rb_configup1"
>
</RadioButton>
</RadioGroup>