Question

I have made a custom preference (i.e. a preference with a custom layout) which displayed in the preferences list of a PreferenceActivity.

The layout is created in code. The problem is that the font of the TextView created in code looks somewhat different than Android's standard preference font.

So the solution would be to apply the style attributes of android's preference to my TextView. The respective styles should be preferenceScreenStyle or preferenceStyle (I'm not sure).

My problem is I can't figure out how to read out android's standard style attributes, so I could set them in code.

Was it helpful?

Solution

I've the same problem but i fixed it out for some mobile devices,HTC Saphire and Samsung Galaxy S, but i'have problems with my HTC Desire HD. You can see standard preference style in android_SDK_resurces/layout/preference.xml. There are the margins, text sizes,....

OTHER TIPS

Apologies for the necro but I was not able to find this answer on any SO question about preference styling. I finally found the answer: the default preference now uses the layout/preference_material. You can see it and other more specific layouts at the android source here. Copied below just in case that link breaks:

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2014 The Android Open Source Project
     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
          http://www.apache.org/licenses/LICENSE-2.0
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<!-- Layout for a Preference in a PreferenceActivity. The
     Preference is able to place a specific widget for its particular
     type in the "widget_frame" layout. -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/listPreferredItemHeightSmall"
    android:gravity="center_vertical"
    android:paddingStart="?attr/listPreferredItemPaddingStart"
    android:paddingEnd="?attr/listPreferredItemPaddingEnd"
    android:background="?attr/activatedBackgroundIndicator"
    android:clipToPadding="false">
    <LinearLayout
        android:id="@+id/icon_frame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="-4dp"
        android:minWidth="60dp"
        android:gravity="start|center_vertical"
        android:orientation="horizontal"
        android:paddingEnd="12dp"
        android:paddingTop="4dp"
        android:paddingBottom="4dp">
        <com.android.internal.widget.PreferenceImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxWidth="48dp"
            android:maxHeight="48dp" />
    </LinearLayout>
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:paddingTop="16dp"
        android:paddingBottom="16dp">
        <TextView android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textAppearance="?attr/textAppearanceListItem"
            android:ellipsize="marquee" />
        <TextView android:id="@+id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/title"
            android:layout_alignStart="@id/title"
            android:textAppearance="?attr/textAppearanceListItemSecondary"
            android:textColor="?attr/textColorSecondary"
            android:maxLines="10"
            android:ellipsize="end" />
    </RelativeLayout>
    <!-- Preference should place its actual preference widget here. -->
    <LinearLayout android:id="@+id/widget_frame"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="end|center_vertical"
        android:paddingStart="16dp"
        android:orientation="vertical" />
</LinearLayout>

I've managed to solve this by replacing current custom preference's layout resource with layout resource used by a standard preference like EditTextPreference e.g. Here is a code example, note that TimePreference is the custom preference.

    TimePreference wake_time = (TimePreference)findPreference("wake_time");
    EditTextPreference exercise = (EditTextPreference)findPreference("exercise");
    int r = exercise.getLayoutResource();
    wake_time.setLayoutResource(r);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top