Tuesday, October 18, 2011

Android: How The Preference Framework Works

Every good application needs preferences that enables the user to customize and personalize it to his needs.
Android provides us with a preference framework ready to use. Like the rest of the UI, you have the choice to define your preference declaratively or programmatically. Android stores the preferences as key-value pairs of primitive data types in a shared preferences object.

We will develope a preference activity consisting of two categories, each with a preference in it. The first categorie is defined declarativeley in an XML document while the second is defined programmatically.

The declarative Way of creating preferences
Start a new Android project like I described it in Part0-2 of my 'Getting Started With Android' tutorial.

At first we have to layout the preference screen through an xml file. This file comes by convention in the folder res/xml that we have to create.

Create a new 'Android XML File' from the context menue of the folder. When the dialog pops up, select the resource type 'Preferences', name it my_preferences.xml and choose PreferenceScreen as the root element.

 <?xml version="1.0" encoding="utf-8"?>  

The preferences UI consists of three basic class types.
It is a container type. It can hold childs of the type PreferenceCategory, Preference and even PreferenceScreen itself to build up a nested preference UI.
Groups Preferences together under one common title.
The actual preference that is a build in (EditTextPreference, CheckBoxPreference, ...) or custom preference.

 <?xml version="1.0" encoding="utf-8"?>  
        android:title="category 1"   

For the sake of getting things done quick in tutorials I used plain strings and not the string.xml from the resources.
To bring the my_preferences.xml to the screen, extend an Activity from the class PreferenceActivity. In the onCreate() methode, inflate the ui from the file.

 public class MyPreferenceActivity extends PreferenceActivity {  
      protected void onCreate(Bundle savedInstanceState) {  
           EditTextPreference etpUsername = (EditTextPreference)

Dont't forget to add the Activity via the AndroidManifest.xml otherwise the app will crash when it can't find it.


Now we have to implement the code that opens the MyPreferenceActivity from the options menue.
Create a new menu file 'my_menu.xml' in the folder res/menu.

 <?xml version="1.0" encoding="utf-8"?>  
   <item android:title="@string/menu_item_preferences"   

In the main Activity we override the following callback methods.

   public boolean onCreateOptionsMenu(Menu menu) {  
        MenuInflater mi = getMenuInflater();  
        mi.inflate(R.menu.my_menu, menu);  
        return true;  
   public boolean onMenuItemSelected(int featureId, MenuItem item) {  
        switch(item.getItemId()) {  
             case R.id.menu_preferences:  
                  Intent i = new Intent(this, MyPreferenceActivity.class);  
                  return true;  
        return super.onMenuItemSelected(featureId, item);  

Start your Application and have a look at our new preferences screen with the first category.

The programmatic Way of creating preferences
Go back to the MyPreferenceActivity and add the following lines at the end of the onCreate() methode.
They will add a second PreferencCategory called 'category 2' with a CheckBoxPreference in it.

     protected void onCreate(Bundle savedInstanceState) {  
         PreferenceManager preferenceManager = getPreferenceManager();  
         PreferenceScreen preferenceScreen = 
                  (PreferenceScreen) preferenceManager.findPreference("prefscreen");  
         PreferenceCategory prefCat2 = new PreferenceCategory(this);  
         prefCat2.setTitle("category 2");  
         CheckBoxPreference checkBoxPreference = new CheckBoxPreference(this);  

With the help of the PreferenceManager we can obtain any preference that is declared so far. We fetch the PreferenceScreen because we want to add another PreferenceCategory at the end. To create an object of one of the preference types, just call the constructor of its class.
Plugging together the hierachy is done with the addPreference() method.

Start your Application and have a look at our new preferences screen.

No comments:

Post a Comment