user2567369
user2567369

Reputation:

set bitmap to background of ImageView with imageView.setImageBitmap method

I have an ImageView. I am using imageView.setImageBitmap to set my image as background to ImageView. But it sets my image to ImageView's source (i.e. android:src) , but it doesn't set my image to ImageView's background (i.e. android:background).

When I use imageView.setImageBitmap, it looks like as if I used imageView.setImageResource not imageView.setBackgroundResource.

How can I handle this if I want to set my image to background using imageView.setImageBitmap. I know by I can do this by making custom ImageView. But is it possible without custom ImageView? If its possible, please let me know how to do it.

Upvotes: 39

Views: 95814

Answers (6)

Siddhpura Amit
Siddhpura Amit

Reputation: 15128

I have tested and it's done

I think you are getting Bitmap

So you have to convert Bitmap to BitmapDrawable

like

  BitmapDrawable ob = new BitmapDrawable(getResources(), bitmap)

then just set bitmap with below function

  imageView.setBackground(ob);

by this way you can do it..

Upvotes: 116

user2792274
user2792274

Reputation: 31

I prefer to use this because is not deprecated and it works in the lowers versions of android.

ImageView imageView;
Bitmap bitmap;
Drawable drawable = new BitmapDrawable(getResources(),bitmap);
imageView.setImageDrawable(drawable);

Upvotes: 3

Anil kumar
Anil kumar

Reputation: 1534

try this..

Create drawable using bitmap & use setBackgroundDrawable(Drawable d) method for imageview.

Drawable d = new BitmapDrawable(getResources(),bitmap);

imageview.setBackgroundDrawable(d);

Upvotes: 5

Chintan Rathod
Chintan Rathod

Reputation: 26034

Try following code to set your own bitmap as background.

Bitmap bitmap = ...;
ImageView imageView = ...;
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
imageView.setBackground(drawable);

Upvotes: 11

Praveen Sharma
Praveen Sharma

Reputation: 4348

Please Use the following line to set image background.

imageview.setBackground(getResources().getDrawable(R.drawable.uporder));

Here uporder is an image resource present in your drawablefolder.

Upvotes: 3

pskink
pskink

Reputation: 24750

call setBackgroundDrawable withe BitmapDrawable param

Upvotes: 1

Related Questions