The textColor attribute isn't working. Here's my XML:
<PreferenceCategory
android:title="Title"
android:textColor="#00FF00">
Any ideas?
The textColor attribute isn't working. Here's my XML:
<PreferenceCategory
android:title="Title"
android:textColor="#00FF00">
Any ideas?
Actually just found out that preference category text using colorAccent.
If your app didn't using style of colorAccent, you can go to styles.xml and find <item name="colorAccent">@color/colorPrimary</item>, and change the color as you want.
Using Material Theme, you just have to override the following property :
<style name="YourTheme" parent="@style/Theme.MaterialComponents">
<item name="colorAccent">@color/your_custom_color</item>
</style>
Define your own PreferenceTheme and override colors.
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="preferenceTheme">@style/AppTheme.PreferenceTheme</item>
</style>
<style name="AppTheme.PreferenceTheme" parent="PreferenceThemeOverlay.v14.Material">
<item name="colorAccent">`#color_value`</item>
</style>
Inspired by @AliSh answer, but I needed to only change the colour of one Preference text item. So, for all the Kotlin guys out there:
class TextColorPreference : Preference {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
constructor(
context: Context, attrs: AttributeSet,
defStyle: Int
) : super(context, attrs, defStyle)
override fun onBindViewHolder(holder: PreferenceViewHolder?) {
super.onBindViewHolder(holder)
context?.let {
(holder?.findViewById(android.R.id.title) as? TextView)?.setTextColor(
ContextCompat.getColor(
it,
R.color.colorPrimary
)
)
}
}
}
And then put it in your xml/prefs.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<this.should.be.your.package.TextColorPreference
android:id="@+id/settings_logout"
android:key="@string/prefs_key_logout"
android:title="@string/settings_logout" />
</PreferenceScreen>
For androidx preference library you can extend PreferenceCategory or Preference class like this:
class DangerPreference(
context: Context?,
attrs: AttributeSet?,
): Preference(context, attrs) {
override fun onBindViewHolder(holder: PreferenceViewHolder?) {
super.onBindViewHolder(holder)
holder?.itemView?.findViewById<TextView>(android.R.id.title)?.setTextColor(Color.RED)
}
}
then use it normally in your PreferenceScreen:
<com.github.anastr.myscore.util.pref.DangerPreference
app:key="deleteServerData"
app:title="@string/delete_server_data"
app:iconSpaceReserved="false" />
A lot of the other answers didn't work for my case. I'm using a PreferenceFragmentCompat and I didn't want to have actual code doing this. So I simply made a copy of the preference category xml file and changed the textColor field. The file goes under the Apache license, Version 2.
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2015 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:layout_marginStart="?android:attr/listPreferredItemPaddingLeft"
android:orientation="vertical">
<TextView
android:id="@android:id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:paddingEnd="?android:attr/listPreferredItemPaddingRight"
android:textAlignment="viewStart"
android:textColor="@color/app_accent"
android:textStyle="bold"
tools:ignore="RtlSymmetry"/>
<TextView
android:id="@android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"
android:textColor="?android:attr/textColorSecondary"/>
</LinearLayout>
And imported it in my .xml layout for the Preference fragment:
<PreferenceCategory
android:layout="@layout/preference_category_companion"
android:key="my_preference_title"
android:title="@string/my_preference_title">