Android: change color of disabled text using Theme/Style?
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.