Compare with the old exercise "Preferences and SharedPreferences" and "EditTextPreference", ListPreference is little bit complicate.

Create a XML file arrays.xml in the folder /res/values/, to define the selection of the list. listDisplayWord define the words to be displayed, listReturnValue define the return value which will be used later.
Create a preferences.xml in the folder /res/xml/
Create SetPreferences.java
Modify main.xml
Main code
Also have to modify AndroidManifest.xml to include SetPreferences.java
Download the files.
For Android 3.0 or higher, you should consider PreferenceFragment with ListPreference.

Create a XML file arrays.xml in the folder /res/values/, to define the selection of the list. listDisplayWord define the words to be displayed, listReturnValue define the return value which will be used later.
- Option 1
- Option 2
- Option 3
- 1 is selected
- 2 is selected
- 3 is selected
Create a preferences.xml in the folder /res/xml/
android:title="List Preference"
android:summary="Select the option of the list"
android:key="listPref"
android:entries="@array/listDisplayWord"
android:entryValues="@array/listReturnValue" />
Create SetPreferences.java
package com.exercise.AndroidListPreference;
import com.exercise.AndroidListPreference.R;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class SetPreferences extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}Modify main.xml
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Preferences:"
/>
android:id="@+id/list_pref"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
android:id="@+id/setpreference"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Set Preference"
/>
Main code
package com.exercise.AndroidListPreference;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class AndroidListPreference extends Activity {
TextView myListPref;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myListPref = (TextView)findViewById(R.id.list_pref);
Button buttonSetPreference = (Button)findViewById(R.id.setpreference);
buttonSetPreference.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(AndroidListPreference.this, SetPreferences.class));
}});
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Toast.makeText(this, "onResume", Toast.LENGTH_LONG).show();
SharedPreferences myPreference=PreferenceManager.getDefaultSharedPreferences(this);
String myListPreference = myPreference.getString("listPref", "default choice");
myListPref.setText(myListPreference);
}
}Also have to modify AndroidManifest.xml to include SetPreferences.java
package="com.exercise.AndroidListPreference"
android:versionCode="1"
android:versionName="1.0">
android:label="@string/app_name">
For Android 3.0 or higher, you should consider PreferenceFragment with ListPreference.