

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.
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();
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);
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.
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>
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With