Android Shared Preferences Tutorial
Android Shared Preferences Tutorial
Android provides many ways of storing data of an application. One of this way is called Shared
Preferences. Shared Preferences allow you to save and retrieve data in the form of key,value pair.
In order to use shared preferences , you have to call a method getSharedPreferences() that returns
a SharedPreference instance pointing to the file that contains the values of preferences.
SharedPreferences sharedpreferences =
getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
The first parameter is the key and the second parameter is the MODE. Apart from private there
are other modes availaible that are listed below:
You can save something in the sharedpreferences by using SharedPreferences.Editor class. You
will call the edit method of SharedPreference instance and will recieve it in an editor object. Its
syntax is:
Apart from the putString method , there are methods availaible in the editor class that allows
manipulation of data inside shared preferences. They are listed as follows:
Example
This example demonstrates the use of the Shared Preferences. It display a screen with some text
fields , whose value are saved when the application is closed and brought back when it is opened
again .
XML TAG TO CREATE GUI ABOVE
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/et_un"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:hint="enter your name" />
<EditText
android:id="@+id/et_pw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/et_un"
android:hint="Enter your password" />
<Button
android:id="@+id/btn_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/et_pw"
android:onClick="btnSaveClicked"
android:text="Save" />
<Button
android:id="@+id/btn_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/btn_save"
android:layout_marginTop="14dp"
android:onClick="btnClearclicked"
android:text="clear cookie" />
</RelativeLayout>
package com.examuple.sharedprefexam;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
}
if (shp.contains(myPw))
{
pw.setText(shp.getString(myPw, ""));
}
}
public void btnSaveClicked(View v) {
String u=un.getText().toString();
String p=pw.getText().toString();
Editor e=shp.edit();
e.putString(myName, u);
e.putString(myPw, p);
e.commit();
}
public void btnClearclicked(View v) {
Editor e=shp.edit();
e.remove(myName);//to remove myName key from shared
preference
e.remove(myPw);
e.commit();
}
}
Now just put in some text in the field. Like i put some random name and other information and
click on save button.
Now when you press save button , the text will be saved in the shared preferences. Now press
back button and exit the application. Now open it again and you will see all the text you have
written back in your application.
How to include UNICODE characters in Android application ( e.g. Amharic letters)
First create a folder called assets in your project and then create other folder fonts inside the
assets folder and then paste your Unicode fonts file (for Amharic “GF zemen.ttf”) to fonts
folder. Every character has its own Unicode example: \u1218 means letter መ in
Amharic
How to use this in java? Assume that you won’t make the name of a button using Amharic letter
Typeface face=Typeface.createFromAsset(getAssets(),"fonts/GF
Zemen.ttf");