22
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory
        android:title="First Category"
        android:textSize="20px">

        <CheckBoxPreference
            android:title="Checkbox Preference"
            android:defaultValue="false"
            android:summary="This preference can be true or false"
            android:key="checkboxPref" />

    </PreferenceCategory>
</PreferenceScreen>

I need to change android:title font size of PrefereceCategory, CheckboxPreference and android:summary size. These tags don't have any android:textSize attribute. Can any one help me how to get this?

Yojimbo
  • 23,288
  • 5
  • 44
  • 48
Anil Kumar
  • 1,065
  • 1
  • 13
  • 24

6 Answers6

64

Finally I was able to solve my problem simply by passing one layout. Maybe this will be helpful to someone. This is my modified preference.xml see below code. We can apply our own properties to pref title and summary.

preference.xml:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory
        android:title="First Category"
        android:textSize="20px"
        android:layout="@layout/mylayout">             

        <CheckBoxPreference
            android:title="Checkbox Preference"
            android:defaultValue="false"
            android:summary="This preference can be true or false"
            android:key="checkboxPref"
            android:layout="@layout/mylayout"/>

    </PreferenceCategory>
</PreferenceScreen>

mylayout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView android:id="@android:id/title"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textSize="40sp"/>  

    <TextView android:id="@android:id/summary"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textSize="26sp"/>

</LinearLayout>

Please ask me if any queries....Keep Smiling. If this is useful give me one comment.

lidox
  • 1,901
  • 3
  • 21
  • 40
Anil Kumar
  • 1,065
  • 1
  • 13
  • 24
  • 2
    It was useful for me. But how can I get system fonts and margins and colors for preference list items and add them to mylayout.xml? – degratnik Feb 07 '12 at 11:01
  • 6
    Many thanks for the tips. I have one comment to improve your answer above. You should specify textSizes as "sp", or "dp", definetely not as "px". Using "dp" will make the font have the same physical size on all devices, "sp" is roughly like "dp", but the size will follow the device settings for global font sizes (e.g. large/medium/small size in Settings-Screen on Android 4). My opinion is that "sp" is the most appropriate here. – Dig Deeper Sep 15 '12 at 11:51
  • How can I change the default blue drawables for the checkbox preference. – Dory Sep 17 '13 at 06:36
  • 1
    When I have used above xml for preference, in below id @+android:id/summary @+android:id/title I am getting error in xml? can you help me on this? – Ankit Thakkar Apr 01 '15 at 05:36
  • @AnkitThakkar: probably best if you create a new question, showing exactly what you did, and what the error is. – ToolmakerSteve Jun 17 '15 at 22:02
  • 1
    Shouldn't the +'s before the ids be removed? We aren't creating new ids here, just using existing ones from android namespace, are we? – Poly Bug Feb 22 '16 at 07:02
  • Thanks, it works for me. I suggested that the unit of textsize should use sp not px. – Pogo Lin Aug 07 '16 at 14:33
  • For some reason, the font sizes are not changing. Any idea why this might be? – Samuel Agbede Nov 08 '17 at 11:57
  • Let's keep smiling! – Gary Chen Sep 18 '20 at 18:20
  • android:textSize in PreferenceCategory not make any change for me – UmAnusorn Dec 22 '20 at 11:06
13

For me, the layout posted by praneetloke made the widget disappear from preferences that have an associated widget, e.g. a checkbox. There is a LinearLayout with id/widget_frame that is needed for the widget. The following worked for me, on Android 2.3 through 4.2.2. The only change that I needed to make was set android:visibility="gone" on the layout that contains the preference icon, which was not available in Gingerbread.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:paddingLeft="8dip"
    android:paddingRight="?android:attr/scrollbarSize" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:minWidth="0dp"
        android:orientation="horizontal"
        android:visibility="gone" >

        <ImageView
            android:id="@+android:id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:minWidth="48dp"
            android:paddingRight="8dip" />
    </LinearLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:paddingBottom="6dip"
        android:paddingRight="8dip"
        android:paddingTop="6dip" >

        <TextView
            android:id="@+android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceMedium" />

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

    <!-- Preference should place its actual preference widget here. -->

    <LinearLayout
        android:id="@+android:id/widget_frame"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:minWidth="48dp"
        android:orientation="vertical" />

</LinearLayout>

References: The default dimensions used in the layout are here GrepCode /res/values/dimens.xml. The actual preference item layout is here GrepCode /res/layout/preference_holo.xml. Note that there is also a non-holo version called preference.xml.

Yojimbo
  • 23,288
  • 5
  • 44
  • 48
4

I modified Kumar's answer to this for ICS and above. I placed this layout in values-v11 to target devices API level 11+.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView android:id="@+android:id/title"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textSize="18dp"
        android:textColor="?android:attr/textColorPrimary"
        android:paddingLeft="8dip"/>

    <TextView android:id="@+android:id/summary"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textSize="14dp"
        android:textColor="?android:attr/textColorSecondary"
        android:paddingLeft="8dip"/>

</LinearLayout>
praneetloke
  • 1,953
  • 1
  • 14
  • 15
4

You can actually take a look at the layout that is used in the specific platform in layout folder located in:

<android-sdk>\platforms\android-<level>\data\res\layout

Preferences layouts start with preference_ prefix.

Tomask
  • 2,344
  • 3
  • 27
  • 37
0

Have a look at my answer to this: Android PreferenceScreen title in two lines.

You can also change the font itself if you want. Just add this:

Typeface myTypeface = Typeface.createFromAsset(textView.getContext().getAssets(),"fonts/your_font_name.ttf");
    if (textView != null) {
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textView.getContext().getResources().getDimension(R.dimen.text_size_from_dimen));
        textView.setTypeface(myTypeface);
    }
Community
  • 1
  • 1
Codes12
  • 376
  • 6
  • 21
0

Just a minor revision on the layout code. "@+android:id/summary" is causing Cannot Resolve Symbol for me. so i remove the '+' sign and the code works

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

    <TextView android:id="@android:id/title"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textSize="40px"/>  

    <TextView android:id="@+android:id/summary"
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="26px"/>

</LinearLayout>
Setiawan
  • 1
  • 1