John Ranniel Sinel John Ranniel Sinel - 5 months ago 134
Android Question

How to set the imageview as the background image of my LinearLayout from Picasso image loader programmatically?

I'm using

Picasso
class/library to load an image from an
url
and display the image to a
ImageView
. Is it possible for me to set the
imageview
loaded by the picasso image loader from an
url
as the background image of a
linearlayout
programmatically?

Answer

I've already found this issue - might be useful for you:

How do i set background image with picasso in code

According to that, Use callback of Picasso

Picasso.with(getActivity()).load(R.drawable.table_background).into(new Target(){

    @Override
    public void onBitmapLoaded(Bitmap bitmap, LoadedFrom from) {
       mainLayout.setBackground(new BitmapDrawable(context.getResources(), bitmap));
    }

    @Override
    public void onBitmapFailed(final Drawable errorDrawable) {
        Log.d("TAG", "FAILED");
    }

    @Override
    public void onPrepareLoad(final Drawable placeHolderDrawable) {
        Log.d("TAG", "Prepare Load");
    }      
})

Read also

Set background resource using Picasso

but there would you find the same solution.