Setting summary to describe the current value - android

When I'm trying to set summary when the users select a preference item, it normally saved. But when my app is restarted, the summary is gone.
Here is my code to set the summary for ListPreference and EditTextPreference:
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key){
Preference pref = findPreference(key);
// I feel the problem is happened here
if (pref instanceof ListPreference) {
ListPreference listPref = (ListPreference) pref;
pref.setSummary(listPref.getEntry());
}
// Same problem here
if (pref instanceof EditTextPreference) {
EditTextPreference editText = (EditTextPreference) pref;
pref.setSummary(editText.getEntry().toString());
}
}
Is there something wrong?

if you only want to show the current entry, try to set the summary in your xml:
android:summary="%s"
This works only for the ListPreference (see Doc):
If the summary has a String formatting marker in it (i.e. "%s" or "%1$s"), then the current entry value will be substituted in its place.

The problem might be that the listener is not called on startup (the value is not changed). But you can set the summary dynamically in the XML. For a ListPreference, this is built-in and #FreshD's answer is the way to go. To extend to an EditTextPreference, you need to create your own class. For example
package your.package;
import android.content.Context;
import android.util.AttributeSet;
public class EditTextPreference extends android.preference.EditTextPreference{
public EditTextPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public EditTextPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public EditTextPreference(Context context) {
super(context);
}
#Override
public CharSequence getSummary() {
String summary = super.getSummary().toString();
return String.format(summary, getText());
}
}
And use this in your xml:
<your.package.EditTextPreference
android:key="pref_alpha"
android:summary="Actual value: %s"
android:title="Title"
android:defaultValue="default"
/>

ListPreference listPref = (ListPreference) findPreference("listkey");
listPref.setSummary(listPref.getEntry());
EditTextPreference editText = (EditTextPreference) findPreference("edittextkey");
editText.setSummary(editText.getEntry().toString());
If you have the key then set the summary like above, in oncreate after addpreferences in your preferenceFragment or Activity

I struggled so much to make summaries dynamic. Not exactly sure if this answers the question but wanted to post somewhere so if someone searches they don't have to struggle like i did.
This is the simple solution useSimpleSummaryProvider="true"

Related

EditTextPreference inputType=textPassword not working

