Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 858
Android Noob Avatar asked Jul 21 '13 04:07

Android Noob


People also ask

What is property changes the color of text Android?

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.

How can I change the font color of my Android app name?

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.

How do I turn off the color on my Android?

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.


3 Answers

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

like image 82
W0rmH0le Avatar answered Oct 14 '22 04:10

W0rmH0le


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));
    }
}
like image 41
Android Noob Avatar answered Oct 14 '22 06:10

Android Noob


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.

like image 31
android developer Avatar answered Oct 14 '22 05:10

android developer