Set drawable resource ID in android:src for ImageView using data binding in Android

后端 未结 15 2007
陌清茗
陌清茗 2020-12-04 09:27

I\'m trying to set drawable resource ID to android:src of ImageView using data binding

Here is my object:

public class Recipe implem         


        
15条回答
  •  一整个雨季
    2020-12-04 10:21

    Building upon the answer from Maher Abuthraa, this is what I ended up using in the XML:

    android:src="@{context.getDrawable(recipe.imageResource)}"
    

    The context variable is available in binding expression without any imports. Also, no custom BindingAdapter necessary. Only caveat: the method getDrawable is only available since API 21.

提交回复
热议问题