I've created a SettingsActivity with the template and put an EditTextPreference in my root_preferences.xml. It should contain a password, so I edited it like this:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/colorBackground">
<EditTextPreference
android:id="#+id/etPassword"
android:dialogTitle="Passwort"
android:inputType="textPassword"
android:key="pref_password"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="Passwort" />
My problem is that neither inputType, singleLine nor setAllOnFocus is working. Do you know what's the problem?
In my case, InputType doesn't hide the password on input and also in the summary.
Here's how I got around the problem
XML file
<PreferenceScreen
xmlns:app="http://schemas.android.com/apk/res-auto">
<EditTextPreference
app:key="pref_password"
app:title="Password"
app:dialogTitle="Set password"
app:useSimpleSummaryProvider="true"
/>
</PreferenceScreen>
In the PreferenceFragmentCompat set in your XML, find your EditTextPreference in the onCreatePreferences section and add an OnBindEditTextListener on it.
public static class YourFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
// Find the password EditText
EditTextPreference etpPassword = getPreferenceManager().findPreference("pref_password");
etpPassword.setOnBindEditTextListener(new EditTextPreference.OnBindEditTextListener() {
#Override
public void onBindEditText(#NonNull EditText editText) {
// Set keyboard layout and some behaviours of the field
editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
// Replace -> android:singleLine="true"
// Not needed for password field, or set it before setTransformationMethod
// otherwise the password will not be hidden
//editText.setSingleLine(true);
// Replace -> android:inputType="textPassword"
// For hiding text
editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
// Replace -> android:selectAllOnFocus="true"
// On password field, you cannot make a partial selection with .setSelection(start, stop)
editText.selectAll();
// Replace -> android:maxLength="99"
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(99)});
}
});
}
}
You also can create your own EditTextPreference class and set other things.
public class YourEditTextPreference extends EditTextPreference {
// Add some preferences, which can be used later for checking
private Integer mPasswordMinSize = 6;
public EditTextPreferencePassword(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
public EditTextPreferencePassword(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public EditTextPreferencePassword(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public EditTextPreferencePassword(Context context) {
super(context);
init();
}
private void init(){
// Set Dialog button text
this.setNegativeButtonText("RETURN");
this.setPositiveButtonText("CHECK");
this.setOnBindEditTextListener(new OnBindEditTextListener() {
#Override
public void onBindEditText(#NonNull EditText editText) {
// Put field parameters here
}
});
}
public void setMinSize(int minSize) { mPasswordMinSize = minSize; }
public Integer getMinSize(){ return mPasswordMinSize; }
// Hide password by stars
#Override
public CharSequence getSummary() {
return getText().equals("") ? super.getSummary() : "*******";
}
}
And in your XML, change <EditTextPreference to <complet.path.to.YourEditTextPreference
As pointed out in a previous answer, inputType is not understood by EditTextPreference and it seems it's not able to pass it to the underlying EditText object by itself, as one would expect.
Adding to the problem, there seems to be a lack of consistency depending on which libraries you are using. The accepted answer doesn't seem to work with AndroidX because there's no such getEditText() method in that EditTextPreference implementation. There's a way, though, by adding a listener that's exactly there for that (Kotlin example):
val myPref = findPreference<EditTextPreference>(
"my_pref_key"
)
myPref?.setOnBindEditTextListener {
it.inputType = InputType.TYPE_CLASS_NUMBER
}
Finally does the trick, but I have wasted a good hour trying to figure out a solution for this, mostly reading outdated questions and answers on the matter.
I see it's been awhile but setting this inside the SettingsFragment onViewCreated() worked for me...
val pw = preferenceScreen.preferenceManager.findPreference<EditTextPreference>("password")
pw?.setOnBindEditTextListener {
it.inputType = InputType.TYPE_CLASS_TEXT + InputType.TYPE_TEXT_VARIATION_PASSWORD
}
You can't do it from XML, but EditTextpreference exposes the EditText so you can do it programmatically. After you load the preferences in your Activity/Fragment, you can do:
EditTextPreference pref = (EditTextPreference)
PreferenceManager.findPreference("edit");
EditText prefEditText = pref.getEditText();
prefEditText.setInputType(InputType.TYPE_CLASS_TEXT); // set properties here
prefEditText.setSingleLine(true);

Custom preference, targetSdkVersion="11": missing indent?

I have a couple of custom DialogPreference implementations floating around, such as this one:
package apt.tutorial;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.TimePicker;
public class TimePreference extends DialogPreference {
private int lastHour=0;
private int lastMinute=0;
private TimePicker picker=null;
public static int getHour(String time) {
String[] pieces=time.split(":");
return(Integer.parseInt(pieces[0]));
}
public static int getMinute(String time) {
String[] pieces=time.split(":");
return(Integer.parseInt(pieces[1]));
}
public TimePreference(Context ctxt) {
this(ctxt, null);
}
public TimePreference(Context ctxt, AttributeSet attrs) {
this(ctxt, attrs, 0);
}
public TimePreference(Context ctxt, AttributeSet attrs, int defStyle) {
super(ctxt, attrs, defStyle);
setPositiveButtonText("Set");
setNegativeButtonText("Cancel");
}
#Override
protected View onCreateDialogView() {
picker=new TimePicker(getContext());
return(picker);
}
#Override
protected void onBindDialogView(View v) {
super.onBindDialogView(v);
picker.setCurrentHour(lastHour);
picker.setCurrentMinute(lastMinute);
}
#Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (positiveResult) {
lastHour=picker.getCurrentHour();
lastMinute=picker.getCurrentMinute();
String time=String.valueOf(lastHour)+":"+String.valueOf(lastMinute);
if (callChangeListener(time)) {
persistString(time);
}
}
}
#Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return(a.getString(index));
}
#Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
String time=null;
if (restoreValue) {
if (defaultValue==null) {
time=getPersistedString("00:00");
}
else {
time=getPersistedString(defaultValue.toString());
}
}
else {
time=defaultValue.toString();
}
lastHour=getHour(time);
lastMinute=getMinute(time);
}
}
They work just fine. However, in an application with android:targetSdkVersion="11" defined, on a XOOM, they show up missing the indent when in the PreferenceActivity:
Also, the font size appears a smidge bigger, at least for the title.
There's nothing in DialogPreference where I am really overriding any formatting behavior for that stuff, AFAIK. The preference XML is unremarkable, other than referring to the above class:
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:key="sort_order"
android:title="Sort Order"
android:summary="Choose the order the list uses"
android:entries="#array/sort_names"
android:entryValues="#array/sort_clauses"
android:dialogTitle="Choose a sort order" />
<CheckBoxPreference
android:key="alarm"
android:title="Sound a Lunch Alarm"
android:summary="Check if you want to know when it is time for lunch" />
<apt.tutorial.TimePreference
android:key="alarm_time"
android:title="Lunch Alarm Time"
android:defaultValue="12:00"
android:summary="Set your desired time for the lunch alarm"
android:dependency="alarm" />
<CheckBoxPreference
android:key="use_notification"
android:title="Use a Notification"
android:defaultValue="true"
android:summary="Check if you want a status bar icon at lunchtime, or uncheck for a full-screen notice"
android:dependency="alarm" />
</PreferenceScreen>
Anyone know where I'm going wrong?
Thanks!
UPDATE
Here is a link to a project that contains this custom preference and a simple preference XML file that demonstrates the problem. Even with just two Java classes, the preference XML, and an arrays.xml file, I get this phenomenon. Here is a compiled APK from this project.
(cross-posting from the associated android-developers thread)
OK, I figured it out.
There are three possible constructors on a Preference:
MyPreference(Context ctxt)
MyPreference(Context ctxt, AttributeSet attrs)
MyPreference(Context ctxt, AttributeSet attrs, int defStyle)
Somewhere along the line, I picked up the pattern of having the
one-parameter constructor chain to the two-parameter constructor
(passing null for the 2nd parameter), and having the two-parameter
constructor chain to the three-parameter constructor (passing 0 for
the 3rd parameter).
And that's not the right answer.
I am hoping that the right answer is to only implement the second
constructor, because the correct default style is internal to Android
(com.android.internal.R.attr.dialogPreferenceStyle). The second
constructor is the one used with inflating preference XML.
Thanks to all for the help!
You can dance with void Preference.setWidgetLayoutResource(int widgetLayoutResId) method, although I prefer to override View Preference.onCreateView(ViewGroup parent) method in my custom Preference class and hack it by adding custom views just below #android:id/summary (use hierarchyviewer utility for details).
The complete method is:
#Override
protected View onCreateView(ViewGroup parent)
{
View ret = super.onCreateView(parent);
View summary = ret.findViewById(android.R.id.summary);
if (summary != null)
{
ViewParent summaryParent = summary.getParent();
if (summaryParent instanceof ViewGroup)
{
final LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup summaryParent2 = (ViewGroup) summaryParent;
layoutInflater.inflate(R.layout.seek_bar_preference, summaryParent2);
seekBar = (SeekBar) summaryParent2.findViewById(R.id.seekBar);
seekBar.setMax(maxValue - minValue);
seekBar.setOnSeekBarChangeListener(this);
statusText = (TextView) summaryParent2.findViewById(R.id.seekBarPrefValue);
unitsRightView = (TextView) summaryParent2.findViewById(R.id.seekBarPrefUnitsRight);
unitsLeftView = (TextView) summaryParent2.findViewById(R.id.seekBarPrefUnitsLeft);
}
}
return ret;
}
Source code of my SeekBarPreference class based on code from http://robobunny.com can be downloaded here
I tried your code on the emulator. There is no problem with the code that you have given, and all the lines have the same formatting; but they all look more similar (in format) to the third preference (Lunch Alarm Time) than the others.
It looks like the other three preferences are getting indented more than required. So, maybe you have some global formatting style that is used, but not picked up by the TimePreference preference.
EDIT: OK. So, the above is not (completely) true. There is definitely a problem when I tried with the target sdk set to HoneyComb. But on setting the theme for the PreferenceActivity class as android:theme="#android:style/Theme.Black", there is a consistency in the look of all the preferences as shown below.
This style looks similar to Froyo, but not the HoneyComb; in the latter, the title font is smaller and there is more indentation. Probably, the default theme is not being assigned to Custom Preferences - just a guess :) A workaround would be to assign the default theme to your preference activity explicitly, but I don't know what the default theme in HoneyComb is (and whether it can be set).
The solution which helped me:
I have replaced
public TimePreference(Context ctxt, AttributeSet attrs) {
this(ctxt, attrs, 0);
}
with
public TimePreference(Context ctxt, AttributeSet attrs) {
this(ctxt, attrs, ctxt.getResources().getSystem().getIdentifier("dialogPreferenceStyle", "attr", "android"));
}
As you can see, I replaced third argument 0 with ctxt.getResources().getSystem().getIdentifier("dialogPreferenceStyle", "attr", "android") in the second constructor of custom preference class.
To make the accepted answer more clear. You only need this constructor:
public TimePreference(Context ctxt, AttributeSet attrs) {
// this(ctxt, attrs, 0); // wrong
super(ctxt, attrs);
}

