codelab-android-databinding
codelab-android-databinding copied to clipboard
Confusing bug with @BindingAdapter requireAll=true
The example defines the binding adapter:
@BindingAdapter(value = ["app:progressScaled", "android:max"], requireAll = true)
fun setProgress(progressBar: ProgressBar, likes: Int, max: Int) {
progressBar.progress = (likes * max / 5).coerceAtMost(max)
}
If you try to use it like so:
<ProgressBar
android:id="@+id/progressBar"
tools:progressBackgroundTint="@android:color/darker_gray"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/like_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@+id/like_button"
app:layout_constraintEnd_toEndOf="@+id/like_button"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintHorizontal_bias="0.5"
app:hideIfZero="@{viewmodel.likes}"
app:progressScaled="@{viewmodel.likes}"
app:progressTint="@{viewmodel.popularity}"
/>
you get the following from a clean build:
.../android-databinding/app/build/generated/source/kapt/debug/com/example/android/databinding/basicsample/DataBinderMapperImpl.java:9: error: cannot find symbol
import com.example.android.databinding.basicsample.databinding.PlainActivityBindingImpl;
^
symbol: class PlainActivityBindingImpl
location: package com.example.android.databinding.basicsample.databinding
Quick, what it the problem? The required attribute android:max is not present! In contrast if you include android:max by leave off app:progressScaled the build succeeds and the binding is simply not applied.
I only changed the project by updated dependencies (including gradle) to current versions.
Add attribute android:max="@{100}" to Progressbar tag, Data Binding won't be able to find the correct Binding Adapter if you don't use @{} to the value