When I click a button, it displays pop up dialog which has five radio buttons. Now requirements are
- 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.
- 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;
}
}
};
});
}
You should use
SharedPreferencesfor this.You have 5 radiobuttons, so you could use
SharedPreferenceswith a default value (0 for example) in case that you have not used the dialog before.Then you could use your
OnClickListenerto know which value has been selected and store it.That should be enough to achieve your desired behaviour
Hope it helps!