Android - creating a custom preferences activity screen
- by Bill Osuch
Android applications can maintain their own internal preferences (and allow them to be modified by users) with very little coding. In fact, you don't even need to write an code to explicitly save these preferences, it's all handled automatically!
Create a new Android project, with an intial activity title Main.
Create two more activities:
ShowPrefs, which extends Activity
Set Prefs, which extends PreferenceActivity
Add these two to your AndroidManifest.xml file:
<activity android:name=".SetPrefs"></activity>
<activity android:name=".ShowPrefs"></activity>
Now we'll work on fleshing out each activity. First, open up the main.xml layout file and add a couple of buttons to it:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:text="Edit Preferences"
android:id="@+id/prefButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
<Button android:text="Show Preferences"
android:id="@+id/showButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
Next, create a couple button listeners in Main.java to handle the clicks and start the other activities:
Button editPrefs = (Button) findViewById(R.id.prefButton);
editPrefs.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), SetPrefs.class);
startActivityForResult(myIntent, 0);
}
});
Button showPrefs = (Button) findViewById(R.id.showButton);
showPrefs.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), ShowPrefs.class);
startActivityForResult(myIntent, 0);
}
});
Now, we'll create the actual preferences layout. You'll need to create a file called preferences.xml inside res/xml, and you'll likely have to create the xml directory as well. Add the following xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
</PreferenceScreen>
First we'll add a category, which is just a way to group similar preferences... sort of a horizontal bar. Add this inside the PreferenceScreen tags:
<PreferenceCategory android:title="First Category">
</PreferenceCategory>
Now add a Checkbox and an Edittext box (inside the PreferenceCategory tags):
<CheckBoxPreference
android:key="checkboxPref"
android:title="Checkbox Preference"
android:summary="This preference can be true or false"
android:defaultValue="false"/>
<EditTextPreference
android:key="editTextPref"
android:title="EditText Preference"
android:summary="This allows you to enter a string"
android:defaultValue="Nothing"/>
The key is how you will refer to the preference in code, the title is the large text that will be displayed, and the summary is the smaller text (this will make sense when you see it).
Let's say we've got a second group of preferences that apply to a different part of the app. Add a new category just below the first one:
<PreferenceCategory android:title="Second Category">
</PreferenceCategory>
In there we'll a list with radio buttons, so add:
<ListPreference
android:key="listPref"
android:title="List Preference"
android:summary="This preference lets you select an item in a array"
android:entries="@array/listArray"
android:entryValues="@array/listValues" />
When complete, your full xml file should look like this:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="First Category">
<CheckBoxPreference
android:key="checkboxPref"
android:title="Checkbox Preference"
android:summary="This preference can be true or false"
android:defaultValue="false"/>
<EditTextPreference
android:key="editTextPref"
android:title="EditText Preference"
android:summary="This allows you to enter a string"
android:defaultValue="Nothing"/>
</PreferenceCategory>
<PreferenceCategory android:title="Second Category">
<ListPreference
android:key="listPref"
android:title="List Preference"
android:summary="This preference lets you select an item in a array"
android:entries="@array/listArray"
android:entryValues="@array/listValues" />
</PreferenceCategory>
</PreferenceScreen>
However, when you try to save it, you'll get an error because you're missing your array definition. To fix this, add a file called arrays.xml in res/values, and paste in the following:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="listArray">
<item>Value 1</item>
<item>Value 2</item>
<item>Value 3</item>
</string-array>
<string-array name="listValues">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
</resources>
Finally (for the preferences screen at least...) add the code that will display the preferences layout to the SetPrefs.java file:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
OK, so now we've got an activity that will set preferences, and save them without the need to write custom save code. Let's throw together an activity to work with the saved preferences. Create a new layout called showpreferences.xml and give it three Textviews:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/textview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="textview1"/>
<TextView
android:id="@+id/textview2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="textview2"/>
<TextView
android:id="@+id/textview3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="textview3"/>
</LinearLayout>
Open up the ShowPrefs.java file and have it use that layout:
setContentView(R.layout.showpreferences);
Then add the following code to load the DefaultSharedPreferences and display them:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
TextView text1 = (TextView)findViewById(R.id.textview1);
TextView text2 = (TextView)findViewById(R.id.textview2);
TextView text3 = (TextView)findViewById(R.id.textview3);
text1.setText(new Boolean(prefs.getBoolean("checkboxPref", false)).toString());
text2.setText(prefs.getString("editTextPref", "<unset>"));;
text3.setText(prefs.getString("listPref", "<unset>"));
Fire up the application in the emulator and click the Edit Preferences button. Set various things, click the back button, then the Edit Preferences button again. Notice that your choices have been saved.
Now click the Show Preferences button, and you should see the results of what you set:
There are two more preference types that I did not include here:
RingtonePreference - shows a radioGroup that lists your ringtones
PreferenceScreen - allows you to embed a second preference screen inside the first - it opens up a new set of preferences when clicked