7

In a PreferenceFragment, I have a SwitchPreferenceCompat added via XML:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory android:title="cat 1">

        <SwitchPreferenceCompat
            android:key="pref_1"
            android:defaultValue="false"
            android:title="from xml"/>

    </PreferenceCategory>

    <PreferenceCategory
        android:key="pref_cat_1"
        android:title="cat 2"/>

</PreferenceScreen>

and one added programmatically:

PreferenceGroup preferenceGroup = (PreferenceGroup) findPreference("pref_cat_1");

SwitchPreferenceCompat switchPreference = new SwitchPreferenceCompat(getActivity());
switchPreference.setWidgetLayoutResource(android.support.v7.preference.R.layout.preference_widget_switch_compat);
switchPreference.setTitle("programmatically");
switchPreference.setChecked(true);
switchPreference.setDefaultValue(true);

switchPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
    @Override
    public boolean onPreferenceChange(final Preference preference, final Object newValue) {

        Toast.makeText(getActivity(), newValue.toString(), Toast.LENGTH_SHORT).show();

        return true;
    }
});

preferenceGroup.addPreference(switchPreference);

On the screen they look different (fontsize):

enter image description here

I tried omitting the line

switchPreference.setWidgetLayoutResource(android.support.v7.preference.R.layout.preference_widget_switch_compat);

but then the Switch button becomes invisible.

How can I make them look the same?

Test project can be found on Github (branch_two).

fweigl
  • 21,278
  • 20
  • 114
  • 205
  • Do you actually wrap the `SwitchPreferenceCompat` in the `PreferenceCategory` just like the one above? – gi097 Mar 22 '17 at 15:37
  • `PreferenceScreen` also has its compat brother, declare it in `xml` instead of original `PreferenceScreen`. – azizbekian Mar 22 '17 at 15:40
  • When I use `Compat 25.2.0` they look exactly the same. – gi097 Mar 22 '17 at 15:49
  • @GiovanniTerlingen Sorry, forgot to push my changes. If you checkout branch_two again, you will see the difference. – fweigl Mar 22 '17 at 17:15
  • @azizbekian There is no PreferenceScreenCompat. – fweigl Mar 22 '17 at 17:15
  • Yes, but there is `android.support.v7.preference.PreferenceScreen` – azizbekian Mar 22 '17 at 17:17
  • @azizbekian I see, sorry, yeah. But using that doesn't seem to solve my font size problem. Please mind, I updated the test project, actually using SwitchPreferenceCompat (had used SwitchPreference before). – fweigl Mar 22 '17 at 17:23
  • I switched to CheckBoxPreference - it doesn't have this problem. – Viktor Yakunin Mar 24 '17 at 08:52
  • 1
    This is a duplicate of the same problem of using the wrong context to create the new Preference. See [this](http://stackoverflow.com/a/32457928/2644098) for the solution. – Gergely Kőrössy Apr 21 '17 at 13:30

1 Answers1

3

When you inflate preferences from XML they use the preferenceTheme you specified in your app theme.

When you created the preference programmatically you used plain activity theme.

If you use the correctly themed Context it will work as expected:

new SwitchPreferenceCompat(getPreferenceManager().getContext());
Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124