Step 1 - Enable DataBinding in your gradle file.

  1. android {
  2. ...
  3. ...
  4. dataBinding {
  5. enabled = true
  6. }

Step 2 - Define POJO/Model class

  1. public class PersonModelClass
  2. {
  3. String name,designation,image_url;
  4. public PersonModelClass(String name, String designation, String image_url) {
  5. this.name = name;
  6. this.designation = designation;
  7. this.image_url = image_url;
  8. }
  9. public String getName() {
  10. return name;
  11. }
  12. public void setName(String name) {
  13. this.name = name;
  14. }
  15. public String getDesignation() {
  16. return designation;
  17. }
  18. public void setDesignation(String designation) {
  19. this.designation = designation;
  20. }
  21. public String getImage_url() {
  22. return image_url;
  23. }
  24. public void setImage_url(String image_url) {
  25. this.image_url = image_url;
  26. }
  27. }

Step 3 - Define custom binding class.

  1. public class CustomBindingAdapter
  2. {
  3. @BindingAdapter({"bind:image_url"})
  4. public static void loadImage(ImageView imageView,String url)
  5. {
  6. Picasso.with(imageView.getContext()).load(url).resize(200,200).into(imageView);
  7. }
  8. }

Here I have used Picasso for image loading, you can replace this code with your own image loader like Glide, UniversaImageLoader etc.

Step 4

Create layout file and add the new image_url attribute to your ImageView. Custom attributes need to use the app namespace instead of android.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <layout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto">
  4. <data>
  5. <variable
  6. name="person"
  7. type="<package name>.PersonModelClass"></variable>
  8. </data>
  9. <LinearLayout
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent"
  12. android:gravity="center"
  13. android:orientation="horizontal">
  14. <ImageView
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. app:image_url="@{person.image_url}" />
  18. <LinearLayout
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:layout_marginLeft="10dp"
  22. android:orientation="vertical">
  23. <TextView
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:text="@{person.name}"
  27. android:textAppearance="?android:attr/textAppearanceLarge" />
  28. <TextView
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:text="@{person.designation}" />
  32. </LinearLayout>
  33. </LinearLayout>
  34. </layout>

Step 5 - Finally in your activity class set view with DataBindingUtils.

  1. public class MainActivity extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. ActivityMainBinding binding= DataBindingUtil.setContentView(this,R.layout.activity_main);
  6. PersonModelClass model=new PersonModelClass("Ravi Rupareliya","Android Developer","<image url>");
  7. binding.setPerson(model);
  8. }
  9. }