Uncheck radio button if already checked? - android

I have a RadioGroup with 6 radio buttons inside it.
Is there a way to uncheck the radio buttons if the user click on an already checked radio button?
I mean is there any way to clear check the whole radio group or do not store the value if user clicks on an already checked radio button?
i know there exist ClearCheck() and setChecked(false), but i don't know how and where to use them because SetOncheckChanged listener only runs when the check state changes. It does not run when you click on an already checked radio button. I think I should use SetOnClickListener for radiogroup but i don't know how to find which radiobutton is checked?

Use ::
// if we already have a checked radiobutton and want to remember it
if(mCurrentlyCheckedRB == null)
mCurrentlyCheckedRB = (RadioButton) v;
mCurrentlyCheckedRB.setChecked(true);
}
//clicked the current or desire one to check it that allready stored
if(mCurrentlyCheckedRB == v)
return;
// else, uncheck the currently checked RadioButton, check the new radio button
mCurrentlyCheckedRB.setChecked(false);
((RadioButton)v).setChecked(true);
mCurrentlyCheckedRB = (RadioButton)v;

Related

Android radio button click already checked button

I've got an activity that lets users choose a "type" of a thing using radio buttons, but when modifying that thing I want to pre-check the radio button for the type the thing already is, and allow the user to click that checked radio button again if they don't want to change the type before moving to the next activity. I want to pre-check because users might not remember which type the thing was when they go to modify it.
The problem is that the listener only triggers when the option changes, not when the user clicks the option that's already checked. That makes sense, seeing as the listener is called "OnCheckedChangeListener," but when I try adding an OnClick listener to catch the click of a checked button, nothing happens. It simply doesn't trigger, and I don't see any other listeners on the RadioGroup object that seem like they'd do what I want.
Here's the checked change listener, which works fine:
rgThingType.setOnCheckedChangeListener { radioGroup, i ->
val checkedRadioButton = radioGroup?.findViewById(i) as? RadioButton
when(checkedRadioButton?.id) {
// ...
}
goToActivity()
}
And here's the on click listener, which never triggers no matter what:
rgThingType.setOnClickListener {
goToActivity()
}
Now, I can add an OnClick to each radio button to accomplish this, ie:
rbThingType1.setOnClickListener {
thingType = 1
if(/* extra logic to prevent double activity start */) {
goToActivity()
}
}
but that is awkward and seems like an anti-pattern, given I already have the checked change listener, and in fact it screws things up as it causes the next activity to be triggered twice since both listeners get called, forcing me to keep/check extra state to avoid. Is there a proper way to do this?
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);
radioGroup.setOnClickListener(v -> {
// get selected radio button from radioGroup
int selectedId = radioGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioButton = findViewById(selectedId);
String slectedValue=radioButton.getText()
});

Android Studio: Using CheckBox to set EditText enabled

