Friday, 27 February 2015

Shared Preferences in Android

* Basically shared preference in android are used to save the state of an activity or to save the important data within the scope of an application means data will remain saved till the application is installed in the devices. Shared Preference also works as Sessions which are used for the automatic login process.

* A lightweight mechanism known as shared preferences to save small chunks of data.

* Using the SharedPreferences object, however, you save the data you want through the use of key/ value pairs — specify a key for the data you want to save, and then both it and its value will be saved automatically to an XML file for you.

using getSharedpreferences() 

 To see how the SharedPreferences object works, the following Try It Out demonstrates how easy it is to save user data to an XML file, and then retrieve it easily via the same object.
 Saving Data Using the SharedPreferences Object

1-  Add the following statements in bold to the main.xml file:

<LinearLayout​xmlns:android=”” ​​​​android:orientation=”vertical”

<SeekBar ​​​​android:id=”@+id/SeekBar01” 
​​​​android:layout_width=”fill_parent” ​​​​
android:layout_height=”wrap_content” />

 <TextView ​​​​android:id=”@+id/TextView01” 
​​​​android:layout_width=”fill_parent” ​​​​
 ​​​​android:text=”@string/hello” /> 

<EditText ​​​​android:id=”@+id/EditText01” 
 ​​​​android:layout_height=”wrap_content” /> 

<Button ​​​​android:id=”@+id/btnSave”
 ​​​​android:text=”Save” ​​​​
​​​​android:layout_height=”wrap_content” /> 


2 . Add the following statements in bold to the file: 

package ​com.emergingandroidtech.SharedPreferences;
import ​; 
import ​android.os.Bundle;
import android.content.SharedPreferences; 
import android.view.View;
import android.widget.Button;
import android.widget.EditText; 
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;
public​ class ​MainActivity ​extends ​Activity​
​​​​private SharedPreferences prefs;
 ​​​​private String prefName = “MyPref”; ​​​​
private EditText editText; ​​​​
private SeekBar seekBar; 
​​​​private Button btn;
​​​​private static final String FONT_SIZE_KEY = “fontsize”;
 ​​​​private static final String TEXT_VALUE_KEY = “textvalue”;
​​​​@Override ​​​​public​ void ​onCreate(Bundle​savedInstanceState)​
​​​​​​​​editText = (EditText) findViewById(;
 ​​​​​​​​seekBar = (SeekBar) findViewById(;
​​​btn = (Button) findViewById(;
​​​​​​​​btn.setOnClickListener(new View.OnClickListener() 
 ​​​​​​​​​​​​public void onClick(View v) 
{ ​​​​​​​​​​​​​​​​

//---get the SharedPreferences object---
 ​​​​​​​​​​​​​​​​prefs = getSharedPreferences(prefName, MODE_PRIVATE);
 ​​​​​​​​​​​​​​​​SharedPreferences.Editor editor = prefs.edit();
​​​​​​​​​​​​​​​​//---save the values in the EditText view to preferences--- ​​​​​​​​​​​​​​​​
editor.putFloat(FONT_SIZE_KEY, editText.getTextSize()); 
​​​​​​​​​​​​​​​​editor.putString(TEXT_VALUE_KEY, editText.getText().toString());
​​​​​​​​​​​​​​​​//---saves the values--- 
​​​​​​​​​​​​​​​​//---display file saved message--- 
​​​​​​​​​​​​​​​​Toast.makeText(getBaseContext(), ​​​​​​​​​​​​​​​​​​​​“Font size saved successfully!”, ​​​​​​​​​​​​​​​​​​​​Toast.LENGTH_SHORT).show(); ​​​​​​​​​​​​}
​​​​​​​​//---load the SharedPreferences object--- ​​​​​​​​
SharedPreferences prefs = getSharedPreferences(prefName, MODE_PRIVATE);
​​​​​​​​//---set the TextView font size to the previously saved values--- 
​​​​​​​​float fontSize = prefs.getFloat(FONT_SIZE_KEY, 12);
​​​​​​​​//---init the SeekBar and EditText--- ​​​​​​​​
seekBar.setProgress((int) fontSize); ​​​​​​​​
editText.setText(prefs.getString(TEXT_VALUE_KEY, “”)); ​​​​​​​​editText.setTextSize(seekBar.getProgress());
​​​​​​​​seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() 
{ ​​​​​​​​​​​​
@Override ​​​​​​​​​​​​public void onStopTrackingTouch(SeekBar seekBar) { ​​​​​​​​​​​​}
​​​​​​​​​​​​@Override ​​​​​​​​​​​​public void onStartTrackingTouch(SeekBar seekBar) { ​​​​​​​​​​​​}
​​​​​​​​​​​​@Override ​​​​​​​​​​​​public void onProgressChanged(SeekBar seekBar, int progress, ​​​​​​​​​​​​​​​​​​​​boolean fromUser) { ​​​​​​​​​​​​​​​​

//---change the font size of the EditText--- ​​​​​​​​​​​​​​​​
editText.setTextSize(progress); ​​​​​​​​​​​​
} ​​​​​​​​

3 . Enter some text into the EditText view and then change its font size by adjusting the SeekBar view  Click Save.
4 .  The application now displays the same text that you entered earlier using the same font size set earlier.

No comments:

Post a Comment