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
See screen shot at http://picasaweb.google.com/phnixwxz/ScreenShots#5327684730584667154
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.
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" ;)
nice! so how could I use EditTextPreferenceWithValue
and ListPreferenceWithValue
in the xml file? or how do I hook these clases to the PreferenceActivity?
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 :(
Awesome information and will prove very useful.
Great code. Thanks for sharing this.
Does this also work when the listpref's?
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]
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