Tuesday, April 2, 2013

5 How to use preferences in android

From Developer.android.com: “android.preference package provides classes that manage application preferences and implement the preferences UI. Using these ensures that all the preferences within each application are maintained in the same manner and the user experience is consistent with that of the system and other applications.
The preferences portion of an application should be ran as a separate Activity that extends the PreferenceActivity class. In the PreferenceActivity, a PreferenceScreen object should be the root element of the layout. The PreferenceScreen contains Preference elements such as a CheckBoxPreference, EditTextPreference, ListPreference, PreferenceCategory, or RingtonePreference..."

In this tutorial we want to introduce you how to use some preference elements with their tasks in a setting activity externs the PreferenceActivity class. Those are some simple tasks setting for backup data like the photo shows in below:

1. Create layout file for preference settings
To make the setting interface we create a preference.xml file in folder res/layout/xml. All xml code to make that show below:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">  
                    <!-- 1 -->
                            android:title="Store Backup Settings"
                                android:layout="@layout/textlayout" >
                                android:title="Store at default location"
                                android:summaryOff="Check to enable choose location backup"
                                android:summaryOn="Check to disable choose location backup"   
                            <Preference android:key="pref_folder_chosen"
                                                    android:summary="Choose a folder on SDCard to store backup file"                                         
                                                    android:title="Choose location backup"                                     
                    <!-- 2 -->
                            android:title="Auto Backup Settings"
                            <CheckBoxPreference android:key="checkboxpref_auto_backup"
                                android:title="Enable Auto Backup Settings"
                                android:summaryOff="Check to enable automatic backup in device"
                                android:summaryOn="Check to disable automatic backup in device" />
                            <PreferenceScreen android:title="Auto Backup Settings"
                                android:summary="Setting Date and Time for automatic backup"
                                    <PreferenceCategory android:title="SMS BACKUP SCHEDULE"
                                                android:name="Default notification time"
                                                android:summary="Choose the time to automatic backup SMS"
                                                android:title="Time to auto backup SMS"
                                                android:layout="@layout/mylayout" />
                                                android:dialogTitle="Day in week dialog"
                                                android:summary="Choose the date to automatic backup SMS"
                                                android:title="Day to auto backup SMS"
                                                android:layout="@layout/mylayout" />
                    <!-- 3 -->
                                android:title="Restore Settings"
                                android:title="Restore Type"
                                android:summary="Choose the Type of restore."
                                android:defaultValue="Keep current contains"   
                    <!-- 4 -->
                                android:title="Other Settings"
                            <Preference android:key="backup_setting_file"
                                            android:summary="Choose to backup all settings to file"                                          
                                            android:title="Backup all settings"                                        

2. Demo with all preferences element.
2.1. For the CheckBoxPreference element  with android:key="checkboxpref_location_backup". This check for enable or disable users to choose the location in sdcard of device to store their backup file.
When disable

and when enable

2.2. For the  Preference element with android:key="pref_folder_chosen".  When user click on this setting then the app run the File explorer for choose one directory to store the backup file

When the File Explorer show up, if you choose the DCIM folder and return with back button on device

You will get the new location like that:

2.3. The CheckBoxPreference with  android:key="checkboxpref_auto_backup" enable or disable autto backup.
If users enable auto bakup then they can choose the days and time for that.

and when user click on Auto Backup Settings, the new PreferenceScreen will show like that:

 Now the time for user to choose the days and time for auto backup.
2.4. choose the time with the custom time preference element duytan.edu.vn.backupdemo.TimePreferenceCustom
with key timepref_auto_backup. and you can get the string for the time backup.

2.5. By the same with Date backup

Here is some demo for how to use simple preference elements in android. All reference source codes are available here. I hope this help for someone had began with preference class in android. Thanks for look at my blog. If you get confused with my poor English, please put a comment


  1. superb really this is very informative post.....thanks for sharing information related to
    develop for android

  2. Can wa have the layout XML file ?

  3. Link download all source code: