Recipe Using the Preferences Framework

Android provides a standardized framework for setting preferences across all applications. The framework uses category preferences and screens to group related settings. PreferenceCategory is used to declare a set of preferences into one category PreferenceScreen presents a group of preferences in a new screen.

This recipe uses the preferences defined in the XML file in Listing 9.1.A PreferenceScreen is the root element with two EditTextPreference elements for username and password. Other possible elements are CheckBoxPreference, RingtonePreference, and DialogPreference.The Android system then generates a UI to manipulate the preferences, as shown in Figure 9.1.These preferences are stored in shared preferences, which means they can be retrieved by calling getPreferences().

Listing 9.1 res/xml/preferences.xml

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

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <EditTextPreference android:title="User Name" android:key="username" android:summary="Please provide user name"></EditTextPreference>

<EditTextPreference android:title="Password" android:password="true" android:key="password" android:summary="Please enter your password"></EditTextPreference> </PreferenceScreen>

Then, an activity extending the PreferenceActivity calls the addPreferencesFromResource() method to include these preferences in the activity, as shown in Listing 9.2.

Listing 9.2 src/com/cookbook/datastorage/MyPreferences.java package com.cookbook.datastorage; import android.os.Bundle;

import android.preference.PreferenceActivity;

public class MyPreferences extends PreferenceActivity {

^Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

addPreferencesFromResource(R.xml.preferences);

The main activity merely needs to launch the PreferenceActivity when needed (for example, when the Menu button is pressed). Listing 9.3 shows the simple example of showing the preferences upon startup of the activity

Listing 9.3 src/com/cookbook/datastorage/DataStorage.java package com.cookbook.datastorage;

import android.app.Activity; import android.content.Intent; import android.os.Bundle;

public class DataStorage extends Activity {

/** Called when the activity is first created. */ ^Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

Intent i = new Intent(this, MyPreferences.class); startActivity(i);

The AndroidManifest XML file needs to include all activities, including the new PreferenceActivity, as shown in Listing 9.4.

Listing 9.4 AndroidManifest.xml

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cookbook.datastorage" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".DataStorage"

android:label="@string/app_name"> <intent-filter>

<action android:name="android.intent.action. MAIN" /> <category android:name="android.intent.category.LAUNCHER"

</intent-filter> </activity>

<activity android:name=".MyPreferences" />

</application>

<uses-sdk android:minSdkVersion="7" /> </manifest>

This creates the preferences screen shown in Figure 9.1.

User Name

Please provide user name

Password

Please eriler your password

Figure 9.1 The preferences UI generated by the Android system from an XML preferences file.

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook


Post a comment