# Kotlin and Android Development featuring Jetpack: SwitchCompat's thumbTint and trackTint being ignored (page 76)

**URL:** <https://forum.devtalk.com/t/kotlin-and-android-development-featuring-jetpack-switchcompats-thumbtint-and-tracktint-being-ignored-page-76/20293>\
**Category:** PragProg Customers\
**Tags:** book-kotlin-and-android-development-featuring-jetpack, errata\
**Created:** [5 September 2021 22:18 UTC](https://forum.devtalk.com/t/kotlin-and-android-development-featuring-jetpack-switchcompats-thumbtint-and-tracktint-being-ignored-page-76/20293 "2021-09-05T22:18:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jskubick](https://forum.devtalk.com/user_avatar/forum.devtalk.com/jskubick/32/3328_2.png) [@jskubick](https://forum.devtalk.com/u/jskubick)\
**Post date:** [5 September 2021 22:18 UTC](https://forum.devtalk.com/t/kotlin-and-android-development-featuring-jetpack-switchcompats-thumbtint-and-tracktint-being-ignored-page-76/20293/1 "2021-09-05T22:18:28Z")

</div>

I think I might have found a problem involving SwitchCompat, thumbTint, and trackTint.

As entered, the SwitchCompat changes color to holo\_blue\_bright when the switch is enabled and checked, but the colors specified for state\_enabled=false and state\_checked=false seem to be getting ignored.

I started by changing the first color (state\_enabled=false) in ai\_toggle\_color.xml to holo\_red\_light, to make it blatantly obvious when it’s being used… then launched it in the Emulator (API 30):

SwitchCompat enabled=false, checked=moot: light gray (vs holo\_red\_light)

SwitchCompat enabled=true, checked=true: holo\_blue\_bright, as expected.

SwitchCompat enabled=true, checked=false: dark gray (vs holo\_green\_light)

**Searching around on StackOverflow, I found a few posts that seem to suggest that this might be due to a theme or style overriding the colors set via thumbTint and trackTint.**

I tried running the same set of experiments under the emulator using API 27, to see whether it might be due to a recent change in Android’s behavior. Same outcome.

As a final test, I loaded Chapter 3 from the zip archive directly into Android Studio, and ran it as-is in the emulator under API 30… and got the same results as before.

For whatever reason, SwitchCompat appears to be ignoring the color choices set by thumbTint and trackTint unless enabled and checked are both true.

---

<div class="post-metadata">

**Author:** ![rgianassi](https://forum.devtalk.com/user_avatar/forum.devtalk.com/rgianassi/32/3807_2.png) [@rgianassi](https://forum.devtalk.com/u/rgianassi)\
**Post date:** [30 December 2021 19:10 UTC](https://forum.devtalk.com/t/kotlin-and-android-development-featuring-jetpack-switchcompats-thumbtint-and-tracktint-being-ignored-page-76/20293/2 "2021-12-30T19:10:52Z")

</div>

Hi everyone,

maybe I found a solution. Here is what I’ve done:

```xml
        <androidx.appcompat.widget.SwitchCompat
            android:id="@+id/switch_player_type"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="@={player.isHuman}"
            android:enabled="@{player.isIncluded}"
            android:thumb="@drawable/ai_toggle_bg"
            app:trackTint="@color/ai_toggle_color"
            android:visibility="@{player.canBeToggled ? View.VISIBLE : View.INVISIBLE}"
            app:layout_constraintBottom_toBottomOf="@id/edit_text_player_name"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@id/edit_text_player_name" />

```

i.e. I removed the `android:thumbTint` attribute and I changed `android:trackTint` to `app:trackTint`.  
Probably some API has been recently changed.

Hope this helps,  
Roberto

---

<div class="post-metadata">

**Author:** ![jaranda](https://forum.devtalk.com/user_avatar/forum.devtalk.com/jaranda/32/4171_2.png) [@jaranda](https://forum.devtalk.com/u/jaranda)\
**Post date:** [13 May 2022 01:30 UTC](https://forum.devtalk.com/t/kotlin-and-android-development-featuring-jetpack-switchcompats-thumbtint-and-tracktint-being-ignored-page-76/20293/3 "2022-05-13T01:30:00Z")

</div>

### @mfazio23

To get the exact Image as in this section I had to define two color state list resources. First I renamed the original ‘ai\_toggle\_color.xml’ to “ai\_toggle\_track\_color.xml”. And created a new ai\_toggle\_color.xml with the following values:

```auto
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:color="@android:color/darker_gray"
        android:state_enabled="false" />
    <item
        android:color="@android:color/holo_blue_dark"
        android:state_checked="true" />
    <item
        android:color="@android:color/holo_green_dark"
        android:state_checked="false"/>
</selector>

```

Once I had two color state lists (ai\_toggle\_color.xml and ai\_toggle\_track\_color.xml) I modified to use them respectively:

```auto
app:thumbTint="@color/ai_toggle_color"
app:trackTint="@color/ai_toggle_track_color"
```
