Monday, July 9, 2012

PreferenceFragment with ListPreference

Last post describe a simple example of PreferenceFragment with CheckBoxPreference and EditTextPreference. ListPreference will be added on the PreferenceFragment in this exercise.

PreferenceFragment with ListPreference


First of all, create /res/values/arrays.xml to define our arrays of entries (listDisplayWord) and entryValues (listReturnValue).
<?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>


Modify /res/xml/preferences.xml to add <ListPreference>.
<PreferenceScreen
        xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory
            android:title="PreferenceCategory A">

        <CheckBoxPreference
                android:key="checkbox_preference"
                android:title="title_checkbox_preference"
                android:summary="summary_checkbox_preference" />

    </PreferenceCategory>

    <PreferenceCategory
            android:title="PreferenceCategory B">

        <EditTextPreference
                android:key="edittext_preference"
                android:title="title_edittext_preference"
                android:summary="summary_edittext_preference"
                android:dialogTitle="dialog_title_edittext_preference" />

    </PreferenceCategory>
    
    <PreferenceCategory
            android:title="ListPreference">

        <ListPreference
                android:key="list_preference"
                android:title="title_list_preference"
                android:summary="summary_list_preference"
                android:entries="@array/listDisplayWord"
                android:entryValues="@array/listReturnValue"
                android:dialogTitle="dialog_title_list_preference" />

    </PreferenceCategory>

</PreferenceScreen>


Modify /res/layout/activity_main.xml (the main layout) to add a TextView to display ListPreference.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="@dimen/padding_medium"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />
    
    <CheckBox
        android:id="@+id/prefCheckBox"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="CheckBoxPreference" />
    
    <TextView
        android:id="@+id/prefEditText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    
    <TextView
        android:id="@+id/list_pref"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
        
</LinearLayout>


package com.example.androidpreferencefragment;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.TextView;

public class MainActivity extends Activity {
 
 CheckBox prefCheckBox;
 TextView prefEditText;
 TextView myListPref;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        prefCheckBox = (CheckBox)findViewById(R.id.prefCheckBox);
     prefEditText = (TextView)findViewById(R.id.prefEditText);
     myListPref = (TextView)findViewById(R.id.list_pref);
     
     loadPref();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {

  /*
   * Because it's onlt ONE option in the menu.
   * In order to make it simple, We always start SetPreferenceActivity
   * without checking.
   */
  
  Intent intent = new Intent();
        intent.setClass(MainActivity.this, SetPreferenceActivity.class);
        startActivityForResult(intent, 0); 
  
        return true;
 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  // TODO Auto-generated method stub
  //super.onActivityResult(requestCode, resultCode, data);
  
  /*
   * To make it simple, always re-load Preference setting.
   */
  
  loadPref();
 }
    
 private void loadPref(){
  SharedPreferences mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
  
  boolean my_checkbox_preference = mySharedPreferences.getBoolean("checkbox_preference", false);
  prefCheckBox.setChecked(my_checkbox_preference);

  String my_edittext_preference = mySharedPreferences.getString("edittext_preference", "");
     prefEditText.setText(my_edittext_preference);
     
     String myListPreference = mySharedPreferences.getString("list_preference", "1 is selected");
     myListPref.setText(myListPreference);

 }
        
}


Download the files.


2 comments:

Anonymous said...

what is setPreferenceActivity here... you did not declared it.

Erik said...

in last exercise Example of using PreferenceFragment.