0

I have a custom AlertDialog with radio buttons which will open on click of a textview. Everything is working as it is supposed to except that the radio buttons are never shown as "checked". Whenever I open the AlertDialog all of the buttons are unchecked. After selecting a button and opening the AlertDialog again the buttons are unchecked again.

My XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          android:padding="15dp">

<RadioGroup
    android:id="@+id/rg_gender"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_marginRight="30dp"
    android:layout_marginLeft="30dp"
    android:layout_marginTop="10dp"
    android:orientation="vertical"
    android:layout_gravity="center_horizontal">

    <RadioButton
        android:id="@+id/buttonMale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/therapist_male"
        />

    <RadioButton
        android:id="@+id/buttonFemale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/therapist_female"
        />

    <RadioButton
        android:id="@+id/buttonEither"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/therapist_either"
        />
</RadioGroup>
</LinearLayout>

My AlertDialog:

private void therapistDialog() {

    final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.gender_dialog, null);
    dialogBuilder.setView(dialogView);
    dialogBuilder.setNegativeButton(getResources().getString(R.string.cancel_button), new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    final AlertDialog alertDialog = dialogBuilder.show();

    final RadioGroup rg = (RadioGroup) dialogView.findViewById(R.id.rg_gender);
    rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (checkedId == R.id.buttonMale){
                tvGender.setText(getResources().getString(R.string.therapist_male));
                therapistSex = 0;
                mMassage.setTherapistSex(therapistSex);
                male.setChecked(true);
                alertDialog.dismiss();
            } else if (checkedId == R.id.buttonFemale){
                tvGender.setText(getResources().getString(R.string.therapist_female));
                therapistSex = 1;
                mMassage.setTherapistSex(therapistSex);
                alertDialog.dismiss();
            } else if (checkedId == R.id.buttonEither){
                tvGender.setText(getResources().getString(R.string.therapist_either));
                therapistSex = 2;
                mMassage.setTherapistSex(therapistSex);
                alertDialog.dismiss();
            }
        }
    });
jlively
  • 735
  • 2
  • 9
  • 29
  • 4
    First of all, consider using switch instead of if else construction. I suppose that you are not storing your checked state. Try to bind you state to some data and ititialize your dialog with this data whed dialog is building. – nutella_eater Jun 24 '16 at 11:30

2 Answers2

0

You are not restoring the state of dialog on opening and assign a new instance of AlertDialog every time You invoke therapistDialog method.
Try to check the button on AlertDialog view init.
Add

int buttonToCheck = mMassage.getTherapistSex();
RadioGroup radioGroup = dialogView.findViewById(R.id.rg_gender);
int buttonId = radioGroup.getChildAt(buttonToCheck).getId();
radioGroup.check(radioGroup.getChildAt(buttonToCheck).getId());

Before invoking dialogBuilder.setView(dialogView);

R. Zagórski
  • 20,020
  • 5
  • 65
  • 90
  • Thank you! This works! I only have one question. The buttonId is never used? – jlively Jun 24 '16 at 11:48
  • Not sure, what You are asking for. You already previously have used button id to know which checkbox was checked. Here, in method `setTherapistSex` You also saved only child order number, so to check the button You must first get 'RadioGroup' child by order number and not by button id. – R. Zagórski Jun 24 '16 at 11:50
  • Nevermind, I figured that out. Thank you one more time! – jlively Jun 24 '16 at 12:18
0

Every time you call the method therapistDialog() is creating a new instance of the alertDialogBuilder. You must create instance of the alertDialogBuilder only once globally,

and just call

final AlertDialog alertDialog = dialogBuilder.show(); inside your method.

note: this is not tested but this should work and will maintain your check box states also

Qadir Hussain
  • 8,721
  • 13
  • 89
  • 124