7

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.

justanoob
  • 1,797
  • 11
  • 27
  • How did you add the `Toolbar`? Did you added in your layout? Would you paste the layout too? Perhaps added start margin ? – ʍѳђઽ૯ท Oct 03 '18 at 07:21
  • does this happen even if you remove the list pref? Try starting with something really simple like just the checkbox and one word for the title and one word for the description. – Itamar Kerbel Oct 03 '18 at 07:23
  • @Mohsen i've added the code for the layout – justanoob Oct 03 '18 at 07:26
  • @justanoob Remove this: `androidx.navigation.fragment.NavHostFragment` and see if it helps. Also, you didn't still migrated to AndroidX but maybe this causes the issue. – ʍѳђઽ૯ท Oct 03 '18 at 07:27
  • @Mohsen why would i remove that? That's required by the [NavComponent](https://developer.android.com/topic/libraries/architecture/navigation/). If i remove it, it breaks the whole navigation of the app. – justanoob Oct 03 '18 at 07:29
  • 2
    Possible duplicate of [Android: How to get remove margin/padding in Preference Screen](https://stackoverflow.com/questions/18509369/android-how-to-get-remove-margin-padding-in-preference-screen) – Tejas Pandya Oct 03 '18 at 07:29
  • This is the default as of Android Oreo and has been baked into the Support library. – Edric Oct 03 '18 at 07:31
  • @justanoob My point is, why would you use `NavHostFragment` for settings? Also, `androidx.navigation.fragment.NavHostFragment` it seems like it is introduced in AndroidX dependencies but you're still using Support Library v28 that's why it is weird to me. – ʍѳђઽ૯ท Oct 03 '18 at 07:37
  • have a look at the post Tejas Pandya pointed too. I'm sure one of the answers will help you. – Itamar Kerbel Oct 03 '18 at 14:44

1 Answers1

6

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

<Preference
    ...
    app:iconSpaceReserved="false"
    .../>
Maksim Ivanov
  • 3,991
  • 31
  • 25