Tuesday, August 17, 2010

ListPreference

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

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.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="listDisplayWord">
 <item>Option 1</item>
 <item>Option 2</item>
 <item>Option 3</item>
</string-array>
<string-array name="listReturnValue">
 <item>1 is selected</item>
 <item>2 is selected</item>
 <item>3 is selected</item>
</string-array>
</resources>


Create a preferences.xml in the folder /res/xml/
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
 <ListPreference
  android:title="List Preference"
  android:summary="Select the option of the list"
  android:key="listPref"
  android:entries="@array/listDisplayWord"
  android:entryValues="@array/listReturnValue" />
</PreferenceScreen>


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
<?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:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/hello"
   />
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Preferences:"
   />
<TextView
   android:id="@+id/list_pref"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   />
<Button
 android:id="@+id/setpreference"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="Set Preference"
 />
</LinearLayout>


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
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.exercise.AndroidListPreference"
     android:versionCode="1"
     android:versionName="1.0">
   <application android:icon="@drawable/icon" android:label="@string/app_name">
       <activity android:name=".AndroidListPreference"
                 android:label="@string/app_name">
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
       </activity>
  <activity android:name=".SetPreferences" />
   </application>
   <uses-sdk android:minSdkVersion="4" />

</manifest>


Download the files.

For Android 3.0 or higher, you should consider PreferenceFragment with ListPreference.


No comments: