jskubick

jskubick

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

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.

First Post!

rgianassi

rgianassi

Hi everyone,

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

        <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

Popular Pragmatic Bookshelf topics Top

New
jesse050717
Title: Web Development with Clojure, Third Edition, pg 116 Hi - I just started chapter 5 and I am stuck on page 116 while trying to star...
New
sdmoralesma
Title: Web Development with Clojure, Third Edition - migrations/create not working: p159 When I execute the command: user=&gt; (create-...
New
simonpeter
When I try the command to create a pair of migration files I get an error. user=&gt; (create-migration "guestbook") Execution error (Ill...
New
mikecargal
Title: Hands-On Rust (Chap 8 (Adding a Heads Up Display) It looks like ​.with_simple_console_no_bg​(SCREEN_WIDTH*2, SCREEN_HEIGHT*2...
New
jskubick
I think I might have found a problem involving SwitchCompat, thumbTint, and trackTint. As entered, the SwitchCompat changes color to hol...
New
nicoatridge
Hi, I have just acquired Michael Fazio’s “Kotlin and Android Development” to learn about game programming for Android. I have a game in p...
New
brunogirin
When running tox for the first time, I got the following error: ERROR: InterpreterNotFound: python3.10 I realised that I was running ...
New
hazardco
On page 78 the following code appears: &lt;%= link_to ‘Destroy’, product, class: ‘hover:underline’, method: :delete, data: { confirm...
New
ggerico
I got this error when executing the plot files on macOS Ventura 13.0.1 with Python 3.10.8 and matplotlib 3.6.1: programming_ML/code/03_...
New

Other popular topics Top

AstonJ
SpaceVim seems to be gaining in features and popularity and I just wondered how it compares with SpaceMacs in 2020 - anyone have any thou...
New
New
New
AstonJ
We have a thread about the keyboards we have, but what about nice keyboards we come across that we want? If you have seen any that look n...
New
AstonJ
Inspired by this post from @Carter, which languages, frameworks or other tech or tools do you think is killing it right now? :upside_down...
New
mafinar
Crystal recently reached version 1. I had been following it for awhile but never got to really learn it. Most languages I picked up out o...
New
PragmaticBookshelf
Rails 7 completely redefines what it means to produce fantastic user experiences and provides a way to achieve all the benefits of single...
New
First poster: joeb
The File System Access API with Origin Private File System. WebKit supports new API that makes it possible for web apps to create, open,...
New
PragmaticBookshelf
Author Spotlight Mike Riley @mriley This month, we turn the spotlight on Mike Riley, author of Portable Python Projects. Mike’s book ...
New
First poster: bot
Large Language Models like ChatGPT say The Darnedest Things. The Errors They MakeWhy We Need to Document Them, and What We Have Decided ...
New