Radio Button does not toggle its state - android

I add a RadioButton in my layout.
It is unchecked to begin with. And when I click on it , it becomes checked (as shown in emulator). But when when i click on it again, it does not become unchecked again?
<RadioButton android:checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/option1"/>

If you're looking for checkbox behavior with radio button appearance, you could pass in the xml style to a checkbox.
<CheckBox android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/option1"
style="#android:style/Widget.DeviceDefault.Light.CompoundButton.RadioButton/>
This can be useful in some cases (ex. using radio buttons in a RecyclerView) but you should be careful because the user expects radio buttons to behave a certain way. If you're allowing the user to make multiple selections you should probably use a normal checkbox, as mentioned in the comments above.
Hope this helps!

If you are only using one radio box for checking on and off, maybe you should use checkbox or toggle button instead.
http://developer.android.com/resources/tutorials/views/hello-formstuff.html
Scroll down and see checkbox and toggle button.
When using radios you usually have more than one and choose between them. Like easy, medium, hard.

If you are having more than 1 radio buttons to work with then add "RadioGroups" as follows:
<RadioGroup android:id="#+id/group1" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="vertical">
<RadioButton android:id="#+id/radio1" android:text="madras"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<RadioButton android:id="#+id/radio2" android:text="bombay"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</RadioGroup>
Have a look at this example , i am sure this will make your idea clear regarding Radio Buttons.
Also refer this Android Developer - Form Stuff page .

After searching a lot on SO, I came up with a not-so-good but decent workaround.
Declare a boolean variable for each RadioButton you have, initialize it with false, and change the state of the variable and the RadioButton at every click.
boolean isToggledRadio1 = false;
RadioButton radio1 = (RadioButton) findViewById(R.id.radiobutton1);
radio1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
isToggledRadio1 = !isToggledRadio1; //Switch boolean value
RadioButton rb = (RadioButton)v;
rb.setChecked( isToggledRadio1 );
}
});
I know it's ideal to use a Checkbox, but if someone needs the radiobutton, then they need the radiobutton.
This is not an optimal solution, as it will basically toggle the button twice every time the user clicks the button (one is the default behaviour, the second time is inside your onclick function), so if you're using an OnCheckedChangeListener you will probably get two calls for the same click.
There's another workaround, which is to change the android:button in a checkbox to another drawable with an xml template, but it's a bit more complex, requires at least 2 more files for the states.

That is a conceptual question: Radio buttons allow you to choose between several options (represented by the other radio buttons). Typically, one radio button out of a group is always checked, even if in an initial state no buttons may be checked if you do not declare one as a default value. That means also that a single button does not allow to toggle its state unless other radio buttons are present in the same group - if there is only one option, you will have to choose it.
If you want a binary toggle, you will want to use a checkbox instead.

Solution is set checked[true/false] intermediate in Java code
ToggleButton t = (ToggleButton) findViewById(R.id.toggle_button);
t.setChecked(true);
// t.setChecked(false);

Related

RadioButton checked programmatically won't uncheck

I have these RadioButtons set up within a RadioGroup:
<RadioGroup
android:id="#+id/radioGroupLeakTight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:text="#string/action_yes"
android:id="#+id/radioLeakTightYes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:text="#string/action_no"
android:id="#+id/radioLeakTightNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RadioGroup>
With the code below I try to toggle one of the RadioButtons through code:
radioGroupLeakTight.check((currentTask.isLeakTight() ? R.id.radioLeakTightYes : R.id.radioLeakTightNo));
If I don't use the above code the RadioButtons work as intended: whenever one gets checked, the other one gets unchecked. If I use the code to pre-check one of the RadioButtons this functionality gets lost. Whenever I try to check a RadioButton the other one remains checked.
I've read solutions where I should implement the 'OnCheckedChangeListener' and toggle them myself. But I guess that's just covering up something else that doesn't work, no? I've also read people that had these issues, but they had the RadioGroup missing, which I clearly have.
What am I doing wrong? Is the only solution really implementing 'OnCheckedChangeListener' and toggling the other RadioButtons myself?
UPDATE 08/12/2016:
So I've continued my search and implemented 'OnCheckedChanged'. What I see now is that whenever I check one of the RadioButtons this event is never fired. The listener is set on the 2 RadioGroups.
I'm starting to suspect these RadioButtons lose their membership to the RadioGroup? Whenever I check them through code (after I set my listeners) the event is fired successfully.
Use clearCheck() for RadioGroups. Learn more.
If you need to check another RadioButton, you can do so after this.
Whenever I try to check a RadioButton, the other one remains checked.
RadioButton radioLeakTightYes = (RadioButton) findViewById(...);
RadioButton radioLeakTightNo = (RadioButton) findViewById(...);
radioGroupLeakTight.clearCheck();
if(currentTask.isLeakTight()){
radioGroupLeakTight.check(radioLeakTightYes.getId());
} else {
radioGroupLeakTight.check(radioLeakTightNo.getId());
}

