0

I am working on an application where my activity has 2 radio buttons.

  • 4 channel

  • 9 channel

Now:

  1. default selection of the screen is on 4channel radio button.

  2. if the user wants to click the (9channel) button, it will open a dialog box prompting user

" if you move, all saved items will be deleted "

  1. In the dialog box i have "Yes" and "No" button. If you click "Yes" button it will select the 9 channel radio button.

The issues that i want to fix are :

  1. when the user clicks on "No" button on the dialog box, i quit the dialog. But selection of radio button still moves to the (9channel) radio button. How to keep the old selection if user click "No" on the dialog box?

  2. Default selection on the activity is on (4channel) radio button. If the user clicks on the same selected button, i dont want the dialog box to open. The dialog box should open only if i click on the other radio button .

How can i achieve the above 2 scenarios? I don't know how to get the radionbutton view and show to user.

my Code :

rg = (RadioGroup) findViewById(R.id.radioGroup1);
public void addListenerOnButton() 
       {

        OnClickListener listener = new OnClickListener() {
        @Override
        public void onClick(View v) {
               RadioButton rb = (RadioButton) v;
               selection = (String) rb.getText();

                   mFlag = true;
                   showDialog(SELECTION_ALERT_ID);

        }
    };

    RadioButton rb1 = (RadioButton) findViewById(R.id.radioButton1);
    rb1.setOnClickListener(listener);
    RadioButton rb2 = (RadioButton) findViewById(R.id.radioButton2);
    rb2.setOnClickListener(listener);
} 
appoll
  • 2,910
  • 28
  • 40
Sunil
  • 521
  • 1
  • 7
  • 24

2 Answers2

1

Make a code for "Negative" option in your code:

new AlertDialog.Builder(this)
                    .setTitle("Hello!")
                    .setMessage("Choose your RadioButton:")
                    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) { 
                            rb1.setChecked(true);
                            rb2.setChecked(false);                                
                        }
                    })
                    .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            rb1.setChecked(false);
                            rb2.setChecked(true);
                        }
                    })
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .show();
SiloƩ Bezerra Bispo
  • 2,056
  • 1
  • 17
  • 31
0

for 1, in your dialog button listener, do:

first, move your radio button to global variable and add following listener to your dialog popup.

then, update the listener for negative button :

.setNegativeButton(getString(R.string.alert_dialog_negative_text),
      new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
             //not checked, but according to api, should be this method
            rb1.setSelected(true);
        }
 });

for 2, you can do a check in your listener code and if the clicked radio button is checked, do not showDialog.

But personally I would recommend to use the OnCheckedChangeListener for the radio button, refer to this question: radio button listener

Community
  • 1
  • 1
Surely
  • 1,649
  • 16
  • 23