Danny Hambourg Danny Hambourg - 13 days ago 10
Android Question

Android - support.v7.PreferenceCategory fill the whole screen

I am using support preference library and every

PreferenceCategory
fill the whole screen with blank space and then show the preferences that it contains. I have tried changing
PreferenceCategory
style attribute - layout_height to wrap_content and it didn't work.

My preference styles:

<style name="PreferenceThemeOverlay">
<item name="preferenceScreenStyle">@style/Preference.PreferenceScreen</item>
<item name="preferenceFragmentStyle">@style/PreferenceFragment</item>
<item name="preferenceCategoryStyle">@style/Preference.Category</item>
<item name="preferenceStyle">@style/Preference</item>
<item name="preferenceInformationStyle">@style/Preference.Information</item>
<item name="checkBoxPreferenceStyle">@style/Preference.CheckBoxPreference</item>
<item name="switchPreferenceCompatStyle">@style/Preference.SwitchPreferenceCompat</item>
<item name="dialogPreferenceStyle">@style/Preference.DialogPreference</item>
<item name="editTextPreferenceStyle">@style/Preference.DialogPreference.EditTextPreference</item>
<item name="preferenceFragmentListStyle">@style/PreferenceFragmentList</item>
<item name="android:subtitleTextStyle">@color/dark_gray</item>
</style>


My prefernce xml:

<android.support.v7.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<android.support.v7.preference.PreferenceCategory
android:key="pref_user_settings"
android:title="@string/pref_group_user">
<android.support.v7.preference.ListPreference
android:defaultValue="Publisher"
android:entries="@array/pref_user_types"
android:entryValues="@array/pref_user_types"
android:key="pref_user_type"
android:title="@string/pref_title_user_type" />
</android.support.v7.preference.PreferenceCategory>




This is how it looks:
enter image description here
enter image description here

Answer

The way i solved it out is customing the preference category file.

Solution:

Create a new layout xml file called custom_preference_category:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/gray"
    android:paddingTop="8dp"
    android:id="@android:id/title" />

Then add this to your styles file:

<style name="Preference.Category">
    <item name="android:layout">@layout/custom_preference_category</item>
    <item name="android:shouldDisableView">false</item>
    <item name="android:selectable">false</item>
</style>

<style name="PreferenceThemeOverlay">
    <item name="preferenceScreenStyle">@style/Preference.PreferenceScreen</item>
    <item name="preferenceFragmentStyle">@style/PreferenceFragment</item>
    <item name="preferenceCategoryStyle">@style/Preference.Category</item>
    <item name="preferenceStyle">@style/Preference</item>
    <item name="preferenceInformationStyle">@style/Preference.Information</item>
    <item name="checkBoxPreferenceStyle">@style/Preference.CheckBoxPreference</item>
    <item name="switchPreferenceCompatStyle">@style/Preference.SwitchPreferenceCompat</item>
    <item name="dialogPreferenceStyle">@style/Preference.DialogPreference</item>
    <item name="editTextPreferenceStyle">@style/Preference.DialogPreference.EditTextPreference
    </item>
    <item name="preferenceFragmentListStyle">@style/PreferenceFragmentList</item>
</style>

And then use this style in your main app theme:

<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>

I did it a lot of time ago so I'm not sure if this all I've done, so give me a feedback if something is missing.