May 7, 2013

Implement ListPreference in PreferenceFragment

ListPreference A Preference that displays a list of entries as a dialog. This preference will store a string into the SharedPreferences.

ListPreference in PreferenceFragment
ListPreference in PreferenceFragment

Works on previous article of "PreferenceFragment example".

Modify /res/xml/preferences.xml to add <ListPreference>.
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    
    <PreferenceCategory
        android:title="PreferenceCategory CheckBoxPreference">
        <CheckBoxPreference
            android:key="PREF_CHECKBOX"
            android:title="Title"
            android:summary="summary" />
    </PreferenceCategory>
    
    <PreferenceCategory
        android:title="PreferenceCategory ListPreference">
        <ListPreference
            android:key="PREF_LIST"
            android:title="ListPreference title"
            android:summary="ListPreference summary"
            android:entries="@array/listentries"
            android:entryValues="@array/listvalues" />
    </PreferenceCategory>

</PreferenceScreen>


Create /res/values/arrays.xml to define the list items.
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="listentries">
        <item>List item 1</item>
        <item>List item 2</item>
        <item>List item 3</item>
        <item>List item 4</item>
    </string-array>
    <string-array name="listvalues">
        <item>1</item>
        <item>2</item>
        <item>3</item>
        <item>4</item>
    </string-array>
     
</resources>


Keep using PrefFragment.java and PrefActivity.java in last article.

Modify MainActivity.java
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.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
 
 Button buttonSetPreference;
 TextView settingCheckBox, settingList;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  buttonSetPreference = (Button)findViewById(R.id.setpreference);
  settingCheckBox = (TextView)findViewById(R.id.setting_checkbox);
  settingList = (TextView)findViewById(R.id.setting_list);
  
  buttonSetPreference.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    Intent intentSetPref = new Intent(getApplicationContext(), PrefActivity.class);
    startActivityForResult(intentSetPref, 0);
   }});
 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  
  SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
  Boolean prefCheckBox = sharedPreferences.getBoolean("PREF_CHECKBOX", false);
  settingCheckBox.setText("CHECKBOX preference = " + prefCheckBox.toString());
  String prefList = sharedPreferences.getString("PREF_LIST", "no selection");
  settingList.setText("LIST preference = " + prefList);
 }

}


/res/layout/activity_main.xml
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/setpreference"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Set Preference" />
    <TextView
        android:id="@+id/setting_checkbox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/setting_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>


Next:
The default display on summary field is hard-coded in /res/xml/preferences.xml. Such that, the user cannot know the current selected item of ListPreference, without open the ListPreference dialog. To display ListPreference selected item on summary, read it.

No comments:

Post a Comment

Infolinks In Text Ads