0

Hello i have the following fragment which extends PreferenceFragmentCompat:

class SettingsFragment : PreferenceFragmentCompat(){
    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        Log.d(TAG, "onCreatePreferences")
        addPreferencesFromResource(R.xml.app_preferences)
    }
...
}

The xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory
        android:title="Catogeory 1">

        <SwitchPreference
            android:key="key1"
            android:title="Switch Preference"
            android:summary="Switch Summary"
            android:defaultValue="true" />

        <EditTextPreference
            android:key="key2"
            android:title="EditText Preference"
            android:summary="EditText Summary"
            android:dialogMessage="Dialog Message"
            android:defaultValue="Default value" />
        <CheckBoxPreference
            android:key="key3"
            android:title="CheckBox Preference"
            android:summary="CheckBox Summary"
            android:defaultValue="true"/>
    </PreferenceCategory>

</PreferenceScreen>

in the preview it looks fine, but on my emulator, there is a lot of padding on the left side as seen here:

enter image description here

How do i make it so the text goes closer to the edge of the screen

Thanks

Community
  • 1
  • 1
Barcode
  • 930
  • 1
  • 13
  • 31
  • 1
    The AndroidX/Support library Preferences force a blank space to the start of the Preference. It's a reserved space for the icon. – TheWanderer Mar 13 '19 at 23:30
  • Possible duplicate of [Android: How to remove margin/padding in Preference Screen](https://stackoverflow.com/questions/18509369/android-how-to-remove-margin-padding-in-preference-screen) – TheWanderer Mar 13 '19 at 23:30
  • @TheWanderer thanks – Barcode Mar 13 '19 at 23:36

2 Answers2

1
app:iconSpaceReserved="false"

use this attribute in xml.

Mazin Ibrahim
  • 7,433
  • 2
  • 33
  • 40
sah Rishabh
  • 51
  • 1
  • 3
1

First of all, this question is a duplicate of Android: How to remove margin/padding in Preference Screen and PreferenceFragmentCompat has padding on PreferenceCategory that I can't get rid of.

As Sah Rishabh as said, you can sort of fix this by adding

app:iconSpaceReserved="false"

to every Preference tag in the xml.

However, this is not a good solution as it doesn't really work with Preference Categories (the titles are still a bit too the right) and requires adding something to every single tag.

The best solution is therefore described here or here (the same answer), as:

Create res/values-sw360dp-v13/values-preference.xml containing:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
    <bool name="config_materialPreferenceIconSpaceReserved" tools:ignore="MissingDefaultResource,PrivateResource">false</bool>
    <dimen name="preference_category_padding_start" tools:ignore="MissingDefaultResource,PrivateResource">0dp</dimen>
</resources>
NoHarmDan
  • 492
  • 5
  • 16