Letting Users Have Their

Given that you have set up the preference XML, you can use a nearly built-in activity for allowing your users to set their preferences. The activity is "nearly built-in" because you merely need to subclass it and point it to your preference XML, plus hook the activity into the rest of your application.

So, for example, here is the EditPreferences activity of the Prefs/Simple project available on the Apress Web site:

package com.commonsware.android.prefs;

import android.app.Activity;

import android.os.Bundle;

import android.preference.PreferenceActivity;

public class EditPreferences extends PreferenceActivity { @Override public void onCreate(Bundle savedInstanceState) { super. onCreate(savedInstanceState);

addPreferencesFromResource(R.xml.preferences);

As you can see, there is not much to see. All you need to do is call addPreferencesFromResource() and specify the XML resource containing your preferences. You will also need to add this as an activity to your AndroidManifest.xml file:

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.commonsware.android.prefs"> application android:label="@string/app name"> <activity android:name=".SimplePrefsDemo" 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=".EditPreferences" android:label="@string/app name"> </activity> </application> </manifest>

And you will need to arrange to invoke the activity, such as from a menu option, here pulled from SimplePrefsDemo at http://apress.com:

@Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(Menu.NONE, EDIT ID, Menu.NONE, "Edit Prefs") . setIcon(R.drawable.misc) .setAlphabeticShortcut('e'); menu.add(Menu.NONE, CLOSE ID, Menu.NONE, "Close") .setIcon(R.drawable.eject) .setAlphabeticShortcut('c');

return(super.onCreateOptionsMenu(menu));

@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case EDIT ID:

startActivity(new Intent(this, EditPreferences.class)); return(true);

case CLOSE ID: finish(); return(true);

return(super.onOptionsItemSelected(item));

However, that is all that is needed, and it really is not that much code outside of the preferences XML. What you get for your effort is an Android-supplied preference UI, as shown in Figure 17-1.

e3abh«4:26 pm

Checkbox Preference

Check it on, check It off

Ringtone Preference

Pick a tone, any tone

Figure 17-1. The Simple project's preferences UI

The checkbox can be directly checked or unchecked. To change the ringtone preference, just click on the entry in the preference list to bring up a selection dialog like the one in Figure 17-2.

^^ Select a ringtone

Beat Plucker

®

Bell Phone

0

Bentley Dubs

0

Bird Loop

0

Caribbean Ice

o

II " II °BL 1

Figure 17-2. Choosing a ringtone preference

Note that there is no explicit Save or Commit button or menu—changes are persisted as soon as they are made.

The SimplePrefsDemo activity, beyond having the aforementioned menu, also displays the current preferences via a TableLayout:

<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout width="fill parent" android:layout height="fill parent"

<TableRow> <TextView android:text="Checkbox:" android:paddingRight="5px"

<TextView android:id="@+id/checkbox" />

</TableRow> <TableRow> <TextView android:text="Ringtone:" android:paddingRight="5px"

<TextView android:id="@+id/ringtone" />

</TableRow> </TableLayout>

The fields for the table are found in onCreate():

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

checkbox=(TextView)findViewById(R.id.checkbox); ringtone=(TextView)findViewById(R.id.ringtone);

The fields are updated on each onResume():

@Override public void onResume() { super.onResume();

SharedPreferences prefs=PreferenceManager

.getDefaultSharedPreferences(this);

checkbox.setText(new Boolean(prefs

.getBoolean("checkbox", false)) .toString());

ringtone.setText(prefs.getString("ringtone", "<unset>"));

This means the fields will be updated when the activity is opened and after the preferences activity is left (e.g., via the back button); see Figure 17-3.

ftflss 6:19 pm

SimplePrefsDemo

Checkbox: true

Ringtone: content://medla/internal/audlo/media/'

Figure 17-3. The Simple project's list of saved preferences

0 0

Post a comment