PreferenceFragmentCompat has iconSpaceReserved true by default
Asked Answered
S

1

6

I've migrated to androidx.* libraries and one of them is the new preferences library: androidx.preference:preference:1.1.0-alpha01 - the latest version of it.

As said in release notes iconSpaceReserved attribute not working correctly with PreferenceCategories is fixed.

But looks like it's set to true by default.

I've built demo project to test it.

PreferencesFragment

import androidx.preference.PreferenceFragmentCompat;

public class SetttingsFragment extends PreferenceFragmentCompat {
    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        addPreferencesFromResource(R.xml.preferences);
    }
}

preferences.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <PreferenceCategory android:title="Category 1">
        <Preference
            android:key="pref1"
            android:title="Preference 1" />
        <Preference
            android:key="pref2"
            android:title="Preference 2" />
    </PreferenceCategory>
    <PreferenceCategory
        android:title="Category 2"
        app:iconSpaceReserved="false">
        <Preference
            android:key="pref3"
            android:title="Preference 3" />
        <Preference
            android:key="pref4"
            android:title="Preference 4"
            app:iconSpaceReserved="false" />
    </PreferenceCategory>
    ...
</PreferenceScreen>

Preferences theme is set as needed.

styles.xml

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

Screenshot

PreferencesFragment

When app:iconSpaceReserved=false is set manually (as you can see on PreferenceCategory 2 and Preference 4) it works fine, but otherwise a space for icon is reserved. Also docs says that it's false by default:

By default, preference icon view visibility will be set to GONE when there is no icon provided, so the default value of this attribute is false.

Is it a new bug in this library or a new feature to leave space for icon if it's not set? Or am I doing something wrong?

I know about workarounds:


Edit:

After looking through the sources I found the next thing.

PreferenceThemeOverlay -> anyPreferenceStyle has set the attribute

<item name="iconSpaceReserved">@bool/config_materialPreferenceIconSpaceReserved</item>

which refers to

values/values.xml
    <bool name="config_materialPreferenceIconSpaceReserved">false</bool>

and

values-sw360dp-v13/values-sw360dp-v13.xml
    <bool name="config_materialPreferenceIconSpaceReserved">true</bool>

For some reason it's set to true here.

Saying answered 23/11, 2018 at 8:53 Comment(7)
I've not used that yet, but looking through the source, it seems PreferenceThemeOverlay.v14.Material is deprecated – android.googlesource.com/platform/frameworks/support/+/…. Try using PreferenceThemeOverlay instead. All of the styles in that theme have iconSpaceReserved set to false (actually, they're set to a bool resource value that's false).Saline
@MikeM. thanks, forgot to check this. But nothing changed, padding is still there.Saying
Well, I'm having machine issues, atm, and can't pull that to test. Have you tried to check the theme value of iconSpaceReserved at runtime, just to see if the resource value is wrong, or if it's an issue in the Preference classes?Saline
@MikeM. Thanks for the suggestion, done. When iconSpaceReserved is not set manually it returns true (for example, Preference 1 on my screenshot), if set to false manually returns false (Preference 4) as expected. Tested it on my physical device with API 26 and on emulators with different APIs (including 28). Also Layout Inspector shows some padding.Saying
Also see edit at the end of my question. iconSpaceReserved is true in values-sw360dp-v13, almost for all android devices. Looks like they forgot to change it to false in the library. I will check issue tracker and post new issue if I find nothing related.Saying
I'm not seeing a values-sw360dp-v13 bucket in the androidx repo. android.googlesource.com/platform/frameworks/support/+/… Do you have the source downloaded? I'm sure that's probably more up-to-date.Saline
Ah, it is true, though, in values-sw360dp, so same diff.Saline
S
6

I've posted this on the Issue Tracker and got the following answer:

This is intended and part of the Material spec for Settings. See Material Design guide under 'Alignment'.

Saying answered 23/11, 2018 at 13:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.