How to set bitmap image to Button Background Image
Asked Answered
M

4

5
gridcell = (Button) row.findViewById(R.id.calendar_day_gridcell);    
gridcell.setText("Day 1");    
URL url = new URL("http://172.16.4.29:81/pht/2013/9/18/3027_2013_9_18_12_14_56_b.JPG");
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());

How can I set bitmap bmp image to button gridcells background Image?

Michel answered 23/9, 2013 at 6:11 Comment(2)
ain't any answer you get helpful? If you found solution please put yours..Csc
Check out Android ImageButton Example.Brooklynese
C
16

You can use following code to do that..

BitmapDrawable bdrawable = new BitmapDrawable(context.getResources(),bitmap);

then just set bitmap with below function

button.setBackground(bdrawable);
Csc answered 23/9, 2013 at 7:37 Comment(3)
BitmapDrawable(bitmap) is deprecated.Nombril
ImageButton#setBackgroundDrawable(Drawable) is deprecated as well. The answer by Sruit A.Suk is better.Adrianadriana
@Adrianadriana thanks for your update.. answer edited as per requirement. :)Csc
C
3

you need to check current version of Android also

Button bProfile; // your Button
Bitmap bitmap; // your bitmap

if(android.os.Build.VERSION.SDK_INT < 16) {
    bProfile.setBackgroundDrawable(new BitmapDrawable(getResources(), bitmap));
}
else {
    bProfile.setBackground(new BitmapDrawable(getResources(),bitmap));
}
Cutting answered 29/5, 2015 at 6:38 Comment(0)
B
0

I always and will always recommended you strongly using Image Button.

http://developer.android.com/reference/android/widget/ImageButton.html

I hope that helps you, Shaun.

Beleaguer answered 23/9, 2013 at 6:16 Comment(0)
N
0

Hey First learn about Handling bitmaps: http://developer.android.com/training/displaying-bitmaps/process-bitmap.html

You should not process bitmaps on the UI thread at all. Once you have attained Bitmap use ImageView's method setImageBitmap(Bitmap bitmap).

To fetch images through network, You can also use libraries like picasso, Universal Image Loader, Volley to attain what you are wanting.

Narthex answered 23/9, 2013 at 6:16 Comment(1)
he was talking about Button background, not ImageView...Functional

© 2022 - 2024 — McMap. All rights reserved.