2

In my Android Application it's required to give a notification(warning) message before user change Radio Group Selection.

Basically I am trying to give a dialog message (that have warning message and YES/NO buttons to confirm change) when user click any other radio button within that Radio groiup. If user presses YES then to change readio button selection with what user wanted to change, otherwise (if press NO) to select back with what was selected before.

I don't see any option in onCheckedChanged method when listen to OnCheckedChangeListener. What would be the best way to do this. Thanks

EDITS

I'm providing the warning message from a Dialog within onCheckedChanged method with option to Confirm the change (with YES/NO buttons). Problem is when user don't want the new selection after seeing the warning, I need a way to know the prevoiusly selected Radio Button to revert user's new selection.

public void onCheckedChanged(RadioGroup group, int checkedId) 

The above method provides "checkedId" but how to know which Radio Button had selected before. It's required when user want to Cancel change..

Can do this by tracking the ealier selection defining a class level variable to hold old Radio button ID or some other logic like that. But there should be a straight forward way in Radio group to cancel new selection (by selecting old one back). I looked for beforeRadioCheckChange Type listener but didn't manage to find

JibW
  • 4,538
  • 17
  • 66
  • 101
  • 2
    Create and show a dialog in `onCheckedChange` and depending on what they press either let the check go through or uncheck it. This is pretty unclear exactly what you are stuck on. – codeMagic Aug 15 '16 at 16:20
  • Yeah, seems very easy to do this cM; – Eenvincible Aug 15 '16 at 16:30
  • Thanks codeMagic and Eenvincible. I've tried the same thing in "onCheckedChanged" method. That's, loading the dialog with warning message with YES/NO buttons to confirm or cancel radio button selection change. But the problem is when user press NO I just want to select the previously selected radio button. Basically looking for a way to cancel new selection and to select earlier selected radio button. Can do it by defining a class level variable to hold old selection and a logic like that. But wonder if there is a straight forward way to cancel new selection and to select old one back. Thanks – JibW Aug 16 '16 at 09:18
  • Solutions mentioned below are causing the onChange to refire as they are detected as a change. Need either a auto revert mechanism or a confirm enabled change – Rahul Kahale Sep 12 '20 at 14:58

2 Answers2

0

You can store selected button before (when you actually set which one of radio buttons should be checked initially):

int selectedButton = mRadioGroup.getCheckedRadioButtonId();

And update it inside onCheckedChanged callback:

selectedButton = group.getCheckedRadioButtonId();

when user confirms change. Otherwise return checked state to the last selectedButton.

Anton Malyshev
  • 8,686
  • 2
  • 27
  • 45
0

there is no a method in RadioGroup that enables you to revert your action, however you can easily achieve this by holding the id of selected RadioButton in an outside variable, and use it to revert your changes back:

RadioGroup radio;
static int buttonChecked;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    radio = (RadioGroup) findViewById(R.id.radioGroup);
    buttonChecked = radio.getCheckedRadioButtonId(); // be sure to get the checked button id first time before assigning the onCheckedChange
    radio.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // Raise Dialog
            if(yes){ // User confirmeed
                buttonChecked = checkedId;
            }else{ // User refused
                if(buttonChecked != -1) // to prevent app from crashing if nothing is already selected
                    radio.check(buttonChecked);
                else
                    radio.clearCheck();
            }
        }
    });
}
Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
  • The moment old radio button is set programmatically, again onChange is fired and user is stuck in loop till they click the positive button. Need a confirmation built along with radio change working on approval else auto revert. – Rahul Kahale Sep 12 '20 at 14:54