SwitchPreferenceCompat looking different when added to XML vs created programmatically [duplicate]
Asked Answered
P

1

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).

Pix answered 22/3, 2017 at 15:32 Comment(9)
Do you actually wrap the SwitchPreferenceCompat in the PreferenceCategory just like the one above?Pled
PreferenceScreen also has its compat brother, declare it in xml instead of original PreferenceScreen.Mcbee
When I use Compat 25.2.0 they look exactly the same.Pled
@GiovanniTerlingen Sorry, forgot to push my changes. If you checkout branch_two again, you will see the difference.Pix
@Mcbee There is no PreferenceScreenCompat.Pix
Yes, but there is android.support.v7.preference.PreferenceScreenMcbee
@Mcbee 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).Pix
I switched to CheckBoxPreference - it doesn't have this problem.Sibling
This is a duplicate of the same problem of using the wrong context to create the new Preference. See this for the solution.Sessions
Q
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());
Quicksilver answered 30/4, 2018 at 23:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.