Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unwanted blank space in PreferenceFragmentCompat

In my Android app i've defined a very basic PreferenceScreen:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <ListPreference
        android:entries="@array/test_entries"
        android:entryValues="@array/test_values"
        android:key="key_test1"
        android:title="Test title 1" />

    <CheckBoxPreference
        android:key="key_test2"
        android:summary="Test description 1"
        android:title="Test title 2" />
</PreferenceScreen>

And implemented the corresponding PreferenceFragmentCompat class:

class SettingsFragment : PreferenceFragmentCompat() {

    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        setPreferencesFromResource(R.xml.settings, rootKey)
    }
}

The dependency i'm using:

implementation "com.android.support:preference-v7:28.0.0"

And the code for the layout that hosts the Fragment:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".ui.activity.MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/mainToolbar"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize" />

    <fragment
        android:id="@+id/navHostFragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/navigation" />
</LinearLayout>

Everything is working fine, except there's a huge blank space on the left side of each preference:

enter image description here

What is the cause of this problem and how can i prevent it?

EDIT:

It seems the problem is the space for the icons are reserved, even if we use no icons for the preference entries.

Setting the icon attribute of the preference to @null has no effect.

Setting the iconSpaceReserved attribute to false solves the problem for the Preference items, but it has no effect on PreferenceCategory items.

like image 626
justanoob Avatar asked Oct 03 '18 07:10

justanoob


1 Answers

If using AndroidX, add the following attribute to your Preferences (and Preference Categories if needed) in XML:

<Preference
    ...
    app:iconSpaceReserved="false"
    .../>
like image 60
Maksim Ivanov Avatar answered Nov 09 '22 06:11

Maksim Ivanov