In android studio, I am attempting to use a CheckBox to control whether a numerical EditText is enabled or disabled. The relevant code when the checkbox is clicked:
boolean checked = ((CheckBox) view).isChecked();
EditText yzEdit = (EditText) findViewById(R.id.yzEdit);
// Check which checkbox was clicked
switch(view.getId()) {
case R.id.yzCheck:
if (checked){
yzEdit.setBackgroundColor(Color.parseColor("#FFFFFF"));
yzEdit.setTextIsSelectable(true);
yzEdit.setFocusable(true);
yzEdit.setFocusableInTouchMode(true);
yzEdit.setCursorVisible(true);
yzEdit.setEnabled(true);
}
else{
yzEdit.setBackgroundColor(Color.parseColor("#CCCCCC"));
yzEdit.setTextIsSelectable(false);
yzEdit.setFocusable(false);
yzEdit.setFocusableInTouchMode(false);
yzEdit.setCursorVisible(false);
yzEdit.setEnabled(false);
}
break;
}
Everything almost works. However, when I check the box, enabling yzEdit, then click on yzEdit, no user numerical keyboard pops up directly. So the user cannot enter any numbers into the newly enabled EditText (except in a roundout way of focusing on it through "Next" from a previous EditText.
Which property am I looking for that controls this behavior?
You need to use a listener on your checkbox, if you want to detect if an object has changed his state.
For your case, it's setOnCheckedChangeListener() to use on your checkbox.
You can take a look here : http://developer.android.com/reference/android/widget/CompoundButton.html
and :
Android: checkbox listener

how can i set RadioGroup unchecked by default in android?

I am doing an app to conduct simple quiz.I have given the answers are multiple choice using RadioGroup. So on loading each question how can I set RadioGroup unchecked by default ?
Just set the checked Property in your XML for every RadioButton to false:
android:checked="false"
That should solve the issue.
Have you looked on the android developer guide?
http://developer.android.com/reference/android/widget/RadioGroup.html
Intially, all of the radio buttons are unchecked.
Although you can do this in code with the clearCheck() method
Clears the selection. When the selection is cleared, no radio button in this group is selected and getCheckedRadioButtonId() returns null.
Perhaps your problem might be a different from my understanding. Perhaps explain more about how you are loading questions and so forth.
Try
RadioGroup.check(0)
0 is an id which will nevere be generated by adt
upd: from source of RadioGroup
public class RadioGroup extends LinearLayout {
// holds the checked id; the selection is empty by default
private int mCheckedId = -1;
...

Possible to allow all radio buttons to be unchecked in an Android app?

Just wanted to see if anyone knows if there's a radiogroup or radiobutton attribute or something else quick that will allow radio buttons to be unchecked when they're in checked mode. I'm looking to build functionality that works like a radio group (i.e. only one can be checked) but I also want them to be able to be all unchecked.
Maybe I don't get the question here, but this is something I wanted to do. I have a single activity that I use to classify many pictures. I am classifying using radio buttons. After the user checks one of the options he is allowed to switch with the next picture. I needed to clear the selection, when switching the picture, but decided not to create new activity.
So I initialize my radio group in the layout like that:
<RadioGroup
android:id="#+id/radio_selection"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/radio_true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="#string/true" />
<RadioButton
android:id="#+id/radio_false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="#string/false" />
</RadioGroup>
This initializes my radio group and both RadioButton are not selected initially. Afterwards when I change the picture I need to clear the selection (because user has not yet chosen for the new picture). I do like that:
RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radio_selection);
radioGroup.clearCheck();
This is doing exactly what I need: making again none of the radio buttons being selected. I hope I understood the question and this will help somebody in the future.
You can use a CheckBox to mimic the functionality you want as shown below. The code assumes that you have two check boxes but you could have more than two.
public void onClick(View v) {
int id = v.getId();
if (id == R.id.checkBox1) {
// Toggle status of checkbox selection
checkBox1Selected = checkBox1.isChecked();
// Ensure that other checkboxes are not selected
if (checkBox2Selected) {
checkBox2.setChecked(false);
checkBox2Selected = false;
}
else if (id == R.id.checkBox2) {
// Toggle status of checkbox selection
checkBox2Selected = checkBox2.isChecked();
// Ensure that other checkboxes are not selected
if (checkBox1Selected) {
checkBox1.setChecked(false);
checkBox1Selected = false;
}
}

Android RadioButton not able to unset using setChecked(false) method

If I set a radio button to be selected on the first time, it works fine. But if I unselect it by calling
((RadioButton) findViewById(R.id.ID)).setChecked(false);
then, later even if I try to make it selected by calling setChecked(true) will not work unless the user select it from the screen.
Have any one come across this? or is it only me?
if(Val != null){
if( ((RadioButton) findViewById(R.id.ID1)).getText().toString().trim().equals(Val))
((RadioButton) findViewById(R.id.ID1)).setChecked(true);
else if(((RadioButton) findViewById(R.id.ID2)).getText().toString().trim().equals(Val))
((RadioButton) findViewById(R.id.ID2)).setChecked(true);
}
else {
((RadioButton) findViewById(R.id.ID1)).setChecked(false);
((RadioButton) findViewById(R.id.ID2)).setChecked(false);
}
If the else part is executed atleast once then everything gets mess up.
When I step thro my debugger, I can see the execution goes in the correct path and setting it to true. It is getting executed only once, I checked that. And I am not resetting it back to false in any other part of the code.
I found the solution.
It is not possible to uncheck a particular radio button. You can only set the other item to true.
So to clear all the checked items, you should call the clearcheck() method on the RadioGroup.
So my else part is
else {
((RadioGroup) findViewById(R.id.ID0)).clearCheck();
}
Take one invisible radio button and check it. All other radio buttons of group will be unchecked automatically..

Categories

Resources