Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting bitmap to drawable in Android

Tags:

android

I'm downloading the image from server and storing it in bitmap object. I want to set this image as background for button. But the button doesn't have the property setImageBitmap. So is there anyway I can set the background of button with the downloaded bitmap image? Like by converting the bitmap to drawable? Sorry, I'm new to Android, please bear with my mistakes (if any).

P.S : I want to use button control only. Because I want some text at the bottom of each buttons and I'm creating these buttons dynamically.

like image 920
user3522370 Avatar asked Apr 16 '14 10:04

user3522370


2 Answers

The best way to convert a Bitmap to drawable in android is as follows,

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

where bitmap is the name the Bitmap. Then set the drawable as your Button background as follows,

btn.setBackground(drawable);

N.B: Without specifying getResources() as the first argument, you may experience inconsistent image sizing across different screen densities.

for more info refer this http://developer.android.com/reference/android/graphics/drawable/BitmapDrawable.html

like image 170
Spring Breaker Avatar answered Oct 01 '22 10:10

Spring Breaker


Simply use BitmapDrawable.

Drawable drawable=new BitmapDrawable(contact_pic); 
like image 21
Amresh Avatar answered Oct 01 '22 12:10

Amresh