Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load image with data binding

I would like to set the image using the Android Data Binding. I have read a lot of tutorials about this and the images still don't appear.

Inside my model I have the following method:

@BindingAdapter({"app:imageUrl"})
    public static void loadImage(ImageView view, String url) {

        Log.i("III", "Image - " + url);
        Context context = view.getContext();
        Glide.with(context).load(GlobalValues.img_start_url + url).into(view);
    }

I also should mention that "III", "Image - " + url is never print so the problem is not with the Glide.

This is my xml:

 <data>

    <variable
        name="feeling"
        type="bg.web3.helpkarma.ViewModels.Feeling"/>

</data>

<ImageView
            android:layout_width="wrap_content"
            android:id="@+id/icon"
            android:layout_height="wrap_content"
            android:layout_marginRight="16dp"
            app:imageUrl="@{feeling.maleIcon}"/>

and the maleIcon is a url String

@SerializedName("male_icon")
@Expose
private String maleIcon;
like image 471
charbinary Avatar asked Oct 17 '22 22:10

charbinary


2 Answers

Here are the steps: 1. Declare this method in model class, in annotation @BindAdapter("{bind:should be same variable here in which we are getting imageurl"})

  @BindingAdapter({"bind:imageUrl"})
public static void loadImage(ImageView view, String imageUrl) {
    Picasso.with(view.getContext())
            .load(imageUrl)
            .placeholder(R.drawable.placeholder)
            .into(view);}

2.Go to xml file of adapter and add app:imageUrl="@{user.imageUrl}". In this line both property app:imageurl and user.imageUrl should be same parameter in which we are getting the url of image in bean class. also we have defined the same infirst step.

 <ImageView
            android:id="@+id/restaurantIV"
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            android:scaleType="fitXY"
            app:image="@{user.image}"
            tools:ignore="ContentDescription"/>

3.)Dont forget to add,Datum is name of Bean class user name of object `

    <variable
        name="user"
        type="com.brst.cocktailclub.beans.restaurantSearch.Datum"/>
</data>`

4.) Also add , xmlns:app="http://schemas.android.com/apk/res-auto" in layout parameter.

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

5.)Remember your Bean class should extend BaseObservable.

6.)Finally set the data in adapter

 public void onBindViewHolder(@NonNull RestaurantAdapter.ViewHolder viewHolder, int i) {

    Datum datum=data.get(i);

    viewHolder.customRestaurantListBinding.setUser(datum);


}
like image 50
jatin kumar Avatar answered Oct 21 '22 01:10

jatin kumar


Android DataBinding @BindingAdapter only supports the android namespace or none. So replace the definition with

@BindingAdapter("imageUrl")
public static void loadImage(ImageView view, String url) {
    [...]
}

Additionally loadImage() will never be called if the feeling.maleIcon is null.

like image 30
tynn Avatar answered Oct 21 '22 00:10

tynn