While using the new data binding api, I found that you can't bind to the "style" attribute. Compiler complains that it can't find the style. However, if I simply set the style as is, it'll find it just fine. For example:
doesn't work:
style="@{TextUtils.isEmpty(row.getSubtitle()) ? @style/SubTitle : @style/Title}"
works:
style="@style/SubTitle"
Error:
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
java.lang.RuntimeException: Found data binding errors. ****/ data binding error ****msg:Identifiers must have user defined types from the XML file. SubTitle is missing it file:/~/test/app/src/main/res/layout/row.xml loc:48:71 - 48:78 ****\ data binding error ****
The Data Binding Library is an Android Jetpack library that allows you to bind UI components in your XML layouts to data sources in your app using a declarative format rather than programmatically, reducing boilerplate code. This codelab has been designed for those with some Android development experience.
Data Binding Library Part of Android Jetpack. 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.
viewModel.priceStyleResource is a MutableLiveData in my view model which is set with the style resource ID. You can also probably make a generic bindStyle binding adapter directly for the View class, but in that case the attribute items specifically for textviews ( textColor for example) will not get applied.
<TextView android:text="@{viewmodel.userName}" />. Binding components in the layout file lets you remove many UI framework calls in your activities, making them simpler and easier to maintain. This can also improve your app's performance and help prevent memory leaks and null pointer exceptions.
The data binding unfortunately is not supported for styles: https://code.google.com/p/android-developer-preview/issues/detail?id=2613
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