Test Script

Tuesday, December 17, 2013

Shared Preferences In Android

So a lot of times you see a few of your favorite apps make the app more customisable and friendlier to you by letting you choose your favorite theme, font and whole bunch of other stuff. Ever wondered how they do it? Wished your apps had such personalisation? Well, look no further today in this tutorial I will be covering something called Shared Preferences in Android which is exactly the magic behind the personalisation of your app for each user.

Shared Preference - It is named so because the preference value  is shared between different components of your app. These are a lightweight mechanism to store key value pairs and use those data later on in the app. You see the bigger picture now don't you? Each of your choice(theme, font etc) are stored as a preference. Thus every time the app starts it reads the stored preferences and customises the entire app for you. Exciting, isn't it ? Enough of my ranting, lets go see how you can setup this awesome thing in your apps.

So to better demonstrate the concept, let me assume that we wish to save the username of the user, so that the next time he opens the app he doesn't need to type the password.

The code snippet below explains how to save a key value pair in a shared preference :

String USERNAME_KEY ="UserName";
String prefName = "userNamePref";

public void savePreferences(){

  SharedPreferences  prefs = getSharedPreferences(prefName, MODE_PRIVATE);
  SharedPreferences.Editor prefEditor = prefs.edit();
  prefEditor.putString(USERNAME_KEY, "user123");


Let me quickly explain what we did there :

  1. Created a instance of SharedPreferences called 'prefs', using the getSharedPreferences() method. This method opens the preference named userNamePref if it already exists or creates a new preference if it doesn't exist.
  2. Opened the editor to modify the value stored within the preference.
  3. Inserted the username of the user(i.e. user123 in this case) into the preference.
  4. Lastly, after you have edited the preference you always need to call the commit() on the Editor, else the preference value won't be stored. This is a very common mistake and I forget it quite a few times as well.

So now that you have created a new preference you would like to read it every time the user opens the app so that you can fetch the saved username and display it to the user. So let us see how we read it :

SharedPreferences userPrefs = getSharedPreferences(prefName, MODE_PRIVATE);
String userName = userPrefs.getString(USERNAME_KEY, "");

That's all there is to reading the SharedPreference, the getString() method takes 2 parameters : key and a default value which is returned if the shared preference is not found. In this case if the shared preference is not found then we return an empty string. That's all there is to a simple Shared Preference.

This Shared Preference is available only to components within your app. There are a few extra methods to change the accessibility of the shared preferences but I will be covering that in a follow-up post sometime next week. If you have any queries feel free to drop me a comment below !

No comments:

Post a Comment

UA-42774700-1 Twitter Bird Gadget