Search code examples
androidimagebutton

Android Get dimensions of Image in ImageButton


Is there a way to get the dimensions of the image currently set in the ImageButton? I'm trying to achieve this.

I have a ImageButton with a default pic of 36 x 36. I then select an image of size say 200 x 200. I wanna call something like:

imageButton.setImageBitmap(Bitmap.createScaledBitmap(
                        bitmap, 36, 36, true));

to shrink the image to 36 x 36. Reason why I want to get the original image size is to cater for hdpi, mdpi and ldpi so I can set dimensions of the bitmap to 36 x 36, 24 x 24 and 18 x 18 respectively before adding it to the ImageButton. Any ideas?

Oh man, I got the answer after randomly fiddling with the code:

imageButton.getDrawable().getBounds().height();    
imageButton.getDrawable().getBounds().width();

Solution

  • Try this code -

    imageButton.getDrawable().getBounds().height();    
    imageButton.getDrawable().getBounds().width();