Set bitmap background on ImageButton in Android

Set bitmap background on ImageButton in Android

android imagebutton
android imagebutton set image programmatically
android imagebutton scale image
android image button with text below
android imagebutton with text
how to add icon image button in android studio
android imagebutton background transparent
android change image button color programmatically

I want to set an Image background at run time on ImageButton. I know I can use the following code:

Bitmap b=bitmap;
ImageButton btn;

but the above code requires android API 16, and I am working on lower API. I also know the following code can be used:

ImageButton imgButton = (ImageButton) findViewById(;

but with this, I have a problem that the image that I am trying to set is not available in the resources and is generated at run time.

How can I set an image to ImageButton at run time in android with APIs before API level 16.


use setBackgroundDrawable(Drawable)...

    Bitmap b = bitmap;
    ImageButton btn;
    btn = (ImageButton) findViewById(;
    BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), b);

How to set bitmap image to Button Background Image, MainActivity" android:background="#d9e6eb" > <Button android:id="@+id/btn" Programmatically set the ImageView image using Bitmap  ImageButton image can be easily replaceable through programming file. So here is the complete step by step tutorial for Change ImageButton image programmatically android.

Try this..

btn = (ImageButton) findViewById(;
BitmapDrawable drawable_bitmap = new BitmapDrawable(getResources(), b);

like @Gopal Rao said


btn = (ImageButton) findViewById(;

ImageButton, Use an ImageButton, and call setImageBitmap() to set the foreground image. setBackgroundResource() is for the button background, which needs to be a  Following are some of the commonly used attributes related to ImageButton control in android applications. It is used to set the background color for an image button control. It is used to set the padding from left, right, top and bottom of the image button. It is used to set the offset of the baseline within the view.

public Bitmap getBitmapFromDrawable (@DrawableRes int resId){
    // create bitmap

    Drawable d =ContextCompat.getDrawable(getContext(),res);

    Bitmap bmp = Bitmap.createBitmap(d.getIntrinsicWidth(),
                    d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bmp);
    d.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());

    return bmp;

If you want to scale bitmap add this line to the method :

//with x and y as your new width and height
Bitmap newBitmap = Bitmap.createScaledBitmap (bmp,x,y,false);



How to set a bitmap to ImageView in Android, getId() returns set the valid drawable resource like ImageButton stuff = (​ImageButton) set bitmap to background of ImageView with imageView. In this tutorial we are changing image button width and height from programming file after application start. So it will completely dynamically method. So here is the complete step by step tutorial for Set/Change ImageButton image width height in android programmatically.

How can I set a bitmap on button?, ImageButton view = gestureImages[playerChoice]; BitmapDrawable drawable b> * <p> * The original wrapped background drawable can be obtained via {@​link onLongClick(View v) { // long press on center button sets track toggle mPref​. ImageView và ImageButton trong Android. Sử dụng ImageView để hiện thị các hình ảnh trong lập trình Android, ImageButton nút bấm hiện thị ảnh, ảnh được thu phóng phù hợp với mục đích sử dụng, nguồn ảnh Bitmap hay Drawable

why I can't change the transparency of a Drawable?, Background Image Placement. Bitmap bmp = BitmapFactory.decodeResource(​getResources(), R.drawable.image); BitmapDrawable bitmapDrawable = new  This example demonstrates how do I set background drawable programmatically in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.

android.widget.ImageButton.getBackground java code examples , Android: how to set the height/width of the src image of a ImageButton android Set bitmap background on ImageButton in Android - Stack Overflow. android  Create bitmap using external xml to set image inside bitmap drawable then add bitmap as relativelayout background. In this tutorial we are creating Bitmap using another layout xml file with Bitmap tag and inside Bitmap tag we are setting up background image src path.