Android Programming Tutorials

(Romina) #1

TUTORIAL 13


What's Your Preference?


In this tutorial, we will add a preference setting for the sort order of the


restaurant list. To do this, we will create a PreferenceScreen definition in


XML, load that into a PreferenceActivity, connect that activity to the


application, and finally actually use the preference to control the sort order.


Step-By-Step Instructions.........................................................................


First, you need to have completed the previous tutorial. If you are


beginning the tutorials here, or if you wish to not use your existing work,


you can download a ZIP file with all of the tutorial results, and you can copy


the 12-Activities edition of LunchList to use as a starting point.


Step #1: Define the Preference XML..................................................


First, add a LunchList/res/xml/preferences.xml file as follows:


<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:key="sort_order"
android:title="Sort Order"
android:summary="Choose the order the list uses"
android:entries="@array/sort_names"
android:entryValues="@array/sort_clauses"
android:dialogTitle="Choose a sort order" />
</PreferenceScreen>

135
Free download pdf