5. Android SharedPreferences Example | Android Settings Tutorial

This entry is part 5 of 22 in the series Android-Intermediate

Icon for Android SharedPreferences TutorialSharedPreferences Can Save and Load Data

In this Android tutorial, we will learn about creating a settings menu using Android’s sharedPreferences.  This sharedPreferences example will save the state of check boxes, strings, and other data that will be loaded each time the application starts.  As long as the user doesn’t erase the data, nor uninstall the application, the data will be stored on the user’s phone.  If you haven’t programmed with Android before, or if you think I’m moving a little fast in this tutorial, you should start with the Android basics series.


SharedPreferences Walk-through


Android SharedPreferences Project Setup

If you have done any sort of Android programming before, you have probably wanted to save some data, and that is probably why you have ended up here.  SharedPreferences is the easiest way to save and load primitive data, whether you are looking to save the user’s name or whether you are creating a settings (or preferences) menu.

There are actually five different ways to save data for Android according the to Android Developers Page:

  • Shared Preferences: Store private primitive data in key-value pairs.
  • Internal Storage: Store private data on the device memory.
  • External Storage: Store public data on the shared external storage (or SD card).
  • SQLite Databases: Store structured data in a private database.
  • Network Connection: Store data on the web with your own network server.
We will learn about all of these in future tutorials, but as for now we are going to focus solely on shared preferences, and storing key-value pairs.  What is a key-value pair?  A key-value can be of any primitive data type, whether that be a string, boolean, int, double, or other primitive data type.  For example:

 String name = "Travis";
name would be “key” the  and Travis would be the “value”.

So, now we know what  a key-value pair is, we will be able to use sharedPreferences to save a bunch of stuff.  Another example would be if we have our app save a boolean data type with key “music” with the value of “true” (or false).  This could be a checkbox within a settings menu, and before we play any music within our app we would check sharedPreferences to see if “music” is set to “true” and if so, we would play music, if it was set to false, we wouldn’t play music.  The user would then be able to go into the settings and change the state of the “music” key  and that state would be saved forever (at least while the user doesn’t erase the data or uninstall the app), or until the user changes the state of the checkbox again.

So hopefully you now know a little be more about Android sharedpreferences.  Look at the source code below to setup the structure of our sharedPreferences example that we will be using in the next couple of tutorials.  In the next few Android tutorials we will continue to see how sharedPreferences can be used, so make sure you check that out.


Source Code

settings.xml layout

<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Display Name"/>
<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Enter your name."/>
<EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content">
<requestFocus/>
</EditText>
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Save"/>
</LinearLayout>

Settings.java

package com.mybringback.theworks;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

public class Settings extends Activity implements OnClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);
	}

	private void loadPrefs() {

	}

	private void savePrefs(String key, boolean value) {

	}

	private void savePrefs(String key, String value) {

	}

}

Navigation

Previous Lesson
Next Lesson

Author: trav

I'm just an average guy that love programming.

Share This Post On

Submit a Comment

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>