Search code examples
javaandroidandroid-radiogroup

How to save the state of a radio group?


I want the user to have the same radio button checked which he had previously checked before the app was closed.

This is my source code

package com.example.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.R;

public class MainActivity extends AppCompatActivity {

    RadioGroup radioGroup;
    RadioButton radioButton;
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        radioGroup = findViewById(R.id.radioGroup);
        textView = findViewById(R.id.text_view_selected);

        Button buttonApply = findViewById(R.id.button_apply);
        buttonApply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int radioId = radioGroup.getCheckedRadioButtonId();

                radioButton = findViewById(radioId);

                textView.setText("Your choice: " + radioButton.getText());
            }
        });
    }

    public void checkButton(View v) {
        int radioId = radioGroup.getCheckedRadioButtonId();

        radioButton = findViewById(radioId);

        Toast.makeText(this, "Selected Radio Button: " + radioButton.getText(),
                Toast.LENGTH_SHORT).show();

    }
}

I want the radio button states to be saved for next time the app is used.


Solution

  • You should take a look at sharedPreferences : https://developer.android.com/training/data-storage/shared-preferences

    Save your data :

    SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString("myRadio", radioButton.getText());
    editor.commit();
    

    Retrieve data :

    SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
    String defaultValue = "";
    String radioButtonText = sharedPref.getString("myRadio", defaultValue);
    

    For now, you can do this :

    public class MainActivity extends AppCompatActivity {
    
       RadioGroup radioGroup;
       RadioButton radioButton;
       TextView textView;
       // Declare SharedPreferences as attributes
       private SharedPreferences sharedPref;
       private SharedPreferences.Editor editor;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            radioGroup = findViewById(R.id.radioGroup);
            textView = findViewById(R.id.text_view_selected);
            // Retrieve SharedPreferences 
            sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
            editor = sharedPref.edit();
    
            Button buttonApply = findViewById(R.id.button_apply);
            buttonApply.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int radioId = radioGroup.getCheckedRadioButtonId();
    
                    radioButton = findViewById(radioId);
    
                    textView.setText("Your choice: " + radioButton.getText());
                }
            });
        }
    
       @Override
       protected void onResume() {
           int radioIdChecked = -1;
           radioIdChecked = sharedPref.getInt("myRadioChecked", radioIdChecked);
           if (radioIdChecked == -1) {
               // ERROR, don't check anything
               Log.d("TAG", "error: don't check anything");
           }
           else {
               RadioButton radioButton = radioGroup.findViewById(radioIdChecked);
               if (radioButton != null) {
                   radioButton.setChecked(true);
               }
           }
        }
    
       @Override
       protected void onPause() {
            editor.putInt("myRadioChecked", radioGroup.getCheckedRadioButtonId());
            editor.commit();
       }
    

    Best