15

Android Settings Screenshot

FragmentsBC Screenshot

If you look at either Android Settings screenshot or FragmentsBC screenshot, there are margin in PreferenceFragment. How can you get rid of it?

I tried making PreferenceFragment width to fill_parent, but no luck.

jclova
  • 5,466
  • 16
  • 52
  • 78
  • Similar question answered here: https://stackoverflow.com/questions/18509369/android-how-to-get-remove-margin-padding-in-preference-screen – Vivek Nov 06 '18 at 09:18

8 Answers8

34

Finally, I found the solution to this.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = super.onCreateView(inflater, container, savedInstanceState);
    if(v != null) {
        ListView lv = (ListView) v.findViewById(android.R.id.list);
        lv.setPadding(10, 10, 10, 10);
    }
    return v;
}

You can set padding by using: setPadding();

jclova
  • 5,466
  • 16
  • 52
  • 78
  • hey @jclova, I am facing same problem as urs. Here is link of it : http://stackoverflow.com/questions/18509369/android-how-to-get-remove-margin-padding-in-preference-screen/18509566?noredirect=1#18509566 Acutally I am not creating any custom layout for preference screen. Simply having checkboxpreference in layout,though it is setting any margin to left. Do you have any idea where I am going wrong please help. – Dory Aug 29 '13 at 13:19
  • 1
    Keep in mind that the padding amounts are in Px. You will probably want to convert them from dp to px before setting – Felipe Lima Mar 18 '14 at 20:21
  • Any way to override this in xml. – lostintranslation Mar 23 '14 at 02:24
  • For me "v" always returns null (super.onCreateView(...)) – Gilian Jun 13 '16 at 00:29
  • Worked flawlessly with Android 4.3, I was getting ready to forget about it. – MRodrigues Jul 21 '16 at 08:40
  • getListView().setPadding(0, 0, 0, 0); for android.support.v7.preference.PreferenceFragmentCompat. – matoni Aug 21 '17 at 20:51
10

Just an addition to this; the accepted answer did not work for me because the ListView itself does not have any padding set, it is set on the parent view (usually a LinearLayout). So instead, the following was necessary:

ListView lv = (ListView) findViewById(android.R.id.list);
ViewGroup parent = (ViewGroup)lv.getParent();
parent.setPadding(0, 0, 0, 0);
devunwired
  • 62,780
  • 12
  • 127
  • 139
  • 1
    Wouldn't it be better adding `lv.setPadding(0, 0, 0, 0);`? For me @jclova's answer does work, but I'll set both padding of `lv` and `lv.getParent()`, to deal with untested devices. Thank you. – Weekend Nov 12 '15 at 08:47
5

getListView().setPadding(left, top, right, bottom)

Make sure to call it after your view has been created (not onCreate).

Keep in mind that the int you pass in is in pixels, not dp. To convert from dp to pixels see this answer.

Community
  • 1
  • 1
theblang
  • 10,215
  • 9
  • 69
  • 120
  • 1
    This is the only answer that worked for me. I invoked this in my `PreferenceActivity`'s `onCreate()` method. – Maurizio Aug 02 '14 at 17:45
3

The answers here didn't work for me. However, this one did:

source: https://stackoverflow.com/a/53402144/9026710

In your PreferenceScreen add this line:

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

add this line in PreferenceScreen and it's elements:

app:iconSpaceReserved="false"

Example:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    app:iconSpaceReserved="false"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <CheckBoxPreference
        app:iconSpaceReserved="false"
        android:defaultValue="false"
        android:key="key1"
        android:title="title1" />
    <SwitchPreference
        app:iconSpaceReserved="false"
        android:defaultValue="false"
        android:key="@string/pref_wakeup"
        android:title="key2" />
</PreferenceScreen>
Tayyab Mazhar
  • 1,560
  • 10
  • 26
2

This is what I do in the onResume override:

    // Fix PreferenceFragment's padding...
    int paddingSize = 0;
    if (Build.VERSION.SDK_INT < 14)
    {
        paddingSize = (int) (-32 * CLS_Gfx.scale);
    }
    else
    {
        paddingSize = (int) (-16 * CLS_Gfx.scale);
    }

    final View v = getView();

    v.setPadding(paddingSize, 0, paddingSize, 0);
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 1
    This is working perfectly for me. Even android:icon="@null" was not making any changes but your solution works. Thank u so much for great solution. I think this should be an accepted answer. – Smeet Jul 12 '17 at 05:10
  • @Smeet I'm glad it helped you! – Phantômaxx Jul 12 '17 at 07:03
2

There's more elegant solution, at least for PreferenceFragmentCompat, to define the padding in a theme. Assume we have our activity set a android:theme="@style/PreferenceTheme"

   <style name="PreferenceTheme" parent="@style/Theme.AppCompat">
       <item name="preferenceTheme">@style/MyPreferenceThemeOverlay</item>
   </style>

   <style name="MyPreferenceThemeOverlay" parent="PreferenceThemeOverlay">
        <item name="preferenceFragmentListStyle">@style/MyPreferenceFragmentListStyle</item>
   </style>

   <style name="MyPreferenceFragmentListStyle" parent="@style/PreferenceFragmentList">
       <item name="android:paddingLeft">0dp</item>
       <item name="android:paddingRight">0dp</item>
   </style>
ernazm
  • 9,208
  • 4
  • 44
  • 51
  • this doesnt work for me. Still see paddings when using PreferenceFragmentCompat. Also, I dont get any warnings or errors, but doesnt even exist (autocomplete doesnt work) – qkx Mar 12 '19 at 10:25
  • @qkx the fact autocomplete does not work does not mean the attribute doesn't exist; `preferenceTheme` attribute does exist (https://stackoverflow.com/a/32073430/639183). So most likely you have some issues with setup (perhaps with support library or its version?) – ernazm Mar 14 '19 at 16:21
  • I am using latest version (28.0.0) and I literally copy pasted your code and applied this style. It really doesnt work at all :) And it cant - because problem is not padding, problem is that compat preference always reserve spacing for icon. Thats the root of the problem, not padding. So you have to set preserveIconSpacing to false - but in code, programatically (in xml it doest work). Thats a real solution :) I also found it on some other questions and I can confirm it works perfectly. – qkx Mar 14 '19 at 16:48
  • Ok, it can't work, still it works for me and at least 2 upvoted users ;) You copy-pasted the solution, but there's no setup to copy-paste, so my comment is still applicable: we may have different setup, that's why it works for me but doesn't work for you. – ernazm Mar 14 '19 at 17:06
0

Another option is to set your own adapter. That way you have more flexibility and it's more robust against changes.

See that other answer about custom header adapter

Community
  • 1
  • 1
lujop
  • 13,504
  • 9
  • 62
  • 95
-1

When you do the preference activity with a fragment, here is the solution that I used:

public class T2DPreferenceActivity extends PreferenceActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getFragmentManager().beginTransaction().replace(android.R.id.content,
            new T2DPreferenceFragment()).commit();
}

public static class T2DPreferenceFragment extends PreferenceFragment {
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.server_screen_preference);
    }

    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ListView lv = (ListView)view.findViewById(android.R.id.list);
        ViewGroup parent = (ViewGroup)lv.getParent();
        parent.setPadding(0, 100, 0, 0);
    }
}

}

Droid Chris
  • 3,455
  • 28
  • 31