I need to add a background image to a ListView. Normally I would call listview.setBackground(myImage). But the image is coming from server and so I need to use Picasso to load the image into the background of my ListView. How do I do that?
            Asked
            
        
        
            Active
            
        
            Viewed 2,596 times
        
    5
            
            
         
    
    
        learner
        
- 11,490
- 26
- 97
- 169
1 Answers
11
            Option One
Define an anonymous subclass of com.squareup.picasso.Target
Picasso.with(yourContext)
      .load(yourImageUri)
      .into(new Target() {
        @Override
        @TargetApi(16)
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            int sdk = android.os.Build.VERSION.SDK_INT;
            if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
                yourListView.setBackgroundDrawable(new BitmapDrawable(bitmap));
            } else {
                yourListView.setBackground(new BitmapDrawable(getResources(), bitmap));
            }
        }
        @Override
        public void onBitmapFailed(Drawable errorDrawable) {
            // use error drawable if desired
        }
        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {
            // use placeholder drawable if desired
        }
    });
Option Two
Subclass ListView and implement com.squareup.picasso.Target
public class PicassoListView extends ListView implements Target {
    public PicassoListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public PicassoListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    @Override
    @TargetApi(16)
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        int sdk = android.os.Build.VERSION.SDK_INT;
        if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
            setBackgroundDrawable(new BitmapDrawable(bitmap));
        } else {
            setBackground(new BitmapDrawable(getResources(), bitmap));
        }
    }
    @Override
    public void onBitmapFailed(Drawable errorDrawable) {
        // use error drawable if desired
    }
    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {
        // use placeholder drawable if desired
    }
}
Which lets you do this:
Picasso.with(yourContext)
          .load(yourImageUri)
          .into(yourListView);
 
    
    
        bmat
        
- 2,084
- 18
- 24
- 
                    This deserves a check and a +1. Thank you. – learner Oct 06 '14 at 16:00
- 
                    If I call resize between load and into then `onBitmapLoaded` is never called. Is there a way around this problem? I would like to be able to resize. – learner Oct 06 '14 at 17:16
- 
                    1@learner That's odd. `onBitmapLoaded` is called for me in both examples when I call `resize`. I looked into the problem and found [this SO question](http://stackoverflow.com/questions/24180805/onbitmaploaded-of-target-object-not-called-on-first-load) which seems relevant. Hope this helps – bmat Oct 06 '14 at 20:59