Set Background Image to Relative Layout using Glide in Android

You can load an image in a RelativeLayout like this. I’m just showing you the hard part which is setting an image to the background.

For Glide version before 4.X

Glide.with(this).load(imageViewPath).asBitmap().into(new SimpleTarget<Bitmap>(relLayoutWidth, relLayoutHeight) {
    @Override
    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
        Drawable drawable = new BitmapDrawable(context.getResources(), resource);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            yourRelativeLayout.setBackground(drawable);
        }
    }
});

For caching, refer to this page: Caching and Cache Invalidation.

Update for Glide v4 onward:

GlideApp.with(this).load(R.drawable.backgroundimage).into(new SimpleTarget<Drawable>() {
            @Override
            public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    yourRelativeLayout.setBackground(resource);
                }
            }
        });

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)