Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set background image with picasso in code

I know picasso loads image into imageview etc but how do I set my layout background image using picasso?

My code:

public class MainActivity extends ActionBarActivity {      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativelayout);         relativeLayout.setBackgroundResource(R.drawable.table_background);         Picasso.with(MainActivity.this)                 .load(R.drawable.table_background)                 .resize(200, 200)                 .into(relativeLayout);         return relativeLayout;     } 

What I have here gives any error saying it cannot be resolved. I have a ScrollView and relative layouts.

like image 654
Toye Avatar asked Apr 21 '15 15:04

Toye


People also ask

How do you load an image into an imageView from an image URL using Picasso?

Image loading using Picasso is very easy, you can do it like this way Picasso. get(). load("http://i.imgur.com/DvpvklR.png").into(imageView); and in their website you can get every details. In your case you can parse every image URL and use RecyclerView to show them along with Picasso.

Can I use Picasso image?

For using Picasso in the android project, we have to add a dependency in the app-level gradle file. So, For adding dependency open app/build. gradle file in the app folder in your Android project and add the following lines inside it.


1 Answers

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");   }       }) 

UPDATE:

Please check this also .As @OlivierH mentioned in the comment.

like image 128
Soham Avatar answered Sep 19 '22 16:09

Soham