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?

Arun Bertil
  • 4,598
  • 4
  • 33
  • 59
Viresh
  • 323
  • 2
  • 5
  • 17

4 Answers4

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);
Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
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));
}
Sruit A.Suk
  • 7,073
  • 7
  • 61
  • 71
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.

Rajeev Ranjan
  • 4,152
  • 3
  • 28
  • 41
Shaun
  • 147
  • 1
  • 2
  • 16
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.

Gaurav Vashisth
  • 7,547
  • 8
  • 35
  • 56