Is there a way to display the current setting of a PreferenceActivity?

I feel like I must be missing something, but I just don't see what it is... I have a PreferenceActivity with a bunch of various preferences (some are lists, some are just text fields) and it all works fine, but unless I explicitly write each item's value to the summary (which is obviously not intended for this purpose) I don't see how (or where) the items display what they are currently set to. When I click on them the various views show up with the correct settings, but that's clearly not the intention.
Do I have to create my own custom List item of some sort that has a field that displays the currently populated value of each element?
Unfortunately the default PreferencesActivity doesn't display the values: what you're doing is really the way to go if you care to have all the preferences displayed at a glance.
If you still want to go down the programming direction then look at this thread: How do I display the current value of an Android Preference in the Preference summary?
Has everything there.
Create another preference field: summary.
Update it whenever a preference field is updated, or when displaying the preferences screen.
The user will be able to "update" the summary value, but whenever he/she enters preferences, the correct value will be displayed.
For ListPreferences, this is built-in and you can use
android:summary="Actual value: %s"
For EditTextPreferences, you can easily create your own class:
package your.package.preference;
import android.content.Context;
import android.util.AttributeSet;
public class EditTextPreference extends android.preference.EditTextPreference{
public EditTextPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public EditTextPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public EditTextPreference(Context context) {
super(context);
}
#Override
public CharSequence getSummary() {
String summary = super.getSummary().toString();
return String.format(summary, getText());
}
}
And use this in your xml:
<your.package.EditTextPreference
android:key="pref_alpha"
android:summary="Actual value: %s"
android:title="Title"
android:defaultValue="default"
/>

