user5323488
user5323488

Reputation: 21

Change the title text color of EditTextPreference

How can I change the color of the title and summary in the following preference XML:

<EditTextPreference
    android:key="username"
    android:title="Your Name"
    android:summary="Please provide your username." />

I tried this in my styles.xml:

<style name="SettingsTheme" parent="@style/AppBaseTheme">
    <item name="android:textColorPrimary">@color/green</item>
    <item name="android:textColorSecondary">@color/red</item>
    <item name="android:textColorTertiary">@color/blue</item>
</style>

textColorPrimary changes the color of the title, but it also changes the color of the text in my toolbar (which I don't want).

textColorSecond seems to correctly change the color of the summary though.

How can I change the color of the title without affecting the toolbar text color?

Upvotes: 2

Views: 2391

Answers (4)

Hezy Ziv
Hezy Ziv

Reputation: 5558

make a dedicated style for the preference screen

<style name="PreferenceTheme" parent="@style/AppBaseTheme">
    <item name="android:textColorPrimary">@color/green</item>
    <item name="android:textColorSecondary">@color/red</item>
</style>

Apply this theme only to your PreferenceFragment or PreferenceActivity

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.PreferenceTheme); 
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }

Upvotes: 0

Angel
Angel

Reputation: 61

I had the same problem recently. Just found an answer to it. I made a custom EditTextPreference and did this in my code to change the title and summary color, I also tried changing icon tint (color) and it is working, all you need to do is just finding the view you need to change and applying your changes to it as shown below:

class MyEditTextPreference : EditTextPreference {

constructor(context: Context) : super(context)

constructor(context: Context, attr : AttributeSet?) : super(context, attr)

constructor(context: Context, attr: AttributeSet?, i1: Int) : super(context, attr, i1)

constructor(context: Context, attr: AttributeSet?, i1: Int, i2: Int) : super(context, attr, i1, i2)

override fun onBindViewHolder(holder: PreferenceViewHolder) {
    super.onBindViewHolder(holder)
    (holder.findViewById(android.R.id.title) as TextView).setTextColor(Color.WHITE)
    (holder.findViewById(android.R.id.icon) as PreferenceImageView).imageTintList = ColorStateList.valueOf(Color.WHITE)
    (holder.findViewById(android.R.id.summary) as TextView).setTextColor(ResourcesCompat.getColor(context.resources, R.color.test_color, null))
    }
}

and in your xml file you will have to use this

<mypackage.MyEditTextPreference
        app:icon="@drawable/ic_test"
        app:key="testKey"
        app:title="@string/testString"
        app:useSimpleSummaryProvider="true" />

Note 1: I used a simple summay provider but you can use whatever summery you want.

Note 2: The codes are written in kotlin but you can simply convert them into Java.

Upvotes: 0

Andrew Breen
Andrew Breen

Reputation: 695

You need to define the style for the EditTextPreference explicitly like so:

<style name="SettingsTheme" parent="@style/AppBaseTheme">
    <item name="android:editTextPreferenceStyle">@style/MyStyle</item>
</style>

<style name="MyStyle" parent="@android:style/Preference.DialogPreference.EditTextPreference">
    ...
</style>

This will then set whatever styles you define in MyStyle only to where the EditTextPreference default style was before.

Upvotes: 0

Karakuri
Karakuri

Reputation: 38605

I think the easiest method would be to subclass EditTextPreference and adjust the title color in either onCreateView() or onBindView().

public class MyEditTextPreference extends EditTextPreference {

    // constructors omitted

    @Override
    protected void onBindView(View view) {
        TextView titleView = (TextView) view.findViewById(android.R.id.title);
        int color = getContext().getResources().getColor(R.color.preference_title);
        titleView.setTextColor(color);
    }
}

Then in your preferences XML you would use your class instead (fully qualified name):

<com.package.MyEditTextPreference
    android:key="username"
    android:title="Your Name"
    android:summary="Please provide your username." />

Upvotes: -1

Related Questions