DataBinding: How to get resource by dynamic id?

Another solution is to create a custom @BindingAdapter for it.

@BindingAdapter({"format", "argId"})
public static void setFormattedText(TextView textView, String format, int argId){
    if(argId == 0) return;
    textView.setText(String.format(format, textView.getResources().getString(argId)));
}

And then just provide the variables separately.

<TextView
    app:format="@{@string/myFormatString}"
    app:argId="@{myPojo.resourceId}"

You could use an array if you need multiple arguments, but in my case, one was sufficient.

Leave a Comment