Search code examples
androidandroid-preferences

How to stop my preference screen giving a different font size to a custom made preference


Currently my settings activity gives me two font sizes. One text size for all the predefined preferences and one for a custom preference. The preferences xml does look like this. The custom preference extended from Dialog Preference.

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <CheckBoxPreference
    android:key="changemode"
    android:title="@string/change_mode"
    android:defaultValue="true"
    android:summary="@string/change_mode_summary"
    />

<org.myapp.settings.NumberPickerPreference
    android:id="@+id/pref_num_picker_pref"
    android:key="@string/number_of_uploads"
    android:title="@string/number_of_uploads"
    />


</PreferenceScreen>

What is the reason for this? Since none of the preferences has a predefined text size, is there a way to fix this without adding a textsize attribute?


Solution

  • If you inflate or create your own (Text)Views, you should make sure they have the same styling as default implementations.

    From this source layout it seems like they use textAppearanceMedium for titles and textAppearanceSmall for summaries.

    So, in your custom layout for a title you should use

    <!-- for title -->
    android:textAppearance="?android:attr/textAppearanceMedium"
    <!-- for summary or description -->
    android:textAppearance="?android:attr/textAppearanceSmall"
    

    If you create Views only pramatically (which you probably shouldn't in this case), you can use

    // for title
    titleView.setTextAppearance(android.R.style.TextAppearance_DeviceDefault_Medium);
    // for summary
    titleView.setTextAppearance(android.R.style.TextAppearance_DeviceDefault_Small);