Calling setDefaultValue() on a custom Preference class does not set the default value. Why?

I'm extending PreferenceActivity for my settings screen. In this preference activity i have a couple of preferences one of which is custom made. The problem is as follows:
in this custom preference (which extends from ListPreference) i want to be able to set the default value, so i override the setDefaultValue() method. In this method i do some parsing so it'll take the correct value. When i'm trying to read this value with the getValue() function it just returns null.
So i figured, what happens when i just put some hardcoded value in there (you know, maybe i did something wrong, wouldn't be the first time). Well, i still get null back.
Any ideas what i'm doing wrong?
Edit:
Setting the defaultValue in the xml file isn't really an option because the values aren't known until i retrieve them.
I made a workaround:
When app is started for the first time: get data
Set the values in the preference.
This way i set the default preference when i'm collection the data
I finally found the solution (somewhere besides StackOverflow, for once).
When you create a custom Preference class,
You need to implement onSetInitialValue as XåpplI'-I0llwlg'I - pointed out
You also need to implement onGetDefaultValue(TypedArray a, int index)
For example, if the custom preference is saved as an int,
#Override
protected void onSetInitialValue(boolean restore, Object defaultValue) {
setValue(restore ? getPersistedInt(FALLBACK_DEFAULT_VALUE) : (Integer) defaultValue);
}
#Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return a.getInteger(index, FALLBACK_DEFAULT_VALUE);
}
Now PreferenceManager.setDefaultValues() finally loads the android:defaultValue for the custom preferences too. Still no fix for nulls and false, but there are workarounds for those posted elsewhere.
If you want to call getValue() after calling setDefaultValue() to retrieve a default value the first time your PreferenceActivity opens, you need to override onSetInitialValue() in your Preference subclass. Otherwise, the default value will not be set when you call getValue() and it will return a null (as you experienced).
For example, if your default value is an integer, your onSetInitialValue() might look like this:
#Override
protected void onSetInitialValue(boolean restore, Object defaultValue)
{
setValue(restore ? getPersistedInt(DEFAULT_VALUE) : (Integer) defaultValue);
}
DEFAULT_VALUE is just a private constant inside the Preference to be used in case the persisted int cannot be retrieved. setValue() is the public setter to complement your getValue() public getter, and should look something like this:
public int getValue()
{
return mValue;
}
public void setValue(int value)
{
if (value != mValue)
{
mValue = value;
persistInt(value);
}
}
For more information about onSetInitialValue(), refer to the API documentation here.
It's also a good idea to look at the source code of the Preference class (here) to understand why onSetInitialValue() needs to be implemented. In particular, have a look at setDefaultValue(), and then look at dispatchSetInitialValue().
setDefaultValue doesn't work the way you think it does. Look at the source of Preference.java and you'll the logic behind it all.
The preferred way to set a default is to specify the android:defaultValue attribute in the preferences.xml file of your app.
I converted preferences .xml to code. All setDefaultValues works well there.
val screen = preferenceManager.createPreferenceScreen(context)
val editText = EditTextPreference(context).apply {
setIcon(R.drawable.lobat_cloud)
key = "key"
title = "MyPreferences"
...
setDefaultValue("My Default Value")
}
screen.addPreference(editText)
// add other preferences
preferenceScreen = screen
more info
P.S: I found this way more smaller and clear than customizing all preferences or other answers.
You can extend preference and set the default value during constructing like this:
package com.example.package.preference;
public class CustomPreference extends ListPreference{
public CustomPreference(Context context) {
super(context);
init();
}
public CustomPreference(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
Object anyDefaultValueFromCode = ...
setDefaultValue(anyDefaultValueFromCode );
}
}
then you can use it from XML like this:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="alarm_prefs_screen"
android:title="#string/set_alarm" >
<com.example.package.preference.CustomPreference
android:key="custom_preference"
android:title="#string/any_title" />
</PreferenceScreen>
This is what I did and worked for me:
class DefaultValueEditTextPreference : androidx.preference.EditTextPreference {
#Suppress("unused")
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes)
#Suppress("unused")
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
#Suppress("unused")
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
#Suppress("unused")
constructor(context: Context?) : super(context)
init {
text = ... //the default, dynamic text that you want to have
}
}
I think this works too at anytime.
Preference aaa = (Preference) findPreference("xxx");
aaa.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
// For edit text preference
((EditTextPreference)preference).getEditText().setText("foobar");
// for list preference
(ListPreference)preference).setValue("foobar");
// etc ...
return true;
}
});
This code will detect when the dialog is about to launch and populate the EditText or List in the dialog with your default value.

