Showing posts with label Shared Preference. Show all posts
Showing posts with label Shared Preference. Show all posts

Sunday, September 13, 2015

Using Shared Preferences in Setting Screen

Hi Guys I'm back! Today, My topic is all about Shared Preferences of Android. What is Shared Preference? a class that allows you to save and retrieve persistent key-value pairs of primitive data types(booleans, floats, ints, longs, and strings). This sample code may give you idea how to use Shared Preference in your Android App Projects. I used Shared Preference in saving my Settings, The scenario is if the user wants to save his/her setting in the app for example, turning on/ turning off the Music, Sounds and Updates. Note: This data will persist across user sessions (even if your application is killed).

import android.app.Activity;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Switch;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

    Switch musicSwitch;
    Switch soundsSwitch;
    Switch updatesSwitch;

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

        musicSwitch = (Switch) findViewById (R.id.switch1);
        soundsSwitch = (Switch) findViewById (R.id.switch2);
        updatesSwitch = (Switch) findViewById (R.id.switch3);
        saveBtn = (Button) findViewById (R.id.buttonSave);

        setWidget();
        saveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                savePreferences();
            }
        });

    }

    private void setWidget(){
        //Get the value of SharedPref if null return false 
        SharedPreferences customSharedPreference = getSharedPreferences("mySettings", Activity.MODE_PRIVATE);
        updatesSwitch.setChecked(customSharedPreference.getBoolean("updates",false));
        musicSwitch.setChecked(customSharedPreference.getBoolean("music",false));
        soundsSwitch.setChecked(customSharedPreference.getBoolean("sounds",false));


    }
    private void savePreferences(){

        SharedPreferences myPref = getSharedPreferences("mySettings", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = myPref.edit();
        //Setting the value of SharedPref
        editor.putBoolean("music", musicSwitch.isChecked());
        editor.putBoolean("sounds", soundsSwitch.isChecked());
        editor.putBoolean("updates", updatesSwitch.isChecked());
        editor.commit();

        Toast.makeText(this, "Setting Saved", Toast.LENGTH_SHORT).show();
    }

}



Layouts:
<RelativeLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <Switch android:id="@+id/switch1"
        android:layout_centerInParent="true"
        android:layout_height="20dp"
        android:layout_width="wrap_content"
        android:text="Music"/>

    <Switch android:id="@+id/switch2"
        android:layout_height="20dp"
        android:layout_width="wrap_content"
        android:text="Sounds"
        android:layout_above="@+id/switch3"
        android:layout_alignLeft="@+id/switch3"
        android:layout_alignStart="@+id/switch3"
        android:layout_marginBottom="56dp" />


    <Switch android:id="@+id/switch3"
        android:layout_height="20dp"
        android:layout_width="wrap_content"
        android:text="Updates"
        android:layout_above="@+id/switch1"
        android:layout_alignLeft="@+id/switch1"
        android:layout_alignStart="@+id/switch1"
        android:layout_marginBottom="49dp" />

    <Button android:id="@+id/buttonSave"
        android:layout_height="45dp"
        android:layout_width="80dp"
        android:text="SAVE"
        android:layout_below="@+id/switch1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="58dp" />

</RelativeLayout>
Output: