How to customize the layout (maxLines) of a CheckBoxPreference?

476 views Asked by At

I am using the following settings.xml to inflate my SettingsFragment extending android.support.v7.preference.PreferenceFragmentCompat:

<android.support.v7.preference.PreferenceScreen 
    xmlns:android="http://schemas.android.com/apk/res/android">

    <android.support.v7.preference.PreferenceCategory 
        android:title="@string/settings_category_title_general">

        <android.support.v7.preference.CheckBoxPreference
            android:defaultValue="@string/settings_default_value_lorem"
            android:key="@string/settings_key_lorem"
            android:summary="@string/settings_summary_lorem"
            android:title="@string/settings_title_lorem" />

    </android.support.v7.preference.PreferenceCategory>

</android.support.v7.preference.PreferenceScreen>

The summary text is cut off after four lines which is defined in com.android.support:preference-v7:24.2.1 in res/layout/preferences.xml:

<TextView android:id="@android:id/summary"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@android:id/title"
    android:layout_alignStart="@android:id/title"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="?android:attr/textColorSecondary"
    android:maxLines="4" />

The layout inspector shows the summary id as well:

Preference layout

To overwrite the setting I defined the following styles:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="preferenceTheme">@style/AppPreferenceTheme</item>
</style>

<style name="AppPreferenceTheme" parent="@style/PreferenceThemeOverlay">
    <item name="preferenceStyle">@style/AppPreference</item>
</style>

<style name="AppPreference" parent="@style/Preference">
    <item name="android:layout">@layout/settings_item</item>
</style>

and created a copy of the original layout file preferences.xml named settings_item.xml which overwrites android:maxLines="4".

However, the style is not applied. I added a background color to the summary view to check this.


Related

1

There are 1 answers

0
cketti On BEST ANSWER

From the JavaDoc of the Preference constructor:

Perform inflation from XML and apply a class-specific base style. This constructor of Preference allows subclasses to use their own base style when they are inflating. For example, a CheckBoxPreference constructor calls this version of the super class constructor and supplies android.R.attr.checkBoxPreferenceStyle for defStyleAttr. This allows the theme's checkbox preference style to modify all of the base preference attributes as well as the CheckBoxPreference class's attributes.

So change your styles to:

<style name="AppPreferenceTheme" parent="@style/PreferenceThemeOverlay">
    <item name="checkBoxPreferenceStyle">@style/AppPreference</item>
</style>

<style name="AppPreference" parent="@style/Preference.CheckBoxPreference">
    <item name="layout">@layout/settings_item</item>
</style>