How to get Android PreferenceFragment title/header to show in right pane?

10,625

What you showed here seems to be fine, but that is not everything. Look at Device Admin example with the code here I use the same technique in my app and it works great.

Share:
10,625
gyoda
Author by

gyoda

Android developer and general tech enthusiast.

Updated on August 16, 2022

Comments

  • gyoda
    gyoda almost 2 years

    I am creating a PreferenceActivity for a tablet app (Android 3.0+) using PreferenceFragments. The goal is to have a two-pane layout displaying the preferences just like the system preferences are displayed.

    The issue I'm having is that I cannot for the life of me seem to get the title of the currently selected PreferenceFragment to display above the ListView of the PreferenceFragment in the right pane. Here is an example of what I'm trying to do (trying to display the content in red box.)

    enter image description here Here is my preference_headers.xml file:

    <?xml version="1.0" encoding="utf-8"?>
    <preference-headers xmlns:android="http://schemas.android.com/apk/res/android">
        <header android:fragment="com.mycompany.mobile.preferences.MyPreferencesActivityHC$GeneralSettingsPreferenceFragment"
                android:title="General Settings">
        </header>
        <header android:fragment="com.mycompany.mobile.preferences.MyPreferencesActivityHC$PhotoOptionsPreferenceFragment"
                android:title="Photo Settings"
                android:summary="Testing photo settings">
        </header>
    </preference-headers>
    

    and here is the code for MyPreferencesActivity.java

    package com.mycompany.mobile.preferences;
    
    import java.util.List;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.preference.Preference;
    import android.preference.PreferenceActivity;
    import android.preference.PreferenceFragment;
    
    import com.mycompany.mobile.R;
    
    public class MyPreferencesActivityHC extends PreferenceActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setTitle(R.string.taskCaptionSettings);
        }
    
        @Override
        public void onBuildHeaders(List<Header> target) {
            loadHeadersFromResource(R.xml.preference_headers, target);
        }
    
        public static class GeneralSettingsPreferenceFragment extends PreferenceFragment {
    
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                addPreferencesFromResource(R.xml.general_settings_preferences);
            }
        }
    
        public static class PhotoOptionsPreferenceFragment extends PreferenceFragment {
    
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                addPreferencesFromResource(R.xml.photo_options_preferences);
            }
        }
    }