[source code] Viewpager containing preferencescreens

Search This thread

Fr4gg0r

Inactive Recognized Developer
Aug 4, 2010
698
679
Hi,
since the compability package does not ship PreferenceListFragment, I built my own one, which I am using in my gtalk widget app.

I was asked for the source, so here it is. It's quite a hacky workaround.. ;)

Edit: Someone asked about the license: Use it however you like ;)

Code:
public class PreferenceListFragment extends ListFragment{
    
    private PreferenceManager mPreferenceManager;
    
    /**
     * The starting request code given out to preference framework.
     */
    private static final int FIRST_REQUEST_CODE = 100;
    
    private static final int MSG_BIND_PREFERENCES = 0;
    private Handler mHandler = new Handler() {
        [user=439709]@override[/user]
        public void handleMessage(Message msg) {
            switch (msg.what) {
                
                case MSG_BIND_PREFERENCES:
                    bindPreferences();
                    break;
            }
        }
    };
    private ListView lv;
    private int xmlId;
    
    public PreferenceListFragment(int xmlId){
        this.xmlId = xmlId;
    }
    //must be provided
    public PreferenceListFragment(){
        
    }
    
    [user=439709]@override[/user]
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle b){
        postBindPreferences();
        return lv;
    }
    
    [user=439709]@override[/user]
    public void onDestroyView(){
        super.onDestroyView();
        ViewParent p = lv.getParent();
        if(p != null)
            ((ViewGroup)p).removeView(lv);
    }

    [user=439709]@override[/user]
    public void onCreate(Bundle b) {
        super.onCreate(b);
        if(b != null)
            xmlId = b.getInt("xml");
        mPreferenceManager = onCreatePreferenceManager();
        lv = (ListView) LayoutInflater.from(getActivity()).inflate(R.layout.preference_list_content, null);
        lv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        addPreferencesFromResource(xmlId);
        postBindPreferences();
        ((OnPreferenceAttachedListener)getActivity()).onPreferenceAttached(getPreferenceScreen(), xmlId);
    }

    [user=439709]@override[/user]
    public void onStop(){
        super.onStop();
        try{
            Method m = PreferenceManager.class.getDeclaredMethod("dispatchActivityStop");
            m.setAccessible(true);
            m.invoke(mPreferenceManager);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    [user=439709]@override[/user]
    public void onDestroy() {
        super.onDestroy();
        lv = null;
        try{
            Method m = PreferenceManager.class.getDeclaredMethod("dispatchActivityDestroy");
            m.setAccessible(true);
            m.invoke(mPreferenceManager);
           }catch(Exception e){
               e.printStackTrace();
           }
    }

    [user=439709]@override[/user]
    public void onSaveInstanceState(Bundle outState) {
        outState.putInt("xml", xmlId);
        super.onSaveInstanceState(outState);

    }

    [user=439709]@override[/user]
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try{
            Method m = PreferenceManager.class.getDeclaredMethod("dispatchActivityResult", int.class, int.class, Intent.class);
            m.setAccessible(true);
            m.invoke(mPreferenceManager, requestCode, resultCode, data);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    /**
     * Posts a message to bind the preferences to the list view.
     * <p>
     * Binding late is preferred as any custom preference types created in
     * {@link #onCreate(Bundle)} are able to have their views recycled.
     */
    private void postBindPreferences() {
        if (mHandler.hasMessages(MSG_BIND_PREFERENCES)) return;
        mHandler.obtainMessage(MSG_BIND_PREFERENCES).sendToTarget();
    }
    
    private void bindPreferences() {
        final PreferenceScreen preferenceScreen = getPreferenceScreen();
        if (preferenceScreen != null) {
            preferenceScreen.bind(lv);
        }
    }
    
    /**
     * Creates the {@link PreferenceManager}.
     * 
     * [user=2056652]@return[/user] The {@link PreferenceManager} used by this activity.
     */
    private PreferenceManager onCreatePreferenceManager() {
        try{
            Constructor<PreferenceManager> c = PreferenceManager.class.getDeclaredConstructor(Activity.class, int.class);
            c.setAccessible(true);
            PreferenceManager preferenceManager = c.newInstance(this.getActivity(), FIRST_REQUEST_CODE);
            return preferenceManager;
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }
    
    /**
     * Returns the {@link PreferenceManager} used by this activity.
     * [user=2056652]@return[/user] The {@link PreferenceManager}.
     */
    public PreferenceManager getPreferenceManager() {
        return mPreferenceManager;
    }

    /**
     * Sets the root of the preference hierarchy that this activity is showing.
     * 
     * [user=955119]@param[/user] preferenceScreen The root {@link PreferenceScreen} of the preference hierarchy.
     */
    public void setPreferenceScreen(PreferenceScreen preferenceScreen){
        try{
            Method m = PreferenceManager.class.getDeclaredMethod("setPreferences", PreferenceScreen.class);
            m.setAccessible(true);
            boolean result = (Boolean) m.invoke(mPreferenceManager, preferenceScreen);
            if (result && preferenceScreen != null) {
                postBindPreferences();
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    /**
     * Gets the root of the preference hierarchy that this activity is showing.
     * 
     * [user=2056652]@return[/user] The {@link PreferenceScreen} that is the root of the preference
     *         hierarchy.
     */
    public PreferenceScreen getPreferenceScreen(){
        try{
            Method m = PreferenceManager.class.getDeclaredMethod("getPreferenceScreen");
            m.setAccessible(true);
            return (PreferenceScreen) m.invoke(mPreferenceManager);
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }
    
    /**
     * Adds preferences from activities that match the given {@link Intent}.
     * 
     * [user=955119]@param[/user] intent The {@link Intent} to query activities.
     */
    public void addPreferencesFromIntent(Intent intent) {
        throw new RuntimeException("too lazy to include this bs");
    }
    
    /**
     * Inflates the given XML resource and adds the preference hierarchy to the current
     * preference hierarchy.
     * 
     * [user=955119]@param[/user] preferencesResId The XML resource ID to inflate.
     */
    public void addPreferencesFromResource(int preferencesResId) {   
        try{
            Method m = PreferenceManager.class.getDeclaredMethod("inflateFromResource", Context.class, int.class, PreferenceScreen.class);
            m.setAccessible(true);
            PreferenceScreen prefScreen = (PreferenceScreen) m.invoke(mPreferenceManager, getActivity(), preferencesResId, getPreferenceScreen());
            setPreferenceScreen(prefScreen);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    /**
     * Finds a {@link Preference} based on its key.
     * 
     * [user=955119]@param[/user] key The key of the preference to retrieve.
     * [user=2056652]@return[/user] The {@link Preference} with the key, or null.
     * [user=690402]@see[/user] PreferenceGroup#findPreference(CharSequence)
     */
    public Preference findPreference(CharSequence key) {
        if (mPreferenceManager == null) {
            return null;
        }
        return mPreferenceManager.findPreference(key);
    }
    
    public interface OnPreferenceAttachedListener{
        public void onPreferenceAttached(PreferenceScreen root, int xmlId);
    }
    
}
How to use:

Code:
public class Settings extends FragmentActivity implements OnPreferenceAttachedListener, OnPreferenceChangeListener, OnPreferenceClickListener{
    private Preference filterPref;
    private ViewPager viewPager;
    
    public void onCreate(Bundle b){
        super.onCreate(b);
        setContentView(R.layout.settings);
        viewPager = (ViewPager) findViewById(R.id.pager);
        PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), this);
        viewPager.setAdapter(adapter);
        
        TitlePageIndicator titleIndicator = (TitlePageIndicator) findViewById(R.id.titles);
        titleIndicator.setViewPager(viewPager);
        titleIndicator.setFooterIndicatorStyle(IndicatorStyle.Underline);
        
        viewPager.setCurrentItem(1);
        
    }
    
    //setup your onPreferenceClickListener/onPreferenceChangeListener here
    [user=439709]@override[/user]
    public void onPreferenceAttached(PreferenceScreen root, int xmlId){
        if(root == null)
           return; //for whatever reason in very rare cases this is null
        if(xmlId == R.xml.widget_settings){        //example id    
            root.findPreference("somePreference").setOnPreferenceClickListener(this);
        }
    }
    
    //handle your preferenceChanged events here (if needed)
    [user=439709]@override[/user]
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        return true;
    }
    
    //handle your preferenceClick events here (if needed)
    [user=439709]@override[/user]
    public boolean onPreferenceClick(Preference pref){
        return true;
    }    

}
and the adapter I am using:
Code:
public class PagerAdapter extends FragmentPagerAdapter implements TitleProvider{

    PreferenceListFragment[] fragments;
    String[] titles;
    
    public PagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        fragments = new PreferenceListFragment[4];
        fragments[0] = new PreferenceListFragment(R.xml.settings);
        fragments[1] = new PreferenceListFragment(R.xml.widget_settings);
        fragments[2] = new PreferenceListFragment(R.xml.s_widget_settings);
        fragments[3] = new PreferenceListFragment(R.xml.color_settings);
        
        titles = new String[4];
        titles[0] = context.getString(R.string.main_settings);
        titles[1] = context.getString(R.string.widget_settings);
        titles[2] = context.getString(R.string.s_widget_settings);
        titles[3] = context.getString(R.string.color_settings_main);
        
    }

    [user=439709]@override[/user]
    public Fragment getItem(int position){
        return fragments[position];
    }

    [user=439709]@override[/user]
    public int getCount() {
        return fragments.length;
    }

    [user=439709]@override[/user]
    public String getTitle(int position) {
        return titles[position];
    }


    
}

Lastly you will need this xml file: preference_list_content.xml (belongs into /res/layout/) (pulled from aosp)
Code:
<?xml version="1.0" encoding="utf-8"?>
<!--
/* //device/apps/common/assets/res/layout/list_content.xml
**
** Copyright 2006, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License"); 
** you may not use this file except in compliance with the License. 
** You may obtain a copy of the License at 
**
**     http://www.apache.org/licenses/LICENSE-2.0 
**
** Unless required by applicable law or agreed to in writing, software 
** distributed under the License is distributed on an "AS IS" BASIS, 
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
** See the License for the specific language governing permissions and 
** limitations under the License.
*/
-->
<ListView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/list"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:drawSelectorOnTop="false"
    android:scrollbarAlwaysDrawVerticalTrack="true"
    />
 
Last edited:

Waza_Be

Inactive Recognized Developer
Jul 22, 2009
822
234
Working fine on my devices (Nexus S and GNex), but received logs with crashes here:

Code:
((OnPreferenceAttachedListener)getActivity()).onPreferenceAttached(getPreferenceScreen(), xmlId);

NullPointerException here:

Code:
@Override
	public void onPreferenceAttached(PreferenceScreen root, int xmlId) {
                List<PreferenceScreen> rootPref=new ArrayList<PreferenceScreen>() ;
		rootPref.add(root);
		root.getSharedPreferences().registerOnSharedPreferenceChangeListener(
				this);
 
Last edited:

dragondgold

Member
Jun 24, 2011
35
1
tmax.net16.net
Hello, i ma new at this and i am trying to understand the code, what exactly are "R.array.display_filter_array" and "R.layout.preference_list_content", these are the only things i have left to try this code, hope you can help me :)

Thank you :)
 
Last edited:

Fr4gg0r

Inactive Recognized Developer
Aug 4, 2010
698
679
I added the missing layout file in the first post.
I also added a check for nullpointer in onPreferenceAttached().
R.array.display_filter_array correspondends to a string array I defined.

getResources().getStringArray(int stringId) will return an array of localized strings defined in res/values/strings.xml.
 

jdkoren

Senior Member
Jan 4, 2011
373
43
Silicon Valley
Thank you so much for this! I've been wracking my brain for a long time to come up with a way to use a PreferenceFragment on pre-Honeycomb devices, and this fits my needs exactly. So much love.

I'll let you know if I run across any issues.
 

Viish

Member
Jun 30, 2010
42
1
Grenoble
linphone.org
Hi.

First I'd like to thank you for sharing this.

I have an issue with this : each preference row is only taking the width it's really nead, instead of using all width available like any Preference view. The result is kinda ugly (widgets of preferences not aligned in the right and click area way too little).
But if I start a new Activity and come back, they are well displayed. The only difference between the code I use and yours is I don't use a ViewPage, I directly use the ListFragment as Fragment.

Can someone help me please ?

Thanks in advance.
 

bigrushdog

Inactive Recognized Developer
Apr 23, 2007
3,547
7,244
Hey guys, I just wanted to share a different initialization scheme. I couldn't get the activity to survive orientation change without fc. So i came up with this...

Extended PreferenceFragment
Code:
package org.teameos.settings.device;

import android.os.Bundle;

public class DiagPreferences extends PreferenceListFragment {

    public static DiagPreferences newInstance(int xml) {
        DiagPreferences f = new DiagPreferences(xml);
        Bundle b = new Bundle();
        b.putInt("xml", xml);
        f.setArguments(b);
        return f;
    }

    public DiagPreferences(int xmlId) {
        // TODO Auto-generated constructor stub
        super(xmlId);
    }

    public DiagPreferences() {
        super();
    }
The adapter
Code:
    public ViewPagerAdapter(Context context, FragmentManager fm) {
        super(fm);
        mContext = context;
        mFragments = new ArrayList<Pair<Fragment, String>>();
        mFragments.add(Pair.create(
                (Fragment) UpdatesPreferences.newInstance(R.xml.updates_and_menus),
                mContext.getString(R.string.eos_ota_menus)));
        mFragments.add(Pair.create((Fragment) DiagPreferences.newInstance(R.xml.diagnostics),
                mContext.getString(R.string.eos_diag)));
        mFragments.add(Pair.create((Fragment) PrlPreferences.newInstance(R.xml.prl_management),
                mContext.getString(R.string.prl_management)));
    }
 
  • Like
Reactions: jug6ernaut

zen kun

Senior Member
Jan 23, 2010
1,874
387
Mexico City
forum.xda-developers.com
using the above code (you cant use constructors) and using newInstance its not working
you need modify this in Oncreate

Code:
   super.onCreate(b);
        if(b != null){
            xmlId = b.getInt("xml");
        }else
        {
        	xmlId = getArguments().getInt("xml");
        }
This code gave me a warning about memory leak we shouls use WeakReference or use as static
Code:
 private Handler mHandler = new Handler() {
          [user=439709]@override[/user]
        public void handleMessage(Message msg) {
            switch (msg.what) {
                
                case MSG_BIND_PREFERENCES:
                    bindPreferences();
                    break;
            }
        }
    };
like this:
Code:
private [B][COLOR="Red"]static[/COLOR] [/B]Handler mHandler = new Handler() {
          [user=439709]@override[/user]
        public void handleMessage(Message msg) {
            switch (msg.what) {
                
                case MSG_BIND_PREFERENCES:
                    bindPreferences();
                    break;
            }
        }
    };
and in all "dependencies" of that function like bindPreferences and so on.
Ps AMAZING Work BTW thanks a lot :)
 
Last edited:

Top Liked Posts

  • There are no posts matching your filters.
  • 19
    Hi,
    since the compability package does not ship PreferenceListFragment, I built my own one, which I am using in my gtalk widget app.

    I was asked for the source, so here it is. It's quite a hacky workaround.. ;)

    Edit: Someone asked about the license: Use it however you like ;)

    Code:
    public class PreferenceListFragment extends ListFragment{
        
        private PreferenceManager mPreferenceManager;
        
        /**
         * The starting request code given out to preference framework.
         */
        private static final int FIRST_REQUEST_CODE = 100;
        
        private static final int MSG_BIND_PREFERENCES = 0;
        private Handler mHandler = new Handler() {
            [user=439709]@override[/user]
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    
                    case MSG_BIND_PREFERENCES:
                        bindPreferences();
                        break;
                }
            }
        };
        private ListView lv;
        private int xmlId;
        
        public PreferenceListFragment(int xmlId){
            this.xmlId = xmlId;
        }
        //must be provided
        public PreferenceListFragment(){
            
        }
        
        [user=439709]@override[/user]
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle b){
            postBindPreferences();
            return lv;
        }
        
        [user=439709]@override[/user]
        public void onDestroyView(){
            super.onDestroyView();
            ViewParent p = lv.getParent();
            if(p != null)
                ((ViewGroup)p).removeView(lv);
        }
    
        [user=439709]@override[/user]
        public void onCreate(Bundle b) {
            super.onCreate(b);
            if(b != null)
                xmlId = b.getInt("xml");
            mPreferenceManager = onCreatePreferenceManager();
            lv = (ListView) LayoutInflater.from(getActivity()).inflate(R.layout.preference_list_content, null);
            lv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
            addPreferencesFromResource(xmlId);
            postBindPreferences();
            ((OnPreferenceAttachedListener)getActivity()).onPreferenceAttached(getPreferenceScreen(), xmlId);
        }
    
        [user=439709]@override[/user]
        public void onStop(){
            super.onStop();
            try{
                Method m = PreferenceManager.class.getDeclaredMethod("dispatchActivityStop");
                m.setAccessible(true);
                m.invoke(mPreferenceManager);
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    
        [user=439709]@override[/user]
        public void onDestroy() {
            super.onDestroy();
            lv = null;
            try{
                Method m = PreferenceManager.class.getDeclaredMethod("dispatchActivityDestroy");
                m.setAccessible(true);
                m.invoke(mPreferenceManager);
               }catch(Exception e){
                   e.printStackTrace();
               }
        }
    
        [user=439709]@override[/user]
        public void onSaveInstanceState(Bundle outState) {
            outState.putInt("xml", xmlId);
            super.onSaveInstanceState(outState);
    
        }
    
        [user=439709]@override[/user]
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            try{
                Method m = PreferenceManager.class.getDeclaredMethod("dispatchActivityResult", int.class, int.class, Intent.class);
                m.setAccessible(true);
                m.invoke(mPreferenceManager, requestCode, resultCode, data);
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    
        /**
         * Posts a message to bind the preferences to the list view.
         * <p>
         * Binding late is preferred as any custom preference types created in
         * {@link #onCreate(Bundle)} are able to have their views recycled.
         */
        private void postBindPreferences() {
            if (mHandler.hasMessages(MSG_BIND_PREFERENCES)) return;
            mHandler.obtainMessage(MSG_BIND_PREFERENCES).sendToTarget();
        }
        
        private void bindPreferences() {
            final PreferenceScreen preferenceScreen = getPreferenceScreen();
            if (preferenceScreen != null) {
                preferenceScreen.bind(lv);
            }
        }
        
        /**
         * Creates the {@link PreferenceManager}.
         * 
         * [user=2056652]@return[/user] The {@link PreferenceManager} used by this activity.
         */
        private PreferenceManager onCreatePreferenceManager() {
            try{
                Constructor<PreferenceManager> c = PreferenceManager.class.getDeclaredConstructor(Activity.class, int.class);
                c.setAccessible(true);
                PreferenceManager preferenceManager = c.newInstance(this.getActivity(), FIRST_REQUEST_CODE);
                return preferenceManager;
            }catch(Exception e){
                e.printStackTrace();
                return null;
            }
        }
        
        /**
         * Returns the {@link PreferenceManager} used by this activity.
         * [user=2056652]@return[/user] The {@link PreferenceManager}.
         */
        public PreferenceManager getPreferenceManager() {
            return mPreferenceManager;
        }
    
        /**
         * Sets the root of the preference hierarchy that this activity is showing.
         * 
         * [user=955119]@param[/user] preferenceScreen The root {@link PreferenceScreen} of the preference hierarchy.
         */
        public void setPreferenceScreen(PreferenceScreen preferenceScreen){
            try{
                Method m = PreferenceManager.class.getDeclaredMethod("setPreferences", PreferenceScreen.class);
                m.setAccessible(true);
                boolean result = (Boolean) m.invoke(mPreferenceManager, preferenceScreen);
                if (result && preferenceScreen != null) {
                    postBindPreferences();
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        
        /**
         * Gets the root of the preference hierarchy that this activity is showing.
         * 
         * [user=2056652]@return[/user] The {@link PreferenceScreen} that is the root of the preference
         *         hierarchy.
         */
        public PreferenceScreen getPreferenceScreen(){
            try{
                Method m = PreferenceManager.class.getDeclaredMethod("getPreferenceScreen");
                m.setAccessible(true);
                return (PreferenceScreen) m.invoke(mPreferenceManager);
            }catch(Exception e){
                e.printStackTrace();
                return null;
            }
        }
        
        /**
         * Adds preferences from activities that match the given {@link Intent}.
         * 
         * [user=955119]@param[/user] intent The {@link Intent} to query activities.
         */
        public void addPreferencesFromIntent(Intent intent) {
            throw new RuntimeException("too lazy to include this bs");
        }
        
        /**
         * Inflates the given XML resource and adds the preference hierarchy to the current
         * preference hierarchy.
         * 
         * [user=955119]@param[/user] preferencesResId The XML resource ID to inflate.
         */
        public void addPreferencesFromResource(int preferencesResId) {   
            try{
                Method m = PreferenceManager.class.getDeclaredMethod("inflateFromResource", Context.class, int.class, PreferenceScreen.class);
                m.setAccessible(true);
                PreferenceScreen prefScreen = (PreferenceScreen) m.invoke(mPreferenceManager, getActivity(), preferencesResId, getPreferenceScreen());
                setPreferenceScreen(prefScreen);
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        
        /**
         * Finds a {@link Preference} based on its key.
         * 
         * [user=955119]@param[/user] key The key of the preference to retrieve.
         * [user=2056652]@return[/user] The {@link Preference} with the key, or null.
         * [user=690402]@see[/user] PreferenceGroup#findPreference(CharSequence)
         */
        public Preference findPreference(CharSequence key) {
            if (mPreferenceManager == null) {
                return null;
            }
            return mPreferenceManager.findPreference(key);
        }
        
        public interface OnPreferenceAttachedListener{
            public void onPreferenceAttached(PreferenceScreen root, int xmlId);
        }
        
    }
    How to use:

    Code:
    public class Settings extends FragmentActivity implements OnPreferenceAttachedListener, OnPreferenceChangeListener, OnPreferenceClickListener{
        private Preference filterPref;
        private ViewPager viewPager;
        
        public void onCreate(Bundle b){
            super.onCreate(b);
            setContentView(R.layout.settings);
            viewPager = (ViewPager) findViewById(R.id.pager);
            PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), this);
            viewPager.setAdapter(adapter);
            
            TitlePageIndicator titleIndicator = (TitlePageIndicator) findViewById(R.id.titles);
            titleIndicator.setViewPager(viewPager);
            titleIndicator.setFooterIndicatorStyle(IndicatorStyle.Underline);
            
            viewPager.setCurrentItem(1);
            
        }
        
        //setup your onPreferenceClickListener/onPreferenceChangeListener here
        [user=439709]@override[/user]
        public void onPreferenceAttached(PreferenceScreen root, int xmlId){
            if(root == null)
               return; //for whatever reason in very rare cases this is null
            if(xmlId == R.xml.widget_settings){        //example id    
                root.findPreference("somePreference").setOnPreferenceClickListener(this);
            }
        }
        
        //handle your preferenceChanged events here (if needed)
        [user=439709]@override[/user]
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            return true;
        }
        
        //handle your preferenceClick events here (if needed)
        [user=439709]@override[/user]
        public boolean onPreferenceClick(Preference pref){
            return true;
        }    
    
    }
    and the adapter I am using:
    Code:
    public class PagerAdapter extends FragmentPagerAdapter implements TitleProvider{
    
        PreferenceListFragment[] fragments;
        String[] titles;
        
        public PagerAdapter(FragmentManager fm, Context context) {
            super(fm);
            fragments = new PreferenceListFragment[4];
            fragments[0] = new PreferenceListFragment(R.xml.settings);
            fragments[1] = new PreferenceListFragment(R.xml.widget_settings);
            fragments[2] = new PreferenceListFragment(R.xml.s_widget_settings);
            fragments[3] = new PreferenceListFragment(R.xml.color_settings);
            
            titles = new String[4];
            titles[0] = context.getString(R.string.main_settings);
            titles[1] = context.getString(R.string.widget_settings);
            titles[2] = context.getString(R.string.s_widget_settings);
            titles[3] = context.getString(R.string.color_settings_main);
            
        }
    
        [user=439709]@override[/user]
        public Fragment getItem(int position){
            return fragments[position];
        }
    
        [user=439709]@override[/user]
        public int getCount() {
            return fragments.length;
        }
    
        [user=439709]@override[/user]
        public String getTitle(int position) {
            return titles[position];
        }
    
    
        
    }

    Lastly you will need this xml file: preference_list_content.xml (belongs into /res/layout/) (pulled from aosp)
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <!--
    /* //device/apps/common/assets/res/layout/list_content.xml
    **
    ** Copyright 2006, The Android Open Source Project
    **
    ** Licensed under the Apache License, Version 2.0 (the "License"); 
    ** you may not use this file except in compliance with the License. 
    ** You may obtain a copy of the License at 
    **
    **     http://www.apache.org/licenses/LICENSE-2.0 
    **
    ** Unless required by applicable law or agreed to in writing, software 
    ** distributed under the License is distributed on an "AS IS" BASIS, 
    ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
    ** See the License for the specific language governing permissions and 
    ** limitations under the License.
    */
    -->
    <ListView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/list"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:drawSelectorOnTop="false"
        android:scrollbarAlwaysDrawVerticalTrack="true"
        />
    2
    hm, I received logs, that the listview is null when bindPreferences() is called. =/
    2
    I added the missing layout file in the first post.
    I also added a check for nullpointer in onPreferenceAttached().
    R.array.display_filter_array correspondends to a string array I defined.

    getResources().getStringArray(int stringId) will return an array of localized strings defined in res/values/strings.xml.
    1
    Hey guys, I just wanted to share a different initialization scheme. I couldn't get the activity to survive orientation change without fc. So i came up with this...

    Extended PreferenceFragment
    Code:
    package org.teameos.settings.device;
    
    import android.os.Bundle;
    
    public class DiagPreferences extends PreferenceListFragment {
    
        public static DiagPreferences newInstance(int xml) {
            DiagPreferences f = new DiagPreferences(xml);
            Bundle b = new Bundle();
            b.putInt("xml", xml);
            f.setArguments(b);
            return f;
        }
    
        public DiagPreferences(int xmlId) {
            // TODO Auto-generated constructor stub
            super(xmlId);
        }
    
        public DiagPreferences() {
            super();
        }
    The adapter
    Code:
        public ViewPagerAdapter(Context context, FragmentManager fm) {
            super(fm);
            mContext = context;
            mFragments = new ArrayList<Pair<Fragment, String>>();
            mFragments.add(Pair.create(
                    (Fragment) UpdatesPreferences.newInstance(R.xml.updates_and_menus),
                    mContext.getString(R.string.eos_ota_menus)));
            mFragments.add(Pair.create((Fragment) DiagPreferences.newInstance(R.xml.diagnostics),
                    mContext.getString(R.string.eos_diag)));
            mFragments.add(Pair.create((Fragment) PrlPreferences.newInstance(R.xml.prl_management),
                    mContext.getString(R.string.prl_management)));
        }