Compiler error when applying theme with databinding
Asked Answered
J

1

0

My code shuffles colors and indexes for a list of ColorBox objects indefinitely.
This is my view:

<TextView
    style="@style/App.WidgetStyle.ColorBox"
    android:text="@{item.id}"
    android:theme="@{item.theme}"
    tools:text="A"
    tools:theme="@style/App.ColorBox" />

My styles:

<style name="App.WidgetStyle.ColorBox" parent="App">
    <item name="android:layout_width">@dimen/square_size</item>
    <item name="android:layout_height">@dimen/square_size</item>
    <item name="android:background">@drawable/shape_for_rounded_outlined_bg</item>
    <item name="android:fontFamily">@font/open_sans_bold</item>
    <item name="android:gravity">center</item>
</style>

My custom background shape:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners
        android:radius="?attr/backgroundShapeCornerRadius" />
    <solid android:color="?attr/colorPrimary"/>
    <stroke android:width="1dp" android:color="?attr/colorSecondary"/>
</shape>

My themes:

<style name="App.ColorBox">
    <item name="colorPrimary">@android:color/transparent</item>
    <item name="colorSecondary">@color/black</item>
    <item name="backgroundShapeCornerRadius">0dp</item>
</style>
<style name="App.ColorBox.Red">
    <item name="colorPrimary">@color/color_1</item>
</style>
<style name="App.ColorBox.Green">
    <item name="colorPrimary">@color/color_2</item>
</style>
<style name="App.ColorBox.White">
    <item name="colorPrimary">@color/color_3</item>
</style>
<style name="App.ColorBox.Blue">
    <item name="colorPrimary">@color/color_4</item>
</style>

My data class:

@Parcelize
data class ColorBox(var id: String, @StyleRes var theme: Int) : Parcelable

And if I try to compile, the compiler hates it:

Task :app:kaptDevDebugKotlin ANTLR Tool version 4.5.3 used for code generation does not match the current runtime version 4.7.1ANTLR Runtime version 4.5.3 used for parser compilation does not match the current runtime version 4.7.1ANTLR Tool version 4.5.3 used for code generation does not match the current runtime version 4.7.1ANTLR Runtime version 4.5.3 used for parser compilation does not match the current runtime version 4.7.1/Users/.../DataBinderMapperImpl.java:10: error: cannot find symbol import com....RowForItemBindingImpl; ^ symbol: class RowForItemBindingImpl

Task :app:kaptDevDebugKotlin FAILED location: package com....databinding FAILURE: Build failed with an exception.

  • What went wrong: Execution failed for task ':app:kaptDevDebugKotlin'.

    A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution java.lang.reflect.InvocationTargetException (no error message)

But if I create a BindingAdapter (for instance this didn't work)

object AppBindingAdapters {

    @JvmStatic
    @BindingAdapter(value = ["colorBoxTheme"])
    fun colorBoxTheme(view: View, @StyleRes themeResId: Int) {
        view.background = ResourcesProvider(view.context).drawable(R.drawable.shape_for_rounded_outlined_bg, themeResId)
    }
}

<TextView
    style="@style/App.WidgetStyle.ColorBox"
    android:text="@{item.id}"
    app:colorBoxTheme="@{item.theme}"
    tools:text="A"
    tools:theme="@style/App.ColorBox.Green" />

It works :)

enter image description here

Is this a databinding bug or the desired behaviour? Why can't I apply a theme dynamically with databinding without the BindingAdapter "hack"?

Btw, ResourcesProvider it's a very handy helper class to provide resources.

Junia answered 28/3, 2020 at 21:13 Comment(1)
Yep. It is a databinding thing.Gnostic
J
0

[email protected] #3Apr 30, 2020 16:45 Status: Won't Fix (Infeasible) 16:45 +CC:​[email protected] 16:45

you cannot apply themes via data binding unfortunately. they are only ever read on inflation time and useless afterwards (we don't have a working setStheme function on views). nothing we can do on the data binding side unfortunately.

https://issuetracker.google.com/issues/152712592

Junia answered 30/4, 2020 at 15:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.