ListPreference

ListPreference is one of default preferences. The user can select one of list items.
This is for simple selection setting

How to use?

Steps

  • Prepare entry key-value xml as res/values/arrays.xml
  • Add ListPreference to preference XML

Prepare entry key-value xml as res/values/arrays.xml

Example res/values/arrays.xml

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

<resources>
    <string-array name="listOrientationKeys">
        <item>@string/portrait</item>
        <item>@string/landscape</item>
    </string-array>
    <string-array name="listOrientationValues">
        <item>0</item>
        <item>1</item>
    </string-array>
</resources>

Warning: You can use only string as value. You cannot use integer-array as values
If you want, you should use Integer.parseInt();

Add ListPreference to preference XML

About preference, you can read my other entry.
The example is here

?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
	xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:nanshu="http://schemas.android.com/apk/res/nanshu">
	<PreferenceCategory android:title="Test">
		<ListPreference 
		    android:title="FlashCardPack Orientation"
		    android:key="orientation"
		    android:defaultValue="0"
		    android:entries="@array/listOrientationKeys"
		    android:entryValues="@array/listOrientationValues"
		/>
	</PreferenceCategory>
</PreferenceScreen>

Result

ListPreference