Android Quick Preferences Tutorial

When developing a mobile application, a common need that arises is the one of storing user’s preferences and using them when the application starts. Since this is a recurring need, Google has created a preferences framework for us. The provided mechanism allows us to show, save and manipulate user’s preferences very easily. Furthermore, the framework has support for automatic UI creation. What I mean is, that, by declaring the type of a user preference, a user interface for manipulating these preferences is automatically generated, without having to write a single line code. How does that sound for rapid application development?

This assumes that we have already created a file named “preferences.xml” inside the “res/xml” folder. Then, in run-time, the activity will inflate the given XML resource and add the preference hierarchy to the current preference hierarchy. This XML file has a specific format via which we define the types of the preferences. All types derive from the Preference class, which represents the basic preference UI building block and provides the View to be displayed in the activity and the association with a SharedPreferences object to store/retrieve the preference data. Here are some of the most common subclasses that you can directly use:

Also bear in mind that various preferences can be grouped into categories by using the PreferenceCategory class, which groups Preference objects and provides a disabled title above the group.

Let’s examine what our XML declaration file will be consisted of. We are going to have two categories. Under the first one, we will have a CheckBoxPreference, which will enable or disable data updates to a hypothetical application and a ListPreference which through which we will define how often we want updates to happen. As you might have guessed, there is a dependency between the two preferences. This can be achieved by using the android:dependency attribute. Finally, under the second category, we will have a EditTextPreference via which a welcome message will be saved and shown when necessary. This is what the XML file looks like:

Note that for the ListPreference, an android:entries attribute is defined. We use that to load predefined values which are kept in an external XML file under the name “res/values/arrays.xml”. The “updateInterval” and “updateIntervalValue” entries are used from that file. These are actually key-value pairs, so the actual value stored is the one in the second list. Here is what the file looks like:

As we explained, the persisting part of the equation is handled by the framework. In order to show you how to read the already persisted values, we will create another activity which will be launched from our main one. Let’s first see how the main activity looks like:

Inside the activity, we take reference of the SharedPreferences class by using the static method getDefaultSharedPreferences of the PreferenceManager class. Then, depending on the data type of the preference, we use the appropriate getter method, such as getBoolean or getString. Note that the second argument to those is a default value which will be used if a a value has not yet been stored. We use as keys those defined in the original “preferences.xml” file, i.e. “perform_updates”, “updates_interval” and “welcome_message”. The values are concatenated and then presented in a TextView.

If we now launch the Eclipse configuration, we will get a preferences screen like the following:

Clicking on the “Updates Interval” will cause a list of options to appear:

Similarly, clicking on the “Welcome Message” preference will cause an edit text to appear:

Now let’s read the already persisted values. Hit the “Menu” button on the emulator so that the options menu appears and choose the “Show current settings” item. This will launch our second activity inside which the values are read and presented as follows:

That’s it guys. A quick guide on how to leverage the preferences framework provided in the SDK to rapidly manipulate user preferences in your application. You can find the Eclipse project created in this tutorial here.

This is a very nice, simple tutorial that explains all the basics in a simple language and provides good examples. You make it seem so easy! Well, I guess it is. Anyways, loved the tutorial, cheers to you :)

How to save the dynamiclly created textbox when a button is clicked.. on each button click a textbox will be added.. so I will not know initally how many text box are created. when the activity is destroyed and again launched it should display with the previous state..i.e the same textboxes which i created before.. please help me ..

Whilst this is a very useful tutorial, I have never seen such a disorganised piece of text-based learning in my life. There is information that is in the wrong place, e,g, the first code snippet is followed by: “This assumes that we have already created a file named “preferences.xml” inside the “res/xml” folder.” This is also followed later by a similar reference to a layout file which is then described after the initial reference. Whilst I appreciate that you are not necessarily wanting to spoon-feed your visitors you could at least put all of the food on the plate at… Read more »

Very useful! I am looking for something where I can make the preferences unreadable for those who are going to decompile the APK. do you know if something like that exists? The reason I want that is because I am calling web services that I don’t want to be used by others and I have not yet found a good way to do it in other ways.

Yeah, that’s right. For example, the item tag doesn’t have a name attribute anymore.

Vote Up0Vote Down Reply

3 years ago

Newsletter

Join Us

With 1,240,600 monthly unique visitors and over 500 authors we are placed among the top Java related sites around. Constantly being on the lookout for partners; we encourage you to join us. So If you have a blog with unique and interesting content then you should check out our JCG partners program. You can also be a guest writer for Java Code Geeks and hone your writing skills!

Disclaimer

All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners. Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.