how to add margin/padding to preference screen

JY2k picture JY2k · Jul 21, 2014 · Viewed 8.7k times · Source

This is my code:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    <PreferenceCategory android:title="title" >
        <Preference
            android:layout="@layout/custom_preference_layout"
            android:title="@string/settings_rateUsOnGooglePlay" />

    </PreferenceCategory>
</PreferenceScreen>

It is not possible to simply specify a margin/padding in the PreferenceScreen element. How do i add Margin/Padding to the preference screen?

Answer

Bryan W picture Bryan W · Nov 3, 2019

Nov. 2019 Update: There is a solution, contrary to the first answer from years ago.

Option 1: If you are inflating your PreferenceScreen as a fragment into a FrameLayout, you can assign a dp value to layout_marginTop or layout_marginBottom like so.

        <FrameLayout
        android:id="@+id/fragment_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="64dp" />

Option 2: Otherwise, you can access the RecyclerView object, (this contains the PreferenceScreen's items) in the fragment's OnViewCreated like this:

    // PreferenceFragment class
    @Override
    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        final RecyclerView rv = getListView(); // This holds the PreferenceScreen's items
        rv.setPadding(0, 0, 0, 56); // (left, top, right, bottom)
    }