Kotlin and Android Development featuring Jetpack: android:tint vs app:tint (chapter 7, p186)

I found an issue in Chapter 7 regarding android:backgroundTint vs app:backgroundTint.

How to replicate:

  1. load chapter-7 from zipfile into Android Studio

  2. view layout_coin_slot.xml

  3. Notice that Android Studio is complaining about android:tint="@color/coin_slot_color" in the coin slot’s ImageView

Changing it to app:tint="@color/coin_slot_color" fixes it.

The code in the zipfile is consistent with the code in the text… but both are actually wrong (at least, as far as ArcticFox in October 2021 is concerned) and the prefix has to be changed in order to successfully compile.

I think adding the dependency on com.google.android.material on p179 is what caused the use of android:tint to break in chapter 7 (because it worked just fine in chapter 6). I’m not 100% sure, but it looks like Google’s original implementation of the Material library had a bug, and they later decided to fix it in a way that caused any code that used the original syntax (with the ‘android’ prefix) to break.

Due to the namespace change for app:tint, the section about changing the other two Views becomes confusing.

The ImageView needs app:tint="@color/coin_slot_color", but the View needs android:backgroundTint="@color/coin_slot_color", and the TextView needs android:textColor="@color/coin_slot_color"

Hey Jeff,
Which version of the the Material library are you using here?
Also, if the app:tint version also works with older versions, I’ll just update it to use that instead.

It looks like I’m using com.google.android.material:material:1.4.0

I tried changing it to 1.0.0 to see whether it would fix the reported error, but it didn’t. Android Studio still says ‘android:tint’ needs to be changed to ‘app:tint’

It actually looks like my theory blaming it on the addition of the material dependency was wrong. After trying 1.0.0, I tried commenting it out, then cleaned, re-synced, and rebuilt the project… Android Studio STILL complained about the app-vs-android prefix for tint.

I’m wondering now whether it MIGHT have been one of those occasional weird errors you see in Android Studio, when it swears up, down, left, right, and diagonally that there’s an error somewhere… but the project builds and runs anyway.

It’s been a while since I’ve really researched the matter, but I think I remember reading somewhere that the editor’s syntax-checking is loosely-bound to the build tools, and occasionally gets into a state where the pre-compilation/editor checks report a serious problem that the build tools themselves don’t actually regard as a big deal. I’m wondering whether this might be one of those cases… say, Google changed an annotation somewhere to mark it as deprecated (but still allowed), then something in ArcticFox misinterpreted something intended to be a mere warning as a serious error.

I had trouble with this as well. After changing to app:tint I got an exception. Issue on google: Google Issue Tracker. To fix this I had to add a function to the BindingAdapter:

/*
We need this one to handle "Data binding doesn't support app:tint [appcompat views]"
https://issuetracker.google.com/issues/152953070#comment8
*/
@BindingAdapter("tint")
fun ImageView.setImageTint(@ColorInt color: Int) {
    setColorFilter(color)
}