androidandroid-preferencesandroidxpreferencefragment

PreferenceFragmentCompat has iconSpaceReserved true by default


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.


Solution

  • 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'.