Skip to content Skip to sidebar Skip to footer

Save The State Of Radiobutton

I am having 5 radiobuttons in my application and i want to save their states so that when i exit and then come back to the application then i see the same button clicked which was

Solution 1:

you are saving all button state using same key(check). Try something like this

privatevoidsave(int radioid,finalboolean isChecked) {

SharedPreferencessharedPreferences= getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editoreditor= sharedPreferences.edit();
editor.putBoolean("check"+radioid, isChecked);
editor.commit();
}

In load method loop through all radiobutton

privatevoidload() { 
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
for(int i=0;i<radioGroup.getChildCount();i++){
   RadioButton rbtn=(RadioButton)radioGroup.getChildAt(i);
   rbtn.setChecked(sharedPreferences.getBoolean("check"+rbtn.getId(), false)); 
 }
}

UPDATE

In RadioGroup only one button is checked at a time. so store check button id instead.

privatevoidsave(int radioid) {

SharedPreferencessharedPreferences= getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editoreditor= sharedPreferences.edit();
editor.putInt("check", radioid);
editor.commit();
}

and use following code to restore

privatevoidload() { 
   SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);

   int radioId=sharedPreferences.getInt("check", 0);
   if(radioId>0){
     RadioButton rbtn=(RadioButton)radioGroup.findViewById(radioId);
     rbtn.setChecked(true); 
   }

 }

Solution 2:

you can use this code

publicclassMainActivityextendsActivity {

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stubsuper.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    RadioGrouprg= (RadioGroup) findViewById(R.id.radioGroup1);
    SharedPreferencessp= getSharedPreferences("setting", MODE_PRIVATE);

    if(sp.getInt("checked", 0) != 0){

    rg.check(sp.getInt("checked", 0));

    }
}

@OverrideprotectedvoidonPause() {
    // TODO Auto-generated method stubsuper.onPause();
    RadioGrouprg= (RadioGroup) findViewById(R.id.radioGroup1);
    intid= rg.getCheckedRadioButtonId();
    SharedPreferencessp= getSharedPreferences("setting", MODE_PRIVATE);
    Editore= sp.edit();
    e.putInt("checked", id);
    e.commit();
    }

}

and here is layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" >

<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="53dp" >

    <RadioButton
        android:id="@+id/radio0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="RadioButton" />

    <RadioButton
        android:id="@+id/radio1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" />

    <RadioButton
        android:id="@+id/radio2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" />
</RadioGroup>

Solution 3:

You could also try override:

publicvoidonSaveInstanceState(Bundle savedInstanceState) {
    savedInstanceState.putBoolean("radioButton1", true);
    savedInstanceState.putBoolean("radioButton2", false);
    savedInstanceState.putBoolean("radioButtonN", false);
}

publicvoidonRestoreInstanceState(Bundle savedInstanceState){
    boolean rb1_state = savedInstanceState.getBoolean("radioButton1");
    boolean rb2_state = savedInstanceState.getBoolean("radioButton2");
    boolean rbN_state = savedInstanceState.getBoolean("radioButtonN");
}

methode

Solution 4:

use SharedPreferences in a separate helper class. That way you could initialize the SharedPreferences in the start of the activity and check if there were any values present there.

You can see the code example here. Doing this ... as I said in the onCreate you could check if the SharedPreferences has any value. Check the full source code here.

Solution 5:

save your radio button value using shared preferences like this

SharedPreferencessettings= getSharedPreferences("LEVEL", 0);
    SharedPreferences.Editoreditor= settings.edit();
    editor.putBoolean("FIRST", radiobutton1.isChecked()); // first argument is a                                                        

    editor.commit(); // Commit the changes

And to load this you have to set it to default true/false.

SharedPreferencessettings= getSharedPreferences("LEVEL", 0);
    boolean isChk= settings.getBoolean("FIRST", false);

and to use this write down below code

    if (isChk)
     radiobutton1.setChecked(true);

Post a Comment for "Save The State Of Radiobutton"