I am currently trying to pass the resource id of a raw resource to a special View using the new Android data-binding mechanism. When I try something like this
...
app:bufferedSvg="@{ViewModel.headerCollapsed ? @raw/header_expand : @raw/header_collapse}"
...
I get an error <expr> expected, got '@'
. The raw resources exist and cannot be turned into another kind of resource as they contain SVG data.
Is this a bug of Android data binding or is this intended behaviour?
Data Binding Library Part of Android Jetpack. Stay organized with collections Save and categorize content based on your preferences. The Data Binding Library is a support library that allows you to bind UI components in your layouts to data sources in your app using a declarative format rather than programmatically.
Android Studio Supports Data BindingSyntax Highlighting. XML code completion. Flagging of expression language syntax errors.
ViewBinding vs DataBindingThe main advantages of viewbinding are speed and efficiency. It has a shorter build time because it avoids the overhead and performance issues associated with DataBinding due to annotation processors affecting DataBinding's build time.
At least I found a workaround meanwhile. Import the R class:
<data>
<import type="com.yourdomain.R"/>
<variable name="ViewModel" type="com.yourdomain.ViewModel"/>
</data>
...
<com.yourdomain.yourview
...
app:bufferedSvg="@{ViewModel.yourchoice ? R.raw.raw_resource1 : R.raw.raw_resource2}"
...
/>
Hope this helps someone who runs into the same problem. Would still like to know whether the @-notation for raw resources is planned to be supported or not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With