Updated 17 August 2017
Using Picasso in your app for downloading images into ImageView is quite common these days. But can we download an image and set it as background at run time using Picasso?
The answer to this question is ‘ yes ‘, but of course it will require some extra piece of coding.
So, let’s have a look at this extra piece of code.
1 |
Picasso.with(context).load(url).into(imageView); |
Of- course you can add a whole lot of options to this image like resize(), fit(), placeholder() as per your needs.
But the steps for loading an image into background goes like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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"); } }) |
And, it’s done.
P.S: you can always use any of the other options with this request and you are open to replacing the drawable with URL of your image.
Keep Coding, Keep innovating and Keep sharing. : )
References:
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.