AndroidPub
Published in

AndroidPub

Loading images with data binding and Picasso

Are you trying the new Android data binding library, and you don’t know how to load images into ImageViews? Here are two examples of how to get those images showing!

Option 1

Using @BindingAdapter

This first solution uses the BindingAdapter annotation to create a custom xml attribute. When this attribute is set in the layout file with the right type, the data binding framework will trigger the execution of the annotated method. I called this custom attribute imageUrl and the parameter type is String. Bear in mind that the annotated method must be static.

Option 2

ObservableField & custom Picasso Target

This second approach requires more code but it’s still as valid as the first one. The idea here is to use an ObservableField with type Drawable and a custom Picasso Target. An ObservableField wraps any type of object, in this case a Drawable, and automatically refreshes the layout when its value changes.

--

--

The (retired) Pub(lication) for Android & Tech, focused on Development

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store