How to set a bitmap to a button background image

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 the bitmap bmp image to the background image of the buttons of the cell cell?

+4
source share
4 answers

You can use the following code to do this.

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

then just set the bitmap with the function below

 button.setBackground(bdrawable); 
+12
source

You also need to check the current version of Android.

 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)); } 
+2
source

I always and always recommend you strongly use the "Image" button.

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

Hope this helps you, Sean.

0
source

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

You should not process bitmaps in the user interface stream at all. When you have reached the bitmap, use the ImageView method setImageBitmap (bitmap bitmap).

To get images via the network, you can also use libraries such as picasso, Universal Image Loader, Volley to achieve what you want.

0
source

All Articles