ListPreference and EditTextPreference displaying the current value

preference_with_value.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:paddingRight="?android:attr/scrollbarSize">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dip"
        android:layout_marginRight="6dip"
        android:layout_marginTop="6dip"
        android:layout_marginBottom="6dip"
        android:layout_weight="1">

        <LinearLayout
            android:id="@+id/preference_first_line"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical">

            <TextView android:id="@+android:id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="15dip"
                android:layout_weight="1"
                android:singleLine="true"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:ellipsize="marquee"
                android:fadingEdge="horizontal" />
    
            <TextView android:id="@+id/preference_value"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="10"
                android:singleLine="true"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:gravity="right"
                android:ellipsize="end" />

        </LinearLayout>

        <TextView android:id="@+android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/preference_first_line"
            android:layout_alignLeft="@android:id/title"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:maxLines="2" />

    </RelativeLayout>

</LinearLayout>

Code in Activity:

final ListPreference listPref = ...
listPref.setLayoutResource(R.layout.preference_with_value);
listPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        TextView textView = (TextView) findViewById(R.id.preference_value);
        int index = listPref.findIndexOfValue(newValue.toString());
        if (index != -1) {
            textView.setText(listPref.getEntries()[index]);
        }
        return true;
    }
});

We can also write ListPreferenceWithValue and EditTextPreferenceWithValue classes to automate the above code:
package com.example;

import android.content.Context;
import android.preference.EditTextPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

public class EditTextPreferenceWithValue extends EditTextPreference {

    private TextView mValueText;

    public EditTextPreferenceWithValue(Context context, AttributeSet attrs) {
        super(context, attrs);
        setLayoutResource(R.layout.preference_with_value);
    }

    public EditTextPreferenceWithValue(Context context) {
        super(context);
        setLayoutResource(R.layout.preference_with_value);
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);
        mValueText = (TextView) view.findViewById(R.id.preference_value);
        if (mValueText != null) {
            mValueText.setText(getText());
        }
    }

    @Override
    public void setText(String text) {
        super.setText(text);
        if (mValueText != null) {
            mValueText.setText(getText());
        }
    }

}


10 Comments

#
phnixwxz - April 23, 2009 at 2:59 a.m.

See screen shot at http://picasaweb.google.com/phnixwxz/ScreenShots#5327684730584667154

#
freality - July 24, 2009 at 8:41 p.m.

this is great! the way preferences should appear. A lot better than hacking the summary.

had to add "android:maxWidth" to the summary textview to ensure it didn't encroach on the title.

#
nico - September 21, 2009 at 9:19 p.m.

nice! it would be much better to choose a day > 12 to make obvious what is what. to make it mor clear: in the 2nd screenshot date format options 2 and 3 are both "01/01/2009" ;)

#
Ingo - November 10, 2009 at 4:09 p.m.

nice! so how could I use EditTextPreferenceWithValue
and ListPreferenceWithValue
in the xml file? or how do I hook these clases to the PreferenceActivity?

#
Guygeboe - January 6, 2010 at 9:26 a.m.

Hi,

Thx for the code, this is exactly what I need really.

Does this also work when the listpref's are not added dynamically but just stated in a preferences.xml? For me it doesnt anyway :(

#
Ariat - January 27, 2010 at 11:58 p.m.

Awesome information and will prove very useful.

#
Bearpaw Boots - February 19, 2010 at 3:58 p.m.

Great code. Thanks for sharing this.

#
walk shoes - February 24, 2010 at 3:15 p.m.

Does this also work when the listpref's?

#
handbags balenciaga sale - March 5, 2010 at 2:46 p.m.

balenciaga handbags
http://www.olugg.com/specials.html
<a href="http://www.olugg.com/ugg-classic-tall-c-3.html">ugg classic tall</a>
[url=http://www.olugg.com/handbags-mulberry-handbags-c-490_510.html]mulberry handbags[/url]
[hermes handbags->http://www.olugg.com/handbags-hermes-handbags-c-490_502.html]
[link= http://www.olugg.com/ghd-hair-straightener-c-488.html] ghd hair straightener [/link]

#
mcr - March 9, 2010 at 6:27 p.m.

I'm a little lost as to how to get the ListPreference out when I'm instantiating preferences from a .xml file.
The objects in my res/xml/prefs.xml file do not get compiled into R. anywhere, as far as I can see.

Add a Comment