Android SharedPreferences Tutorial

1- What is SharedPreferences?

First to clarify what is SharedPreferences, you can see a situation:

You're playing a game on Android, before playing the game, you choose the parameters of the game such as  brightness, volume levels, and difficulty level. After finishing, you can turn off the game and continue to play the next day. SharedPreferences allows you to save all the parameters you have set previously, and allow you to replay the settings that can be used without having to reset.
SharedPreferences saves the raw data in the form of pairs of key-value) to the files of the application. You can also choose a private storage mode  that other applications can not access these files, so it is safe.

2- SharedPreferences example

Preview images of the application's interface:
Create a project named AndroidSharedPreferences:
Design the interface:

SLIDER:

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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="org.o7planning.androidsharedpreferences.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Game Setting:"
        android:id="@+id/textView_setting"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Sound:"
        android:id="@+id/textView_sound"
        android:layout_below="@+id/textView_setting"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="35dp" />

    <SeekBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/seekBar_sound"
        android:layout_below="@+id/textView_sound"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="35dp"
        android:layout_alignRight="@+id/textView_setting"
        android:layout_alignEnd="@+id/textView_setting"
        android:max="100"
        android:progress="90"
        android:indeterminate="false" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Brightness:"
        android:id="@+id/textView_brightness"
        android:layout_below="@+id/seekBar_sound"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="35dp" />

    <SeekBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/seekBar_brightness"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignEnd="@+id/seekBar_sound"
        android:max="100"
        android:progress="95"
        android:indeterminate="false"
        android:layout_below="@+id/textView_brightness"
        android:layout_marginTop="35dp"
        android:layout_alignRight="@+id/textView_setting" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Difficulty Level"
        android:id="@+id/textView_diffLevel"
        android:layout_below="@+id/seekBar_brightness"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="35dp" />

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@+id/textView_diffLevel"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="35dp"
        android:orientation="horizontal"
        android:id="@+id/radioGroup_diffLevel">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Easy"
            android:id="@+id/radioButton_easy"
            android:checked="false" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Medium"
            android:id="@+id/radioButton_medium"
            android:checked="false" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hard"
            android:id="@+id/radioButton_hard"
            android:checked="false" />
    </RadioGroup>

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save"
        android:id="@+id/button"
        android:layout_below="@+id/radioGroup_diffLevel"
        android:layout_toRightOf="@+id/textView_diffLevel"
        android:onClick="doSave" />

</RelativeLayout>
MainActivity.java
package org.o7planning.androidsharedpreferences;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


    private SeekBar seekBarSound ;
    private SeekBar seekBarBrightness;

    private RadioGroup radioGroupDiffLevel;
    private RadioButton radioButtonEasy;
    private RadioButton radioButtonMedium;
    private RadioButton radioButtonHard;

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

        this.seekBarBrightness= (SeekBar)this.findViewById(R.id.seekBar_brightness);
        this.seekBarSound= (SeekBar)this.findViewById(R.id.seekBar_sound);

        this.radioGroupDiffLevel= (RadioGroup) this.findViewById(R.id.radioGroup_diffLevel);
        this.radioButtonEasy=(RadioButton) this.findViewById(R.id.radioButton_easy);

        this.radioButtonMedium = (RadioButton)this.findViewById(R.id.radioButton_medium);
        this.radioButtonHard=(RadioButton) this.findViewById(R.id.radioButton_hard);


        // Load saved game setting.
        this.loadGameSetting();
    }


    private void loadGameSetting()  {
        SharedPreferences sharedPreferences= this.getSharedPreferences("gameSetting", Context.MODE_PRIVATE);

        if(sharedPreferences!= null) {

            int brightness = sharedPreferences.getInt("brightness", 90);
            int sound = sharedPreferences.getInt("sound",95);
            int checkedRadioButtonId = sharedPreferences.getInt("checkedRadioButtonId", R.id.radioButton_medium);

            this.seekBarSound.setProgress(sound);
            this.seekBarBrightness.setProgress(brightness);
            this.radioGroupDiffLevel.check(checkedRadioButtonId);

        } else {
            this.radioGroupDiffLevel.check(R.id.radioButton_medium);
            Toast.makeText(this,"Use the default game setting",Toast.LENGTH_LONG).show();
        }

    }

    // Called when user click to Save button.
    public void doSave(View view)  {
        // The created file can only be accessed by the calling application
        // (or all applications sharing the same user ID).
        SharedPreferences sharedPreferences= this.getSharedPreferences("gameSetting", Context.MODE_PRIVATE);

        SharedPreferences.Editor editor = sharedPreferences.edit();

        editor.putInt("brightness", this.seekBarBrightness.getProgress());
        editor.putInt("sound", this.seekBarSound.getProgress());

        // Checked RadioButton ID.
        int checkedRadioButtonId = radioGroupDiffLevel.getCheckedRadioButtonId();

        editor.putInt("checkedRadioButtonId", checkedRadioButtonId);

        // Save.
        editor.apply();

        Toast.makeText(this,"Game Setting saved!",Toast.LENGTH_LONG).show();
    }
}
 
Running apps:
After saving, you can exit application and re-open it, paramaters that you have saved will be set automatically for the game