How to remove left indent in the PreferenceScreen?
Asked Answered
O

2

22

Since PreferenceFragment is deprecated, I use PreferenceFragmentCompat.

After replacing the fragment, I get left indentation from the content.:
After replacing the fragment, I get left indentation from the content.

Indentation most likely appears because of the icons, but I do not use them (default is no icon).

I've tried to set the icon attribute to android:icon="@null" or android:color/transparent, but it did not help.

The function of the replacement fragment:

private fun replaceFragment(fragment: Fragment) {
    supportFragmentManager.beginTransaction()
            .replace(R.id.fragment_layout, fragment)
            .commit()
}

content_main.xml

<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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/app_bar_main">

    <FrameLayout
        android:id="@+id/fragment_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

My settings fragment:

class SettingsFragment : PreferenceFragmentCompat() {

companion object {

    /**
     * A preference value change listener that updates the preference's summary
     * to reflect its new value.
     */
    private val sBindPreferenceSummaryToValueListener = Preference.OnPreferenceChangeListener { preference, value ->

        val stringValue = value.toString()

        if (preference is ListPreference) {
            // For list preferences, look up the correct display value in
            // the preference's 'entries' list.
            val index = preference.findIndexOfValue(stringValue)

            // Set the summary to reflect the new value.
            preference.setSummary(
                    if (index >= 0)
                        preference.entries[index]
                    else
                        null)

        } else {
            // For all other preferences, set the summary to the value's
            // simple string representation.
            preference.summary = stringValue
        }
        true
    }
}

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

    /**
     * Bind preference summary to value for lists and sorting list preferences
     */

    bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_key_name)))
    bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_key_language)))

}

private fun bindPreferenceSummaryToValue(preference: Preference) {
    // Set the listener to watch for value changes.
    preference.onPreferenceChangeListener = sBindPreferenceSummaryToValueListener

    // Trigger the listener immediately with the preference's
    // current value.
    sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
            PreferenceManager
                    .getDefaultSharedPreferences(preference.context)
                    .getString(preference.key, ""))
}

preferences.xml

<android.support.v7.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<android.support.v7.preference.PreferenceCategory android:title="@string/pref_header_general">

    <android.support.v7.preference.EditTextPreference
        android:defaultValue="@string/nav_header_subtitle"
        android:key="@string/pref_key_name"
        android:summary="@string/nav_header_subtitle"
        android:title="@string/pref_title_your_name" />

    <android.support.v7.preference.SwitchPreferenceCompat
        android:defaultValue="true"
        android:key="@string/pref_key_sound"
        android:summary="@string/pref_summary_sound"
        android:title="@string/pref_title_sound" />

    <android.support.v7.preference.ListPreference
        android:dialogTitle="Select language"
        android:entries="@array/settings_list_preference_languages_titles"
        android:entryValues="@array/settings_list_preference_languages_values"
        android:key="@string/pref_key_language"
        android:summary="Click to select language"
        android:title="Language" />

</android.support.v7.preference.PreferenceCategory>

<android.support.v7.preference.PreferenceCategory android:title="@string/pref_header_notifications">

    <android.support.v7.preference.SwitchPreferenceCompat
        android:defaultValue="true"
        android:key="@string/notifications_new_message"
        android:title="@string/pref_title_new_notification_sound" />


    <android.support.v7.preference.SwitchPreferenceCompat
        android:defaultValue="true"
        android:key="@string/pref_key_notifications_vibrate"
        android:summary="@string/pref_summary_vibrate"
        android:title="@string/pref_title_vibrate" />

</android.support.v7.preference.PreferenceCategory>

<!--Account Settings-->
<android.support.v7.preference.PreferenceCategory android:title="@string/pref_header_account">

    <android.support.v7.preference.Preference
        android:key="@string/pref_key_logout"
        android:title="@string/pref_title_logout" />

    <android.support.v7.preference.Preference
        android:key="@string/pref_key_delete_account"
        android:title="@string/pref_title_delete_account" />
</android.support.v7.preference.PreferenceCategory>

<android.support.v7.preference.PreferenceCategory android:title="@string/pref_header_about">

    <android.support.v7.preference.Preference
        android:summary="@string/app_version"
        android:title="@string/pref_title_version" />

</android.support.v7.preference.PreferenceCategory>

styles.xml

<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">?themePrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="themePrimary">@color/colorPrimary</item>

    <!-- PreferenceFragmentCompat -->
    <item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item>
</style>

Version SDK

ext.support_version = '28.0.0-rc01'
Obsequious answered 23/8, 2018 at 12:37 Comment(5)
I am using com.android.support:preference-v7:28.0.0-rc02 and this issue has not been fixed yet. I have tried several workarounds and I recommend this one(https://mcmap.net/q/176880/-android-how-to-remove-margin-padding-in-preference-screen) at this point.Dissolute
Looking for a solution as well. For now I've sticking with version 28.0.0-alpha1 which doesn't seem to have the issue...Flood
Confirming the issue still persists in com.android.support:preference-v7:28.0.0 (stable). The culprit must have been introduced after 27.1.1. I had no issues in 27.1.1Atavistic
Recommend referring to this question #51519258Flood
Does this answer your question? Android: How to remove margin/padding in Preference ScreenRunnels
Y
27

If you migrate your code to androidx you can easily remove the extra paddings by adding the following to each preference:

app:iconSpaceReserved="false"

You'll also need to add the following namespace to the top of your xml file:

xmlns:app="http://schemas.android.com/apk/res-auto"

Yawata answered 2/1, 2019 at 15:41 Comment(6)
what about the PreferenceCategory thenLangtry
This solution works with "com.android.support:preference-v7:28.0.0"Carpophore
Exactly what I was looking for. +1Guillemot
Can we apply to all PreferenceCategory?Propagandism
Funny how the attribute documentation swears it's "false" by default. And yes, you can apply it to PreferenceCategory too.Fortnightly
This is only supported on API 26+ so only for Android 8.0 and higher.Costanzo
H
6

Put values-preference.xml file in res/values-sw360dp:

<?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>
</resources>

By digging into library code you can see it has a resource file in values-sw360dp-v13, which overrides your base modifications, such as app:iconSpaceReserved="false".

Helmsman answered 18/8, 2019 at 8:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.