Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add margin/padding to preference screen

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?

like image 948
JY2k Avatar asked Jul 20 '14 22:07

JY2k


2 Answers

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)
    }
like image 79
Bryan W Avatar answered Sep 20 '22 10:09

Bryan W


No, you cannot simply specify a padding/margin. It seems you need to either specify a custom layout or android:icon="@null" like in

Android: How to adjust Margin/Padding in Preference?

Or you can try setting margin/padding programmatically like in

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

like image 26
AmmarCSE Avatar answered Sep 19 '22 10:09

AmmarCSE