android: different textvies and edittexts depending on radiobuttons

I'm starting to learn a bit about android programming and watch plenty of guides/tutorials but there's one question none of the guides answer. I have a radio group of two buttons, simply like this(in the xml):
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="button1" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button2" />
</RadioGroup>
The thing is that i want to show different textviews and different edittexts depending on what radio button is clicked, does anybody know where i can find a good example about this or might even wanna make an example yourself?
Thanks
You can simply wrap your textview's of type1 with some parent View like LinearLayout and so on for the other textview's types.
Than simply onCheckedChanged(RadioGroup arg0, int checkedId) show and hide the wrappers views.
The easiest way to do it is to just add everything to your main view. Then, depending on which is clicked, set the visibility of all the items you don't want to see to View.GONE, and those you do to View.INVISIBLE. Or if you have something more complex and the two views don't share a lot of variables, implementing them as fragments may make sense.

Is it possible to make a checkbox/radio button uncheckable?

The program I am making has several views in which the user is trying to basically figure out a lot of boolean values. Sounds strange, but what I mean is that my device will connect to another device via bluetooth, and then read the status of that device. I basically display a checklist of the current status of that device. So depending on what that device is doing, different checkboxes are checked. Is there a way to make all of the checkboxes unselectable?
Even better, is there a way to hardcode this into the XML file?
Have you considered using android:clickable for an xml attribute for your checkboxes?
From the documentation:
Defines whether this view reacts to click events.
If it cannot react to click events, then it effectively becomes uncheckable.
<CheckBox
android:id="#+id/blahblah"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false" />
and in your code you can call cbox.setChecked(boolean)
to expand on what crocboy and nicholas.hauschild said.
Setting this in you XML
android:enabled="false"
will make the button/checkbox be grayed-out, to show it is disabled
Another way to do this is to set it to:
android:clickable="false"
Here is the code to make a RadioButton that is red, that turns green when checked (in your code), but can not be checked by the user.
android:checked="false"
android:textColor="#d1d2d4"
android:buttonTint="#d1202d"
android:textColorHighlight="#248d51"
android:clickable="false"
AND here is the code to check it (this is using JSON: so it is checking if english=="yes")
String yes = "yes";
if(yourObject.english.equals(yes)) {
if (m_english.isChecked()) {/*do nothing */}
else m_english.toggle();
}
Check this out: http://developer.android.com/reference/android/widget/CheckBox.html
You can use checkbox.setEnabled(false) to make it unselectable.
Try this in your XML: android:enabled="false"
It's supposedly deprecated, but may work depending on your traget SDK.
Hope that helps!

how to show that the radio button is selected permanently

i have the same problem as
how to show the radio button is selected is still ticked? but in android platform... can anybody help me please..
my codes are
<RadioButton
android:checked="false"
android:id="#+id/BBC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffff6600"
android:text="BBC NEWS"
android:typeface="serif"
android:textStyle="bold"
android:layout_x="10px"
android:layout_y="100px">
</RadioButton>
Thanks in advance...
You could save the value (when changed) to SharedPreferences, read it each time the page is loaded, and set the checked state based on that.
You can use a static variable like is_ticked & set its value true/false when user tick it. and check value of this variable in onCreate() if(is_ticked) set radio button to checked else unchecked .
A question with similar content was asked yesterday:
I have radio button for the user to choose if he/she wants to keep his/her account logged in.
you can't solve this problem just with the layout.xml - you need to save the status of the radio button and load it again once the appropriate activity is started
You could use this:
mListView.setItemChecked(selectedPosition, true);//this will check(enable) the desired radio button.
selectedPosition, you can save using SharedPreferences.

Custom View in android/

I need to make a custom view as shown in diagram, which needs to functional like radio button, any of the options needs to be selected. How can I achieve this? I don`t want to use series of buttons.
You can use RadioGroup and RadioButtons to achieve this. For example,
<RadioButton
android:id="#+id/custom_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:button="#null"
android:background="#drawable/button_custom"/>
Where button_custom is the background image of the button shown. This image should have all the variations (like, normal, pressed, enabled, disabled, selected).

Categories

Resources