jclova
jclova

Reputation: 5576

Android: How to maximize PreferenceFragment width (or get rid of margin)?

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.

Upvotes: 15

Views: 15266

Answers (8)

Tayyab Mazhar
Tayyab Mazhar

Reputation: 1702

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>

Upvotes: 3

Droid Chris
Droid Chris

Reputation: 3783

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);
    }
}

}

Upvotes: -1

ernazm
ernazm

Reputation: 9258

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>

Upvotes: 2

lujop
lujop

Reputation: 13873

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

Upvotes: 0

theblang
theblang

Reputation: 10425

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.

Upvotes: 4

Phant&#244;maxx
Phant&#244;maxx

Reputation: 38098

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);

Upvotes: 2

devunwired
devunwired

Reputation: 63293

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);

Upvotes: 10

jclova
jclova

Reputation: 5576

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();

Upvotes: 34

Related Questions