After long attempts to achieve the correct display of Preferences on different versions of the OS, I came to my own solution to the problem. If it seems that the decision is unsuccessful - offer your best option. Please note that no answer was found for such questions on all StackOverflow.
And so, first, override the Preference , for example:
public class PreferenceFix extends Preference { private Context context; public PreferenceFix(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); this.context = context; } public PreferenceFix(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.context = context; } public PreferenceFix(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; } public PreferenceFix(Context context) { super(context); this.context = context; } @Override public void onBindViewHolder(PreferenceViewHolder holder) { holder.itemView.setPadding((int) context.getResources().getDimension(R.dimen.preference_side_padding), 0, (int) context.getResources().getDimension(R.dimen.preference_side_padding), 0); super.onBindViewHolder(holder); } }
In the dimens.xml file dimens.xml specify the indents:
<resources> <dimen name="preference_side_padding">16dp</dimen> </resources>
Now, to use this Preference just write instead of the standard one:
<com.yourpackage.PreferenceFix android:key="about" android:title="@string/title" android:summary="@string/summary"/>
SwitchPreferenceCompat, EditTextPreference, ListPreference do by analogy. I hope this example will save someone a lot of time.
Concerning PreferenceCategory . You can do the same, or you can define a markup file for each OS version, for example:
layout-v17 / my_preference_category_material.xml
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dip" android:layout_marginBottom="0dip" android:textStyle="bold" android:textAppearance="@style/Preference_TextAppearanceMaterialBody2" android:textColor="@color/preference_fallback_accent_color" android:paddingStart="@dimen/preference_side_padding" android:paddingEnd="@dimen/preference_side_padding" android:paddingTop="16dip" />
And in the style indicate a new markup:
values-v17 / style.xml
<resources> <style name="appPreferenceTheme" parent="PreferenceThemeOverlay.v14.Material"> <item name="preferenceCategoryStyle">@style/Preference.MyCategory</item> </style> <style name="Preference.MyCategory" parent="@style/Preference.Category.Material"> <item name="android:layout">@layout/my_preference_category_material</item> <item name="android:selectable">false</item> <item name="android:shouldDisableView">false</item> </style> </resources>