Getting Started in Android Development
Part 29 -
Shared Preferences are an ideal way to store simple and small amounts of persistent data within app. A typical example would be app settings. The data is persistent meaning that it is available even when the app is closed down or the Android device is switched off. This means that shared preferences are stored as a file on the device.
The idea behind this tutorial is to load a simple Activity that will display a username and set a background color stored in shared preferences. This activity has a "settings" button that when clicked, starts an activity were the shared preferences are configured.
It's first worth looking at the SettingsActivity. The screenshot below shows the layout for this activity. It simply has an EditText field for the user to enter a username and a group of radio buttons to set the background color. The settings are committed to a shared preference file when the save button is pressed.
An object of SharedPreference needs to be created. In this example it is called prefs.
The following code is used to define the shared preferences file or retrieve if it already exists:
prefs = getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
Context.MODE_PRIVATE is a predefine integer. It means that this shared preference file is private to the app and cannot be accessed by any other app. The other options are:
Context.MODE_WORLD_READABLE, meaning all apps can access the shared preference file.
Context.MODE_WORLD_WRITEABLE, meaning any app can write to the shared preference file.
World readable and writeable files are very dangerous and likely to cause security holes in applications. They should be avoided.
Three other view objects are declared to handle the values entered by the user.
The data from these views is written to the shared preference file within the onClick method of the save button. The line of code below simply gets the text entered in the EditText view.
String userName = userNameText.getText().toString();
The next three lines of code get the id of the checked radio button within the RadioGroup and then the text associated with the checked button. In this example the text will be either: white, slate or green. These colors are defined in the colors.xml file created for tutorial 28.
int selectedId = radioGroupColor.getCheckedRadioButtonId();
radioButtonColor = (RadioButton) findViewById(selectedId);
String color = radioButtonColor.getText().toString();
The final action is to create an editor to place the values into the shared preference file.
SharedPreferences.Editor editor = prefs.edit();
The values are stored as "key value pairs". The key is a unique string identifier used to reference the preference value when retrieving it. The data will not be written to the file until the editor.commit() method is executed.
In the next tutorial we will look at reading the shared preferences.
After this tutorial your SettingsActivity.java file should look similar to below:
Your PreferenceActivity.java file should the one look similar to the one below:
Your activity_settings.xml file should look similar to the one below:
Your activity_preferences.xml file should look similar to the one below:
Your MainActivity.java file should look similar to the one below:
Your AndroidManifest file should look similar to the one below:
Download Download tutorial set 2