Search code examples
androidandroid-fragmentsandroid-themeandroid-stylespreferencefragment

Style PreferenceFragmentCompat like PreferenceFragment?


In my application, I had first created PreferenceFragment using no support libraries as such:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Display the fragment as the main content.
    FragmentManager mFragmentManager = getFragmentManager();
    FragmentTransaction mFragmentTransaction = mFragmentManager
            .beginTransaction();
    PrefsFragment mPrefsFragment = new PrefsFragment();
    mFragmentTransaction.replace(android.R.id.content, mPrefsFragment);
    mFragmentTransaction.commit();

}

public static class PrefsFragment extends PreferenceFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preference_screen);
    }
}

But then I decided to use the support libraries, as I've been using support fragments all over the app and thought this will be more consistent

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Display the fragment as the main content.
    FragmentManager mFragmentManager = getSupportFragmentManager();
    FragmentTransaction mFragmentTransaction = mFragmentManager
            .beginTransaction();
    PrefsFragment mPrefsFragment = new PrefsFragment();
    mFragmentTransaction.replace(android.R.id.content, mPrefsFragment);
    mFragmentTransaction.commit();

}

public static class PrefsFragment extends PreferenceFragmentCompat {

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        // Load the preferences from an XML resource
        setPreferencesFromResource(R.xml.preference_screen, rootKey);
    }
}

and add in the style file, as it doesn't work without me

<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>

Here's what the PreferenceFragmentCompat looks like

And PreferenceFragment

Is it possible to get the same style in the PreferenceFragmentCompat, doesn't seem like I changed much but got totally different styles.

Also, one more question? Does that affect all my fragments? I never used the non-support fragments so I don't know if I'm missing on a much better style if I ignored the support fragments.

Edit Update: The solution was change the preferenceTheme item to @style/PreferenceThemeOverlay.v14.Material instead


Solution

  • Try this:

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        ...
        <item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item>
    </style>