When I click a button, it displays pop up dialog which has five radio buttons. Now requirements are

  1. When I launch and click button,pop up dialog displays initially with first radio button which should be in checked state while others are in unchecked state.
  2. When user changes by selecting the next radio button,dismiss the dialog and once again if user open pop up means,it should be in previous selected state.

How to proceed with this?

Code is as follows:

public class PopUpDialogRadioButton extends Activity {
    private Button popUpClick;
    private Dialog dialog;
    private RadioGroup radioGroup;
    private RadioButton radioButton1, radioButton2, radioButton3, radioButton4, radioButton5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.sortfilterclick);
    popUpClick = (Button) findViewById(R.id.popupButton); // on click of button, opens a pop up dialog
    popUpClick.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            dialog = new Dialog(PopUpDialogRadioButton.this);
            dialog.setContentView(R.layout.sortfilterrow);
            radioGroup = (RadioGroup) dialog.findViewById(R.id.radioGroup1);
            radioButton1 = (RadioButton) dialog.findViewById(R.id.radio1);
            radioButton2 = (RadioButton) dialog.findViewById(R.id.radio2);
            radioButton3 = (RadioButton) dialog.findViewById(R.id.radio3);
            radioButton4 = (RadioButton) dialog.findViewById(R.id.radio4);
            radioButton5 = (RadioButton) dialog.findViewById(R.id.radio5);
            radioButton1.setOnClickListener(radioButtonOnClickListener);
            radioButton2.setOnClickListener(radioButtonOnClickListener);
            radioButton3.setOnClickListener(radioButtonOnClickListener);
            radioButton4.setOnClickListener(radioButtonOnClickListener);
            radioButton5.setOnClickListener(radioButtonOnClickListener);
            radioGroup.clearCheck();     
            radioButton1.setChecked(true);
            int selectedId = radioGroup.getCheckedRadioButtonId();
            RadioButton osButton = (RadioButton) dialog
                    .findViewById(selectedId);
            StringBuffer responseText = new StringBuffer();
            responseText.append(osButton.getText());
            Toast.makeText(getApplicationContext(), responseText,
                    Toast.LENGTH_SHORT).show();
            // radioGroup.setOnCheckedChangeListener(radioGroupOnCheckedChangeListener);

            dialog.setCancelable(true);
            dialog.setTitle("Sort By");
            dialog.show();
        }

        private final OnClickListener radioButtonOnClickListener = new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                /*
                 * RadioButton rb = (RadioButton) v;
                 * Toast.makeText(SortFilterPopupActivity.this,
                 * rb.getText(), Toast.LENGTH_SHORT).show();
                 */
                switch (v.getId()) {
                case R.id.radio1:
                    Toast.makeText(PopUpDialogRadioButton.this, "first",
                            Toast.LENGTH_SHORT).show();
                                            dialog.dismiss();
                    break;
                case R.id.radio2:
                    Toast.makeText(PopUpDialogRadioButton.this, "two",
                            Toast.LENGTH_SHORT).show();
                                            dialog.dismiss();
                    break;
                case R.id.radio3:
                    Toast.makeText(PopUpDialogRadioButton.this, "three",
                            Toast.LENGTH_SHORT).show();
                                            dialog.dismiss();
                    break;
                case R.id.radio4:
                    Toast.makeText(PopUpDialogRadioButton.this, "four",
                            Toast.LENGTH_SHORT).show();
                                        dialog.dismiss();
                    break;
                case R.id.radio5:
                    Toast.makeText(PopUpDialogRadioButton.this, "five",
                            Toast.LENGTH_SHORT).show();
                    dialog.dismiss();
                    break;
                }
            }
        };
    });
}
2

There are 2 best solutions below

0
On

You could store the selected item in SharedPreferences. Check the developer site for a good introduction to SharedPreferences.

Sample usage:

   // Loading preferences.
   SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
   String selectedItem = settings.getString("chkboxSelection");
   if (selectedItem != null) {
        // Compare value and mark checkbox accordingly.
   }

   // Saving preferences.
   SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
   SharedPreferences.Editor editor = settings.edit();
   editor.putString("chkboxSelection","checkboxIdentifier");
0
On

You should use SharedPreferences for this.

You have 5 radiobuttons, so you could use SharedPreferences with a default value (0 for example) in case that you have not used the dialog before.

int radio_selected = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE).getInt("my_selected_radio",0);

Then you could use your OnClickListener to know which value has been selected and store it.

int radio_selected = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE).edit().putInt("my_selected_radio",selectedValue).commit();

That should be enough to achieve your desired behaviour

Hope it helps!