Search code examples
androidxmlandroid-preferences

How to change the font size of a preference?


I don't know how to change the font size of a few preferences. I have tried this post but it didn't work. After doing it, my icon did not show up. This is what it looks like now but I think that the title of the Preference is too big. Here is what my screen currently looks like:

Image of the screen

Here is the code for my pref_headers.xml xml file:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <Preference
        android:key="@string/key_pref_general"
        android:title="@string/label_pref_general"
        android:icon="@drawable/ic_person"/>

        <Preference
            android:key="@string/key_pref_categories"
            android:title="@string/label_pref_categories"
            android:icon="@drawable/ic_list"/>

        <Preference
            android:key="@string/key_pref_productivity"
            android:title="@string/label_pref_productivity"
            android:icon="@drawable/ic_chart"/>

        <Preference
            android:key="@string/key_pref_delete"
            android:title="@string/label_pref_delete"/>


</PreferenceScreen>

Solution

  • I have one solution for you.you can change font size for specific preference by managing layout file.

    Here is your edited pref_headers.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <Preference
        android:key="general"
        android:title="General"
        android:icon="@drawable/ic_person_black_24dp"
        android:layout="@layout/font_layout"/>
    
    <Preference
        android:key="categories"
        android:title="Categories"
        android:icon="@drawable/ic_list_black_24dp"
        android:layout="@layout/font_layout"/>
    
    <Preference
        android:key="productivity"
        android:title="Productivity"
        android:icon="@drawable/ic_timeline_black_24dp"
        android:layout="@layout/font_layout"/>
    
    <Preference
        android:key="delete_this_accnt"
        android:title="Delete this Account"
        android:layout="@layout/font_layout"/>
    </PreferenceScreen>
    

    Here is the layout(font_layout.xml) file from which you can change font size or any attribute which you want to change like color etc.:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="8dp"
    android:gravity="center"
    android:orientation="horizontal">
    <ImageView
        android:id="@android:id/icon"
        android:layout_width="40dp"
        android:paddingStart="4dp"
        android:paddingEnd="4dp"
        android:layout_height="40dp" />
    
    <TextView
        android:id="@android:id/title"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:textColor="#201f1f"
        android:layout_height="wrap_content"
        android:textSize="18sp" />
    </LinearLayout>
    

    Hope this helps!