What’s Your Preference: Part Two

In a previous post, we covered the basics of the new preference system in Android. To recap: you can define preference characteristics in an XML file. This in turn gives you an Android-generated preference screen, which you can launch from an activity. The results are stored in the SharedPreferences for your application for later retrieval. Today, we will continue our look at the preferences UI, in an excerpt from The Busy Coder’s Guide to Android Development.

If you have a lot of preferences for users to set, having them all in one big list may become troublesome. Android’s preference framework gives you a few ways to impose a bit of structure on your bag of preferences, including categories and screens.

Categories are added via a PreferenceCategory element in your preference XML and are used to group together related preferences. Rather than have your preferences all as children of the root PreferenceScreen, you can put a few PreferenceCategory elements in the PreferenceScreen, and then put your preferences in their appropriate categories. Visually, this adds a divider with the category title between groups of preferences.

If you have lots and lots of preferences — more than is convenient for users to scroll through — you can also put them on separate “screens” by introducing the PreferenceScreen element.

Any children of PreferenceScreen go on their own screen. If you nest PreferenceScreens, the parent screen displays the screen as a placeholder entry — tapping that entry brings up the child screen.

For example, here is a preference XML file that contains both PreferenceCategory and nested PreferenceScreen elements:

[sourcecode language=’xml’]

xmlns:android="http://schemas.android.com/apk/res/android"> android:key="@string/checkbox"
android:title="Checkbox Preference"
android:summary="Check it on, check it off"
/>
android:key="@string/ringtone"
android:title="Ringtone Preference"
android:showDefault="true"
android:showSilent="true"
android:summary="Ping a tone, any tone, even silence"
/>
android:key="detail"
android:title="Detail Screen"
android:summary="Additional preferences held in another page">
android:key="@string/checkbox2"
android:title="Another Checkbox"
android:summary="On. Off. It really doesn't matter."
/>
[/sourcecode]

The result, when you use this preference XML with your PreferenceActivity implementation, is a categorized list of elements:

A preferences UI showing categories and the launch-point for a secondary screen

A preferences UI showing categories and the launch-point for a secondary screen

And, if you tap on the Detail Screen entry, you are taken to the child preference screen:

The secondary screen, launched from the main preferences activity

The secondary screen, launched from the main preferences activity

Next time, in the third and final(?) part of this series of blog posts, we will look at elements that support free-form text entry or a selection of an item out of a list.

About author

AndroidGuys
AndroidGuys 4641 posts

Founded on November 5, 2007, we've enjoyed bringing you the latest in Android news and rumors. Updated daily, we strive to deliver reviews, opinions, and updates on all things related to Android.

You might also like

News and Rumors

Winamp Brings WiFi Sync to Android!

It seems like just yesterday people were complaining that Android’s music systems were lacking in features, and needed a boost to compete. Winamp understands this so they’ve released the latest version of the Android application, adding on WiFi sync support.

News and Rumors

Samsung Galaxy Note 8.0 with AT&T friendly LTE receives the FCC’s approval

Not a fan of the Samsung Galaxy Note 8.0 Wi-Fi only? Not to worry, as the opportunity to buy an LTE version could be coming soon. The FCC just gave

News and Rumors

Sony to host Tablet Q&A and Giveaway on Wednesday

Sony is hosting a live Q&A session later this week, at 3:00PM GMT on Wednesday November 23rd where they will chatting up their new tablets. Join Sony’s tablet expert Omar