sixcookies
sixcookies

Reputation: 387

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>

Upvotes: 1

Views: 2986

Answers (4)

CaptainCrunch
CaptainCrunch

Reputation: 1403

If you're trying to change the icon size in a preference screen use this method I found out after playing around a bit (inspired by @S-R answer)

Let's say you have 48dp icons which are too big for the Preferences, scale it to 36dp:

<style name="AppPreferenceTheme" parent="@style/PreferenceThemeOverlay.v14.Material">
        <item name="android:layout_width">36dp</item>
        <item name="android:layout_height">36dp</item>
</style>

<!-- And set preference Theme in your Main Theme-->
<style name="YourTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="preferenceTheme">@style/AppPreferenceTheme</item>
</style>

For some reason, this only effects the ImageView and not the text or checkboxes or what ever in the screen. Please test before using ...

Upvotes: 0

S.R
S.R

Reputation: 2829

You can also change it in your style

 <!-- Custom Preference Theme inherits from the v14 support library Material Theme -->
<style name="AppPreferenceTheme" parent="@style/PreferenceThemeOverlay.v14.Material">
    <item name="android:textSize">14sp</item>
</style>

<!-- And set preference Theme in your Main Theme-->
<style name="YourTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="preferenceTheme">@style/AppPreferenceTheme</item>
</style>

Upvotes: 2

Pooja Shukla
Pooja Shukla

Reputation: 56

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!

Upvotes: 0

DEVSHK
DEVSHK

Reputation: 863

try this one

    <PreferenceCategory
    android:title="Category"
    android:textSize="20px">

    <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"/>

</PreferenceCategory>

Upvotes: 0

Related Questions