Shared Preferences Example In Android Programming

Description:

This example will show you how to use shared preferences in 4.2 version.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it SharedPreferencesExample.

2.) Write following into layout/main.xml:

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/welcome"
/>
<Button
android:text="@string/Button1"
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_gravity="center_horizontal">
</Button>
</LinearLayout>

 

3.) Write following into your manifest file:

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.SharedPreferencesExample"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="com.example.SharedPreferencesExample.ManageSharedPerfs"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.SharedPreferencesExample.ViewSharedPrefs">
</activity>
</application>
<uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>
</manifest>

 

 

4.) Create and write following into ViewSharedPrefs.java:

 

package com.example.SharedPreferencesExample;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

import android.app.ListActivity;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Toast;

public class ViewSharedPrefs extends ListActivity {

private final String MY_NAME = "myname";
private final String MY_WALLPAPER = "wallpaper";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
String prefName = myPrefs.getString(MY_NAME, "nothing");
String wallPaper = myPrefs.getString(MY_WALLPAPER, null);

if(wallPaper != null) {
try {
Bitmap bm = BitmapFactory.decodeFile("/data/misc/wallpaper/"+wallPaper);
Log.d(getClass().getSimpleName(),"Wallpaper name is: "+ wallPaper);
setWallpaper(bm);
Toast.makeText(this, "Wall paper has been changed." +
"You may go to the home screen to view the same", Toast.LENGTH_LONG).show();
} catch (FileNotFoundException fe){
Log.e(getClass().getSimpleName(),"File not found");
} catch (IOException ie) {
Log.e(getClass().getSimpleName()," IO Exception");
}

}
ArrayList<String> results = new ArrayList<String>();
results.add("Your Preferred name is: " + prefName);
this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));

}

}

 

 

5.) Run for output.

Steps:

1.) Create a project named SharedPreferencesExample and set the information as stated in the image.

Build Target: Android 4.2
Application Name: SharedPreferencesExample
Package Name: com.example.SharedPreferencesExample
Activity Name: SharedPreferencesExample
Min SDK Version: 4.2

2.) Open ManageSharedPrefs.java file and write following code there:

 

package com.example.SharedPreferencesExample;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class ManageSharedPerfs extends Activity {

private final String MY_NAME = "myname";
private final String MY_WALLPAPER = "wallpaper";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putString(MY_NAME, "Sai");
prefsEditor.putString(MY_WALLPAPER, "f664.PNG");
prefsEditor.commit();

Button next = (Button)findViewById(R.id.next);

next.setOnClickListener(new OnClickListener () {

@Override
public void onClick(View arg0) {
Intent explicitIntent = new Intent(ManageSharedPerfs.this,ViewSharedPrefs.class);
startActivity(explicitIntent);
}
});
Toast.makeText(this, "Your preferred name and wall paper have been saved",Toast.LENGTH_SHORT).show();
}
}

 

3.) Compile and build the project.

Output