Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Android data-binding support raw resources?

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?

like image 917
Nantoka Avatar asked Feb 03 '16 17:02

Nantoka


People also ask

What is the benefit of Data Binding in Android?

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.

Which of the following is included in Data Binding?

Android Studio Supports Data BindingSyntax Highlighting. XML code completion. Flagging of expression language syntax errors.

Which is better view binding or Data Binding in Android?

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.


1 Answers

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.

like image 102
Nantoka Avatar answered Nov 14 '22 08:11

Nantoka