Android preferences summary default color?

I have installed my app in a real phone, and even though in the emulator all the texts of the
preferences summaries seem to be in the same color, in the real phone the color is different (some kind of blue... but I guess it depends on the phone's model).
How can I set this color to my custom preference component?
(I have implemented my own seek bar, and its summary text color is different from all the other components text color...).
Thanks!
Preference pUpdate = findPreference("sys_setting_update");
pUpdate.setSummary(Html.fromHtml("<font color=\"#B0C4DE\">This is content</font>"));
use Html.fromHtml("<font color=\"#B0C4DE\">This is content</font>") to setSummary
I found these: android:textAppearance="?android:attr/textAppearanceLarge"
and android:textAppearance="?android:attr/textAppearanceSmall"
seem to do the trick.
I have figured out a way to retrieve the default color used by the Android device your application is running in. It is a bit tricky and requieres that you retrieve the color being shown from another Preference Summary View of your activity and store it in runtime.
Then you can use the same color code in other Views of other preferences, assuring that you will allways get the same color code Android assigned to the standard preferences. Here is how I did it:
My preferences activity has a normal CheckBoxPreference that I use to activate or deactivate a service. I have extended CheckBoxPreference as follows, so my extension retrieves in rutime the default color Android finally gave to the summary of that CheckBoxPreference:
public class MyCheckBoxPreference extends android.preference.CheckBoxPreference {
private static int sSummaryColor = Color.WHITE;
private static boolean sInitialized = false;
public MyCheckBoxPreference(Context context) {
super(context);
}
public MyCheckBoxPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyCheckBoxPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void onBindView(View view) {
super.onBindView(view);
if (!sInitialized) {
sSummaryColor = getSummaryColor(view);
sInitialized = true;
}
}
private int getSummaryColor(View view) {
int color = Color.WHITE;
// Gets the color android gave to the summary by default
TextView summaryView = (TextView) view.findViewById(android.R.id.summary);
if (summaryView != null) {
ColorStateList list = summaryView.getTextColors();
if (list != null) {
color = list.getDefaultColor();
}
}
return color;
}
public static int getSummaryColor() {
return sSummaryColor;
}
}
In my preferences.xml I instantiate that preference as MyCheckBoxPreference instead of just CheckBoxPreference:
<org.yourpackage.MyCheckBoxPreference
android:title="#string/preference_title_activate"
android:defaultValue="false"
android:summary="#string/preference_summary_activate_off"
android:summaryOff="#string/preference_summary_activate_off"
android:key="preference_activate">
</org.yourpackage.MyCheckBoxPreference>
The MyCheckBoxPreference has to be instantiated once before retrieving the summary color with MyCheckBoxPreference.getSummaryColor().
Now you can set the color of other customized preferences from onBindView(View):
public class MyCustmizedPreference extends Preference {
public MyCustmizedPreference (Context context) {
super(context);
setLayoutResource(R.layout.my_customized_preference);
}
#Override
public void onBindView(View view) {
super.onBindView(view);
TextView summaryView = (TextView) view.findViewById(android.R.id.summary);
if (summaryView != null) {
summaryView.setTextColor(MyCheckBoxPreference.getSummaryColor());
}
}
}
It actually works under Samsung Galaxy S. I have also tested that it doesn't break anything under the emulator.
The Samsung Galaxy S phones have their own Preference layout with the text color specified for the Summary line. Even though a TextAppearance.Small is specified the textColor attribute of the layout is overriding the text appearance.
I don't think this is possible. I am able to change the background color and the title text color, but not the summary color.
Background:
getListView().setBackgroundColor(Color.TRANSPARENT);
Title text:
Preference yourpreference = findPreference("yourpreference");
TextView tv = (TextView)yourpreference.getView(null, getListView());
tv.setTextColor(...);
Sorry I couldn't help more...
I had the same problem and I've been experimenting with my custom seekbar-preference's style. Finally these lines in onCreateView method of seekBarPreference.java show preference's summary with default text color:
TextView summaryText = new TextView(getContext());
summaryText.setText(getSummary());
summaryText.setTextAppearance(getContext(), android.R.style.TextAppearance_Small);
I use it on preference_screen.xml:
<com.asdasf.SeekBarPreferencias
android:key="#string/pref_seekBar_distance_key"
android:id="#+id/mySeekBarPreference"
android:title="#string/pref_seekBar_distance_title"
android:summary="#string/pref_seekBar_distance_summary"
android:max="50"
android:defaultValue="12"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
I hope it will be useful...(and that I have written well my first answer)
Regard!

Categories

Resources