Panagiotis Lymperopoulos Panagiotis Lymperopoulos - 19 days ago 9
Android Question

Can I set the Background of RelativeLayout to something different than a drawable in Android Studio?

I am trying to change the background of my RelativeLayout in Android Studio using the .setBackground method. But it only allows to put as a value a drawable resource. The thing is that I use picasso for downloading pictures from imgur and loading them into an imageview and I want to use one of them as a background for my RelativeLayout. I have searched everywhere online but I can not find anything. Is it possible to set the background of RelativeLayout to the image content of an imageview which is not Drawable?

Answer

Use Target to set the bitmap in RelativeLayout

Picasso.with(mContext).
       load(url).fit().
       into(new Target() {
                @Override
                public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                    relativeLayout.setBackground(new BitmapDrawable(mContext.getResources(), bitmap));
                }

                @Override
                public void onBitmapFailed(Drawable errorDrawable) {

                }

                @Override
                public void onPrepareLoad(Drawable placeHolderDrawable) {

                }
            });
Comments