Search code examples
javaandroidandroid-dialogfragmentandroid-radiogroupandroid-radiobutton

RadioGroup check doesn't work for me


I would like to have such a functionality - I have a Dialog (with settings) in which there is a RadioGroup with two RadioButtons. When I close the dialog with the "save" button I would like not only save the data in my Data class, but also I want the Dialog to open with saved setting next time it is opened. I tried to achieve this with RadiGroup methods: getCheckedRadioButtonId() and .check(int), but my solution doesn't work and I don't know why.

My code:

SettingsDialogFragment.java

public class SettingsDialogFragment extends DialogFragment{
    
    private boolean ifMale;
    private int checkedRadio;
    private RadioGroup rg;
    
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        builder.setTitle(R.string.settings);
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.settings_dialog, null);

        builder.setView(view);
        builder.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       Data.ifMale = ifMale;
                       checkedRadio = rg.getCheckedRadioButtonId();
                       System.out.println("numer radio" +checkedRadio);
                   }
               });
        builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User cancelled the dialog
                   }
               });
        

        rg = (RadioGroup) view.findViewById(R.id.radio_group);
        
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch(checkedId) {
                case R.id.radio_female:
                    //Log.v("RadioGroup", "female");
                    ifMale = false;
                    break;
                case R.id.radio_male:
                    Log.v("RadioGroup","male");
                    ifMale = true;
                    break;
                }
            }
        });
        rg.check(checkedRadio);
        
        return builder.create();
    }
}

settings_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <RadioGroup  android:id="@+id/radio_group"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">
        <RadioButton android:id="@+id/radio_female"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/female"/>
        <RadioButton android:id="@+id/radio_male"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/male"/>
    </RadioGroup>
</LinearLayout>

Solution

  • You can use Shared Preference to keep track of checked Radio button's information, and check it next time when dialog is opened.

    Code Snippet:

    SharedPreferences pref = getApplicationContext().getSharedPreferences("RadioPref", MODE_PRIVATE); 
    Editor editor = pref.edit();
    
    //save data in shared preference on button click
    builder.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            Data.ifMale = ifMale;
            checkedRadio = rg.getCheckedRadioButtonId();
            System.out.println("numer radio" +checkedRadio);
            editor.putInt("checkedId", checkedRadio); 
        }
    });
    //get checkedId from shared preference and check that radion button.
    int checkedId = pref.getInt("checkedId", -1);
    if(checkedId != -1)
    {
        //rg.check(checkedId);
        RadioButton rbutton =(RadioButton)findViewById(checkedId);
        rbutton.setChecked(true);
    }
    

    Hope this helps.