I have the following colors defined in my color.xml:
<color name="gold">#d49e43</color>
<color name="gold_disabled">#80d49e43</color>
And the following theme:
<style name="Theme.Example" parent="@style/Theme.Sherlock">
<item name="android:textColor">@color/gold</item>
</style>
In my SettingsActivity, I have a CheckBoxPreference and a Preference that depends on it. When The CheckBoxPreference is unchecked, the Preference is disabled, however, because of the custom gold text color that I set, it doesn't get "greyed out" like it does with the default color. How do I change this in XML? I've tried setting:
<item name="android:textColorPrimaryDisableOnly">@color/gold_disabled</item>
<item name="android:textColorPrimaryInverseDisableOnly">@color/gold_disabled</item>
<item name="android:textColorPrimaryNoDisable">@color/gold_disabled</item>
<item name="android:textColorSecondaryNoDisable">@color/gold_disabled</item>
<item name="android:textColorPrimaryInverseNoDisable">@color/gold_disabled</item>
<item name="android:textColorSecondaryInverseNoDisable">@color/gold_disabled</item>
but nothing seems to work.
Android TextView – Text Color. TextView Text Color – To change the color of text in TextView, you can set the color in layout XML file using textColor attribute or change the color dynamically in Kotlin file using setTextColor() method.
Search for and select Open App, and then, on the New Shortcut page, tap Choose. Locate the app whose appearance you want to change. Back on the New Shortcut page, you'll see the app name; tap More (three dots), change the app's name, tap its icon, select Color, and choose a new color.
Drag the Grayscale icon up into the panel of icons, and you'll have one-tap access to your phone's grayscale mode whenever you need it. The grayscale toggle switch is part of a new Digital Wellbeing suite of tools built into the latest versions of Android.
I know I'm late. However, I had exactly same problem and I fixed just now.
I found a way to fix it using resource files only. I found the answer here: https://stackoverflow.com/a/17123161/4860513
Basically, you can create a color selector under: res/color/
Note: You must create folder color if it does not exist.
For me, I did it:
res\color\primary_text_color_selector.xml (For Title)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="false" android:color="#80000000"/>
<item android:color="#FF000000"/>
</selector>
res\color\secondary_text_color_selector.xml (For Summary)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="false" android:color="#80000000"/>
<item android:color="#C0000000"/>
</selector>
Then, In my preference style, I did:
res\values\styles.xml
<!-- Preference Screen Theme -->
<style name="AppTheme_PreferenceScreenStyle">
<item name="android:textColorPrimary">@color/primary_text_color_selector</item>
<item name="android:textColorSecondary">@color/secondary_text_color_selector</item>
</style>
in SettingsFragmentActivity.java
public class SettingsFragmentActivity extends PreferenceFragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
PreferenceScreen preferenceScreen = getPreferenceScreen();
mContext = preferenceScreen.getContext();
mContext.setTheme(R.style.AppTheme_PreferenceScreenStyle);
...
}
}
This way, Title and Summary are grayed out when option is disabled.
I'm just sharing this solution since I have same problem and it may help someone
I figured this out more or less by accident, but if you subclass Preference and override the onBindView(), you can achieve the "grayed out" effect when a preference is disabled:
@Override
protected void onBindView(View view) {
// TODO Auto-generated method stub
super.onBindView(view);
TextView title = (TextView)view.findViewById(android.R.id.title);
TextView summary = (TextView)view.findViewById(android.R.id.summary);
if (title.isEnabled()) {
title.setTextColor(getContext().getResources().getColor(R.color.gold));
}
else {
title.setTextColor(getContext().getResources().getColor(R.color.gold_disabled));
}
if (summary.isEnabled()) {
summary.setTextColor(getContext().getResources().getColor(R.color.orange));
}
else {
summary.setTextColor(getContext().getResources().getColor(R.color.orange_disabled));
}
}
If you look at how Google did it for their themes, you would notice this for light (Platform.AppCompat.Light ) :
<item name="android:textColorPrimary">@color/abc_primary_text_material_light</item>
<item name="android:textColorPrimaryInverse">@color/abc_primary_text_material_dark</item>
And this for dark theme (Platform.AppCompat) :
<item name="android:textColorPrimary">@color/abc_primary_text_material_dark</item>
<item name="android:textColorPrimaryInverse">@color/abc_primary_text_material_light</item>
And the colors of those are set as such:
abc_primary_text_material_dark.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="@color/primary_text_disabled_material_dark"/>
<item android:color="@color/primary_text_default_material_dark"/>
</selector>
abc_primary_text_material_light.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="@color/primary_text_disabled_material_dark"/>
<item android:color="@color/primary_text_default_material_dark"/>
</selector>
So, you should do the same (create the files and the references to them in the themes file), to handle light&dark theme, and to handle the states of the text color.
To use it in the app, you can use ?android:textColorPrimary
or ?android:textColorPrimaryInverse
.
Same goes for secondary text color and tertiary, BTW.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With