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..
Related
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()
});
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
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;
When my UI is recreated on orientation change, I use super.onCreate(savedInstanceState) and getLastNonConfigurationInstance() to access custom data I stored to fill the dynamic parts of my layout.
I have a RadioGroup which has two RadioButtons and is already defined in the XML file. The XML automatically makes the first one selected.
When an orientation change happens and the SECOND RadioButton is selected, everything seems to work fine; the second RadioButton is still selected in the UI.
But RadioGroup.getCheckedRadioButtonId() says the first RadioButton is selected. And I actually WANT the first one to be selected. But nothing changes when I call rb1.setChecked(true) - second one still shown as selected, and still the RadioGroup tells me the first one is selected (now it would make sense, but it's not shown).
This is REALLY strange behavior, does anyone have tips?
(edit)
Parts of my code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.itemselected);
(...)
radioGroupServingType = (RadioGroup) findViewById(R.id.radioGroupServingType);
(...)
RadioButton radioOwnServing = (RadioButton) findViewById(R.id.radioOwnServing);
RadioButton radioUseServing = (RadioButton) findViewById(R.id.radioUseServing);
radioOwnServing.setOnClickListener(this);
radioUseServing.setOnClickListener(this);
//FIXME WTF
Log.d("", radioOwnServing.isChecked()+"/"+radioUseServing.isChecked()+" own/use checked");
radioOwnServing.setChecked(true);
Log.d("", radioOwnServing.isChecked()+"/"+radioUseServing.isChecked()+" own/use checked");
If I select radioUseServing in the UI and change the orientation, the log says true/false own/use checked both times - although radioUseServing is shown as selected in the UI.
By the way, logcat also outputs
W/asset(4040): deep redirect failure from 0x0103003e => 0x02060007, defStyleAttr=0x0101007e, defStyleRes=0x0103001a, style=0x00000000
when changing the orientation, sometimes multiple times. I haven't found anything with google on what that means.
I already spoke with you on IRC, but I believe that if you get the view, and post a runnable to it that will call .setChecked(), this will in effect cause the setChecked call to occur at the proper time, and thus avoiding having called setChecked before the view hierarchy was ready for it.
Something like this:
final View myRadioButton = findViewById(R.id.myradiobutton);
myRadioButton.post(
new Runnable() {
#Override
public void run() { myRadioButton.setChecked(true); }
}
);
I'm having an issue with RadioGroup's clearChecked(). I'm displaying a multiple choice question to the user and after the user selects an answer I check the answer, give him some feedback and then move to the next question. In the process of moving to the next question I clearCheck on the RadioGroup.
Can anyone explain to me why the onCheckedChanged method is called 3 times? Once when the change actually occurs (with the user changes), once when I clearCheck(with -1 as the selected id) and once in between (with the user changes again)?
As far as I could tell the second trigger is provoked by clearCheck. Code below:
private void checkAnswer(RadioGroup group, int checkedId){
// this makes sure it doesn't blow up when the check is cleared
// also we don't check the answer when there is no answer
if (checkedId == -1) return;
if (group.getCheckedRadioButtonId() == -1) return;
// check if correct answer
if (checkedId == validAnswerId){
score++;
this.giveFeedBack(feedBackType.GOOD);
} else {
this.giveFeedBack(feedBackType.BAD);
}
// allow for user to see feedback and move to next question
h.postDelayed(this, 800);
}
private void changeToQuestion(int questionNumber){
if (questionNumber >= this.questionSet.size()){
// means we are past the question set
// we're going to the score activity
this.goToScoreActivity();
return;
}
//clearing the check
gr.clearCheck();
// give change the feedback back to question
imgFeedback.setImageResource(R.drawable.question_mark); //OTHER CODE HERE
}
and the run method looks like this
public void run() {
questionNumber++;
changeToQuestion(questionNumber);
}
What I've discovered is that if an item is checked and you call clearCheck() on the radio group it will call onCheckedChanged twice. The first time with the id of the item that was checked and the second time with -1/View.NO_ID. IMHO, this is a bug and apparently it has been around since at least 1.6. See this google code bug report: http://code.google.com/p/android/issues/detail?id=4785
It seems to be that the only solution is to check the actual RadioButton.isChecked() and test if it is true or false. This sort of defeats the purpose of the onCheckedChanged returning the id of the item since you now have to either keep references to those buttons or call findViewById every time.
I doubt they will fix this since changing it would probably break existing code in unexpected ways.
I faced the same problem and i solved with other work-around.
Set CheckedChangeListener as NULL
Do your operation
Reset your OnCheckedChangeListener again
Code snnipet :
rdbGroup.setOnCheckedChangeListener(null);
rdbGroup.clearCheck();
rdbGroup.setOnCheckedChangeListener(checkedChangeListener);
Hope this will help ツ
I had similar problem. My solution:
in procedure:
public void onCheckedChanged(RadioGroup rGroup, int checkedId)
I check checkedId. It equals to -1 if you use clearCheck()
else it equals to selected radiogroup child