In my app's preference fragment, I have one custom preference (it's a slider view preference). Functionality works well, but I can't get the visual appearance of the preference to match that of the rest of the preference.
All searches point to using style="?android:textAppearanceLarge"
, however that results in title font much larger than on the rest of preferences and also not matching the colour.
Also, the paddings on the whole preference view don't seem to be correct.
I tried using style="?android:preferenceStyle
, but that did absolutely nothing. The application uses AppCompat.Light.DarkActionBar
theme. Here's what it looks like. Obviously, this isn't good - I need to match the styles of other preferences.
So, the question is how can I match the style of the preference layout to have the right paddings and the right font on the title?
UPDATE: after researching it a bit further, I got the title style to match using style="?attr:textAppearanceMedium"
. I'm still looking for ways to match the padding.
I know this post is a bit older. Maybe you already found a solution for this. But to all others (like me) who have the same problem let me show you the solution that worked for me:
My preference class has several constructors. One of them was the "2 argument constructor" which is the one used when inflating a XML layout. It usually looks like this
public MyPrefClass (Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyPrefClass(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setWidgetLayoutResource(...)
}
which means, the 3 argument constructor will be called giving "0" as the StyleAttributes. This leads to a constructor call without explicit style - even if defined in the XML.
So all I had to do was changing the third parameter to the desired style changing the 2 parameter constructor to this:
public MyPrefClass (Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.dialogPreferenceStyle);
}
That fixed the problem. The preference has the same style as the others.
--- EDIT ----
Make sure to set the layout via setWidgetLayoutResource instead of setLayoutResource since the former one uses the widget layout whereas the later one inflates a separate layout.