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

Where Next?

Popular Pragmatic Bookshelf topics Top

jeffmcompsci
Title: Design and Build Great Web APIs - typo “https://company-atk.herokuapp.com/2258ie4t68jv” (page 19, third bullet in URL list) Typo:...
New
edruder
I thought that there might be interest in using the book with Rails 6.1 and Ruby 2.7.2. I’ll note what I needed to do differently here. ...
New
lirux
Hi Jamis, I think there’s an issue with a test on chapter 6. I own the ebook, version P1.0 Feb. 2019. This test doesn’t pass for me: ...
New
herminiotorres
Hi! I know not the intentions behind this narrative when called, on page XI: mount() |&gt; handle_event() |&gt; render() but the correc...
New
gilesdotcodes
In case this helps anyone, I’ve had issues setting up the rails source code. Here were the solutions: In Gemfile, change gem 'rails' t...
New
jskubick
I’m under the impression that when the reader gets to page 136 (“View Data with the Database Inspector”), the code SHOULD be able to buil...
New
jskubick
I found an issue in Chapter 7 regarding android:backgroundTint vs app:backgroundTint. How to replicate: load chapter-7 from zipfile i...
New
hazardco
On page 78 the following code appears: &lt;%= link_to ‘Destroy’, product, class: ‘hover:underline’, method: :delete, data: { confirm...
New
Henrai
Hi, I’m working on the Chapter 8 of the book. After I add add the point_offset, I’m still able to see acne: In the image above, I re...
New
roadbike
From page 13: On Python 3.7, you can install the libraries with pip by running these commands inside a Python venv using Visual Studio ...
New

Other popular topics Top

PragmaticBookshelf
Design and develop sophisticated 2D games that are as much fun to make as they are to play. From particle effects and pathfinding to soci...
New
DevotionGeo
I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
New
Rainer
My first contact with Erlang was about 2 years ago when I used RabbitMQ, which is written in Erlang, for my job. This made me curious and...
New
AstonJ
Just done a fresh install of macOS Big Sur and on installing Erlang I am getting: asdf install erlang 23.1.2 Configure failed. checking ...
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
AstonJ
Do the test and post your score :nerd_face: :keyboard: If possible, please add info such as the keyboard you’re using, the layout (Qw...
New
AstonJ
In case anyone else is wondering why Ruby 3 doesn’t show when you do asdf list-all ruby :man_facepalming: do this first: asdf plugin-upd...
New
AstonJ
We’ve talked about his book briefly here but it is quickly becoming obsolete - so he’s decided to create a series of 7 podcasts, the firs...
New
PragmaticBookshelf
Author Spotlight Jamis Buck @jamis This month, we have the pleasure of spotlighting author Jamis Buck, who has written Mazes for Prog...
New
PragmaticBookshelf
Author Spotlight Rebecca Skinner @RebeccaSkinner Welcome to our latest author spotlight, where we sit down with Rebecca Skinner, auth...
New

Sub Categories: