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
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:
- PreferenceFragmentCompat has padding on PreferenceCategory that I can't get rid of
- How to get remove margin/padding in Preference Screen
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.
PreferenceThemeOverlay.v14.Material
is deprecated – android.googlesource.com/platform/frameworks/support/+/…. Try usingPreferenceThemeOverlay
instead. All of the styles in that theme haveiconSpaceReserved
set tofalse
(actually, they're set to abool
resource value that'sfalse
). – SalineiconSpaceReserved
at runtime, just to see if the resource value is wrong, or if it's an issue in thePreference
classes? – SalineiconSpaceReserved
is not set manually it returnstrue
(for example, Preference 1 on my screenshot), if set tofalse
manually returnsfalse
(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. – SayingiconSpaceReserved
istrue
invalues-sw360dp-v13
, almost for all android devices. Looks like they forgot to change it tofalse
in the library. I will check issue tracker and post new issue if I find nothing related. – Sayingvalues-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. – Salinetrue
, though, invalues-sw360dp
, so same diff. – Saline