I am new to coding in Android Studio and I just learned about Shared Prefrences. My teacher gave me an assignment to make a Radio Button Group (RadioGroup), that when you check one of the buttons, close the app, open it again, the button will remain checked.
Just like you would save an EditText
answer with SharedPrefrences
, I need to save a RadioButton
with SharedPrefrences
.
As I said I am new to this, so please keep the answer simple :)
--SOLVED-- Thank you all for answering! i found my solution thanks to your comments! :D
Well first, you want to set listener to the RadioGroup
like so:
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// save id into your SharedPreferences
}
});
And than you can restore it in onCreate() from shared preferences and call radioGroup.check(id);
.
So the code will look something like this:
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
int checkedId = sharedPrefs.getInt("your key", 0);
radioGroup.check(checkedId);
Please note that this code is simplified. You need to add checks and so on
Try This,
RadioGroup radioGroup = findViewById(R.id.radio_gropup);
final SharedPreferences preferences = getSharedPreferences("saved", 0);
radioGroup.check(preferences.getInt("CheckedId", 0));
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("CheckedId", checkedId);
editor.apply();
}
});
Here is how you are supposed to get the onCheckChangeListener on a radio group to get the selected radio button in it!
NOTE!!! THERE SHOULD BE IDs GIVEN TO ALL INNER RADIO BUTTONS
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
RadioButton rb=(RadioButton)findViewById(checkedId);
//textViewChoice.setText("You Selected " + rb.getText());
// here the rb.getText() will give you the text and you can send it to your
//sharedPreference with a new key value like you did with the editText values
}
});
And when your app starts you can check in your onCreate() by the sharefPreference if the value is saved then get the RadioButton and set its property isSelected as true! I hope that is simple enough!
public class MainActivity extends AppCompatActivity {
// this will be key for the key value pair
public static final String BUTTON_STATE = "Button_State";
// this is name of shared preferences file, must be same whenever accessing
// the key value pair.
public static final String MyPREFERENCES = "MyPrefs" ;
SharedPreferences sharedpreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// helper method to open up the file.
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
// grab the last saved state here on each activity start
Boolean lastButtonState = sharedpreferences.getBoolean(BUTTON_STATE, false)
RadioButton rb = (RadioButton) findViewById(R.id.radio_button);
// restore previous state
rb.setChecked(lastButtonState);
// set a listener
rb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// call this to enable editing of the shared preferences file
// in the event of a change
SharedPreferences.Editor editor = sharedpreferences.edit();
Boolean isChecked = rb.isChecked();
// use this to add the new state
editor.putBoolean(BUTTON_STATE, isChecked);
// save
editor.apply();
}
});
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.