Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change checkbox text color when checked

I would like to change color of the text when CheckBox is checked. This is what I have for now:

<CheckBox
    android:id="@+id/checkbox"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:background="@drawable/states"
    android:gravity="center_horizontal|center_vertical"
    android:button="@null"
    android:text="test/>

Checkbox background is normally changed when checkbox is checked. The problem is text. It's always the same color. How can I also change text color when checkbox is checked?

This is how I change states for checkbox background (I removed extras because of simplicity):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="false">
        <layer-list >
            <item>
                <shape android:shape="oval">
                </shape>
            </item>
        </layer-list>
    </item>

    <item android:state_checked="true" >
        <layer-list >
            <item>
                <shape android:shape="oval">
                </shape>
            </item>
        </layer-list >
    </item>
</selector>
like image 208
user4386126 Avatar asked Dec 30 '14 23:12

user4386126


3 Answers

You can use selector as well but instead of /res/drawable put it in /res/color.

/res/color/text_my_checked.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="#ffcc00"/> <!-- checked -->
    <item android:color="#ffffff"/> <!-- anything else -->
</selector>

You would get this color as ColorStateList by calling getResources().getColorStateList(R.color.text_my_checked).

EDIT:

Ever since appcompat-v7 24.0.0 we can use theme references in color state lists on platforms down to API 9. This was originally introduced in API 23.

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="?colorControlActivated"/> <!-- checked -->
    <item android:state_checked="true" android:state_enabled="false" app:alpha="?android:disabledAlpha" android:color="?colorControlActivated"/> <!-- checked, disabled -->
    <item android:color="?android:textColorPrimary"/> <!-- anything else -->
</selector>

Call AppCompatResources.getColorStateList(checkbox.getContext(), R.color.text_my_checked) to obtain the color state list.

like image 162
Eugen Pechanec Avatar answered Oct 23 '22 09:10

Eugen Pechanec


I'm not sure why there isn't mentioned setting the text color through xml. If you create text_my_checked.xml like @Eugen mentioned. You are able to set it in xml like this

<CheckBox
    android:id="@+id/checkbox"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:background="@drawable/states"
    android:gravity="center_horizontal|center_vertical"
    android:button="@null"
    android:textColor="@drawable/text_my_checked"
    android:text="test/>
like image 27
Ban Markovic Avatar answered Oct 23 '22 07:10

Ban Markovic


You might do that programmatically, recalling your Checkbox and setting an onCheckedChangeListener.

    CheckBox cb = (CheckBox) findViewById(R.id.checkbox);
    cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) { buttonView.setTextColor(...) }
            if (!isChecked) { buttonView.setTextColor(...); }
        }
    });
like image 27
natario Avatar answered Oct 23 '22 07:10

natario