Data binding in Android currently seems to support the following reference resources (according to data binding guide): @array
, @color
, @int
, @dimen
, @string
... which will give referenced values as arguments in static @BindingAdapter
method.
For example:
layout/web_view.xml
<WebView
app:htmlTextColor="@{@color/colorText}"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Bindings.java
@BindingAdapter({"bind:htmlTextColor"})
public static void setHtml(WebView webView, int textColor) {
// binding logic
}
But with themes and styles, it's more often that I use an attribute resource, e.g. ?android:attr/textColorPrimary
than a @color
reference. For such cases, how would the binding "@{}"
syntax look like? Currently this is how I make it work, but maybe there is a better way?
layout/web_view.xml
<WebView
app:htmlTextColor="@{android.R.attr.textColorPrimary}"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Bindings.java
@BindingAdapter({"bind:htmlTextColor"})
public static void setHtml(WebView webView, int textColorAttr) {
// binding logic
}