Android application often needs settings that allow users to modify preferences in app. Android provides a powerful framework to manage user preferences. It allows us to define the way we want preferrences and it automatically generates UI for that. All we need to do is to simply use it in our app.
Let us see in this tutorial how to integrate Android’s Preference framework (PreferenceActivity) in any android app.
Create a Hello World Android project in Eclipse. Go to New > Project > Android Project. Give the project name as Android_Preferences_example and select Android Runtime 2.1 or sdk 7.
This will generate a basic skeleton of project. But once we add all required source code the project structure would looks like following:
Once you are done with above steps, you will have a basic hello world Android App.
The Preference framework comes with an activity class android.preference.PreferenceActivity
which needs to be overridden with our own class. Create a class UserSettingsActivity under package net.viralpatel.android where all activities are stored for this app. Copy following code into it.
In above class, we are adding preferences to the app using addPreferencesFromResource method. We specified an xml file id with this method.
Create a new XML file under /xml directory class settings.xml and copy following code into it.
Note how we defined the user interface of settings page in above xml file. We created two different Preference categories, One which shows Users Profile related settings and another Update related settings. The setting page has 3 different components to specify user settings.
EditTextPreference is used to define an editable text property. In this example username is editable property.
CheckBoxPreference allows you to define a boolean value in preference screen. In this example we define a property to check weather to send crash report or not.
ListPreference as its name suggest allows user to select any one item from a given list. In this example we let user to decide what should be the frequency for data sync.
The list we defined in above Preference settings file will be declared as an array in arrays.xml. Create a file arrays.xml (if not present already in your project) in /res/values folder and copy following content into it:
<?xml version="1.0" encoding="utf-8"?><resources><string-arrayname="syncFrequency"><itemname="1">Once a day</item><itemname="7">Once a week</item><itemname="3">Once a year</item><itemname="0">Never (Update maually)</item></string-array><string-arrayname="syncFrequencyValues"><itemname="1">1</item><itemname="7">7</item><itemname="30">30</item><itemname="0">0</item></string-array></resources>
In addition to arrays.xml, we will also need some string values for our app. Below is the list of string constant that we will add in strings.xml file.
<resources><stringname="app_name">Android_Preferences_example</string><stringname="hello_world">Hello world!</string><stringname="menu_settings">Settings</string><stringname="title_activity_main">MainActivity</string><stringname="pref_send_report">Send crash reports</string><stringname="pref_send_report_summary">Helps to fix bugs</string><stringname="pref_sync_frequency">Sync frequency</string><stringname="pref_sync_frequency_summary">Set the sync frequency</string><stringname="pref_user_name">Set username</string><stringname="pref_user_name_summary">Set your username</string><stringname="pref_user_profile">User Profile</string><stringname="pref_update_setting">Update Settings</string></resources>
We have now created the basic setup for Preferences in our app. Now we need to integrate it with our actual Activity.
First we need to create a menu which will be shown when User clicks menu button. This menu will have only one menuitem, Settings. On click on this item, we will display the Settings preference screen.
Create a file settings.xml under /res/menu folder and copy following content into it: