Search code examples
androidandroid-preferences

Setting title and summary automatically for custom preference


I made a custom dialog preference with a custom layout in the settings activity. Here the layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:paddingEnd="?android:attr/scrollbarSize"
    android:background="?android:attr/selectableItemBackground"
    >
    <ImageView
        android:id="@android:id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="0"
        />
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dip"
        android:layout_marginEnd="6dip"
        android:layout_marginTop="6dip"
        android:layout_marginBottom="6dip"
        android:layout_weight="1"
        >
        <TextView
            android:id="@android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            />
        <TextView
            android:id="@android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@android:id/title"
            android:layout_alignStart="@android:id/title"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="?android:attr/textColorSecondary"
            android:maxLines="4"
            />
    </RelativeLayout>

    <!-- ... -->
</LinearLayout>

Everything shows up but the title and summary that don't show any value even if they have a @android:id. This seems to work for some people but not for me.

I took my code from here: layout, class and xml preference. If you look closely, nothing is done to link title and summary views to the xml preference file values, but they are. How? Here I'm talking about android:title and android:summary values.

I won't add my custom preference code, but the problem shouldn't be there. I have the 4 constructors and I use setLayoutResource correctly.


Solution

  • The problem was the LinearLayout having no orientation. If I add android:orientation="vertical" everything appears correctly.