Android: change color of disabled text using Theme/Style?

16,159

Solution 1

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));
    }
}

Solution 2

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

Solution 3

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.

Share:
16,159

Related videos on Youtube

Android Noob
Author by

Android Noob

Updated on June 21, 2020

Comments

  • Android Noob
    Android Noob almost 4 years

    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.

  • Noa Drach
    Noa Drach over 8 years
    Excellent solution with all the needed step to implement it - helped me a lot
  • SMBiggs
    SMBiggs over 7 years
    Would this work for changing the color of checkbox or radio button icons? If so, what would the item names in the styles.xml file be?
  • Ahmet Noyan Kızıltan
    Ahmet Noyan Kızıltan over 7 years
    Make sure "android:textColor" is not defined, or this doesn't work.
  • k2col
    k2col over 6 years
    I found that android:textColorPrimary didn't work for the title, however android:textColor did (in styles.xml)