15

Can anyone point me towards an example of how one might change an XML layout's background programatically using Picasso? All the examples I've found are able to update an ImageView using Picasso - but not a layout background.

Unable to set LinearLayout BackgroundResource from URL using Picasso

Community
  • 1
  • 1
  • Check [this answer](https://stackoverflow.com/a/51222667/2425851) to see how to create custom transformation for picasso – NickUnuchek Jul 07 '18 at 13:15

6 Answers6

37

You can use Picasso's Target:

         Picasso.with(this).load("http://imageUrl").into(new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
               mYourLayout.setBackground(new BitmapDrawable(bitmap));
            }

            @Override
            public void onBitmapFailed(Drawable errorDrawable) {

            }

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {

            }
        });

UPDATE

As @BladeCoder mentioned in the comment, Picasso holds a weak reference to Target objects, hence it is likely to be garbage collected.

So, following Jake Wharton's comment on one of the issues, i think this could be a good way to go:

  CustomLayout mCustomLayout = (CustomLayout)findViewById(R.id.custom_layout)
  Picasso.with(this).load("http://imageUrl").into(mCustomLayout);

CustomLayout.java:

public class CustomLayout extends LinearLayout implements Target {

    public CustomLayout(Context context) {
        super(context);
    }

    public CustomLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


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

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {
        //Set your error drawable
    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {
        //Set your placeholder
    }
}
mmark
  • 1,204
  • 12
  • 19
  • 2
    The code of this example may not always work: you *must* keep a hard reference to your `Target` or it may be garbage collected before the image is loaded because Picasso keeps weak references to Targets. – BladeCoder Jul 17 '15 at 00:47
  • 1
    @BladeCoder You're right. I'm editing my answer in order to address that issue. Thank you – mmark Jul 17 '15 at 14:58
  • I'm having a bit of trouble using Picasso's Target as you have shown above... (any assistance is appreciated) http://pastebin.com/uY2LnSDV – AndroidAndroidAndroid Jul 17 '15 at 16:16
  • @NoobDev910 1) Are you using the las version of Picasso (2.5.2)? 2)In the snippet you don't seem to be closing the brackets for the target object – mmark Jul 17 '15 at 17:55
  • @GokulNathKP could you provide context? – mmark Aug 11 '18 at 19:24
9

I use an ImageView as temporary ImageHolder. At first, load image with picasso into ImageView and set Layout Background from this ImageView by using getDrawable.

               ImageView img = new ImageView(this);
               Picasso.with(this)
              .load(imageUri)
              .fit()
              .centerCrop()
              .into(img, new Callback() {
                        @Override
                        public void onSuccess() {

                            myLayout.setBackgroundDrawable(img.getDrawable());
                        }

                        @Override
                        public void onError() {

                        }
                    });
Thiha
  • 175
  • 1
  • 3
4

None of the solutions above worked for me. But @Thiha's solution was the closest. The below worked for me:

final ImageView img = new ImageView(this);
    Picasso.with(img.getContext()).load(url).into(img, new com.squareup.picasso.Callback() {
        @Override
        public void onSuccess() {
            collapsingToolbarLayout.setBackgroundDrawable(img.getDrawable());
        }

        @Override
        public void onError() {
        }
    });
Mayur Karmur
  • 2,119
  • 14
  • 35
Arash Fotouhi
  • 1,933
  • 2
  • 22
  • 43
0

In my case i needed the image to fit the imageview's size so instead of loading the image in background i added this property to the imageview and loaded the image normally.

android:scaleType="fitXY"
henok
  • 864
  • 5
  • 12
0

As an alternative to previous answers. It's possible to set background via Piccasso simply by creating ImageView with match parent on the layout.

XML:

<ImageView
    android:id="@+id/imageView_background"
    android:layout_width="match_parent"
    android:contentDescription="@string/background"
    android:layout_height="match_parent"
    android:alpha="0.7"/>

Kt:

Picasso.get().load(R.drawable.background_green_forest).fit().centerCrop().into(view.imageView_background)
Krzysiulele
  • 346
  • 5
  • 11
0

You can try this, it works fine for me.

//ihave used releative layout in my activity

private RelativeLayout chatBG;

//initialize your layout

chatBG = findViewById(R.id.chat_activity);

//using picasso library, its better to use it on onStart() method to avoid some bugs

Picasso.get().load("https://your/url")
            .placeholder(R.drawable.yourplaceholder_img)
            .into(new Target() {
                @Override
                public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                    chatBG.setBackground(new BitmapDrawable(bitmap));
                }

                @Override
                public void onBitmapFailed(Exception e, Drawable errorDrawable) {
                    Toast.makeText(ChatActivity.this, "Error : loading wallpaper", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onPrepareLoad(Drawable placeHolderDrawable) {

                }
            });
micoder
  • 1
  • 2