Hot questions for Using Glide in data binding

Top 10 Java Open Source / Glide / data binding

Question:

I am trying to load an image using glide but somehow I can not load the image using glide. As it shows following error:

Failed to find GeneratedAppGlideModule. You should include an annotationProcessor compile dependency on com.github.bumptech.glide:compiler in your application and a @GlideModule annotated AppGlideModule implementation or LibraryGlideModules will be silently ignored.

I have reffered This solution too. But, I already have the updated verion of glide.

In my gradle, I have added

implementation 'com.github.bumptech.glide:glide:4.7.1'

and

annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'

Code

XML

<?xml version="1.0" encoding="utf-8"?>

<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"
    tools:context=".view.SettingActivity">

    <data>
        <variable
            name="settingsViewModel"
            type="com.sevenbits.android.mvvmsample.viewmodel.SettingsViewModel"/>

    </data>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/splash_bg">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:id="@+id/profile"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="20dp"
                android:background="@color/white"
                android:elevation="10dp"
                android:orientation="vertical"
                android:padding="5dp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <de.hdodenhof.circleimageview.CircleImageView
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_gravity="center"
                    android:layout_margin="10dp"
                    app:image_url="@{settingsViewModel.imageUrl}"
                    app:civ_border_width="2dp"
                    app:civ_border_color="@color/colorPrimary"/>

                  ...
            </LinearLayout>

    </android.support.constraint.ConstraintLayout>

</ScrollView>

CustomBindingAdapter

public class CustomBindingAdapter {

@BindingAdapter({"bind:image_url"})
public static void loadImage(ImageView imageView, String url) {

    RequestOptions requestOptions = new RequestOptions();
    requestOptions=requestOptions.placeholder(R.drawable.boy_32);

        Glide.with(imageView.getContext())
                .load(url)
                .apply(requestOptions)
                .into(imageView);
}

Answer:

Finally, I found my answer here.

What I have done :

Step-1

I created an empty class named GlideApp

import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.module.AppGlideModule;

@GlideModule
public class GlideApp extends AppGlideModule {

}

Note: Don't forget to add annotation @GlideModule.

Step-2 After that, I build/rebuild the project and then, replaced Glide with GlideApp.and now no need to use RequestOptions.

public class CustomBindingAdapter {

    @BindingAdapter({"bind:image_url"})
    public static void loadImage(ImageView imageView, String url) {

//        RequestOptions requestOptions = new RequestOptions();
//        requestOptions=requestOptions.placeholder(R.drawable.boy_32);

        GlideApp.with(imageView.getContext())
                .load(url)
                .placeholder(R.drawable.boy_32)
                .into(imageView);

//            Glide.with(imageView.getContext())
//                    .load(url)
//                    .apply(requestOptions)
//                    .into(imageView);
    }
}

Edit: For androidx and Glide versin 4.9.0:

In my app's gradle.build:

implementation ("com.github.bumptech.glide:glide:4.9.0") {
    exclude group: "com.android.support"
}
annotationProcessor 'androidx.annotation:annotation:1.0.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
implementation ("com.github.bumptech.glide:glide:4.9.0@aar") {
    transitive = true
}

In my gradle.properties:

android.enableJetifier=true
android.useAndroidX=true

That's all.

Question:

I am setting the background of fragment with data binding in android. Since the background can change dynamically i am setting it in xml like this:

app:image="@{variable.getBackgroundImage()}"

The getBackgroundImage() will give the path of the background. I am using glide to set the background like this:

Glide.with(view.getContext()).load(url + File.separator + path)
                    .dontAnimate()
                    .into(new SimpleTarget<GlideDrawable>() {
                        @Override
                        public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                            view.setBackground(resource);
                        }
                    });

The image is getting set but there is a momentary flickering of the pre-set background in the xml.

How can i avoid this flicker of the background?


Answer:

I solved the issue.The issue was with glide. I am now setting the background without glide and its working fine.No flicker.

BitmapDrawable bd = new BitmapDrawable(view.getContext().getResources(), BitmapFactory.decodeFile(url + File.separator + path));
            view.setBackground(bd);