Learn About Developing Preference Screens for Your Android App
written by: Jbeerdev•edited by: Simon Hill•updated: 8/8/2011
If there is something that *almost* all Android applications have in common it is the preferences screen. Here you can change the settings of the application. This article will explain how to create these kinds of screens and how to handle the preferences through the application.
slide 1 of 5
Creating a preferences screen in your application like the ones you can find in your Android OS, or in most applications, is really easy. Android provides us with a very powerful system to create these kinds of screens with almost no effort. If you already know how to create standard screens, this is not more difficult. Let's go deeper.
slide 2 of 5
Preferences screen using XML
How is your preference screen going to look? What elements do we need? The first step is designing the screen, and we are going to use XML for that. I will give you useful pieces of code so you can build your own preferences screen according to your needs.
In a common Android screen we can find “LinearLayouts" or “RelativeLayouts" as elements containers. Here, looking at the preferences screen, we will find two kinds of containers:
PreferenceScreen and the PreferenceCategory
The PreferenceScreen tag is the parent container, for every different screen we will have one of these containers. On the other hand, the PreferenceCategory tag is used to create divisions in the screen by category, for example. Here in the following image you can see the divisions I was talking about:
Every gray row is a different category you can define using the PreferenceCategory tag:
This way we have a second and separate preference screen inside the root one. This can be helpful when we want to separate the preferences even more than using categories. Think about the Android OS preference screen, it is a great preference application, with lots of different sections, flows and branches, if you have a big application, this can be useful.
slide 4 of 5
Binding preferences screen with an Activity
Once we have our preference user interface created, we need to bind it to an Activity to make it work. For this purpose we need to create a class that extends from PreferenceActivity
public class PreferencesActivity extends PreferenceActivity
and in the “OnCreate" method, we add the following line:
where “preference_screen" is the file name.xml of the user interface we created in the first instance. That's it, you have your preference XML code binded to your activity.
If you run your application it should look like the following:
HINT! - Remember to add the Activity to the AndroidManifest.
And... what about using these preferences in our application? All the values we have set in the preferences screen are persistents, this means that they are stored inside the phone memory and are not erased until you remove your application. When you select “wifi on" in your device, the Wi-Fi is still on, even if you restart the phone, isn't it?
Getting the application preferences is as simple as: