I create an application, in it I need to implement a custom Preference as in the picture. I use Preference Support Lib v7 / v14, but apparently due to curved Google libraries it will not work.

The picture shows that the font size of the custom child Preference is too large and the thumb seekBar'a is clipped, incorrect paddings / margins

enter image description here

Gradle content:

compile 'com.android.support:appcompat-v7:24.1.1' compile 'com.android.support:preference-v7:24.1.1' compile 'com.android.support:preference-v14:24.1.1' 

Content manifest:

 <uses-sdk tools:overrideLibrary="android.support.v14.preference"/> 

I searched for information on this topic for a very long time, but I didn’t find anything useful. Indeed, even on this site, where there is such a question, everyone, like scalded ones, quickly pass by. What is the incredible complexity of solving such a simple task?

Thanks so much for any replies!

  • How I don't like messing with libraries - Flippy
  • Wrote the adapter itself for this and not steamed - Flippy
  • Sergey, which adapter? Do you mean you wrote your analogue Preferences? - Trancer
  • I solved the problem, but I’m not sure of the correctness of the solution, so I’ll not give you the answer. - Trancer
  • one
    Why do you think that Google made the "curves" of the library, and not your "crooked" hands to blame? If you need help with a code or design, you must provide this code or design (creation of preferences and markup). Guessing about the glitchiness of Google libraries from pictures here is not practiced at all, which is probably why your question is bypassed, and the complexity of solving your problem is that no information is provided for a solution other than an unintelligible picture and assurances that it is Google’s “buggy” - pavlofff

1 answer 1

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>