18

In my code I have an Image View in my XML layout and I keep changing the source image for this in my code. Now I want to know the width and height of the generated image each time.

I tried using getWidth(), getHeight(), getMeasuredWidth(), and getMeasuredHeight(), but they all return 0.

How can I get this?

7 Answers 7

43

Where you calling getWidth() and getHeight() on ImageView? If you calling from onCreate() in activity, it won't work. You need to wait for activity window to attached and then call getWidth() and getHeight() on ImageView. You can try calling getWidth() and getHeight() from onWindowFocusChanged() method of your activity.

@Override public void onWindowFocusChanged(boolean hasFocus){ int width=imageView.getWidth(); int height=imageView.getHeight(); } 
Sign up to request clarification or add additional context in comments.

4 Comments

that returns zero the very first time that the image is loaded, and then the correct size every time after that..
How does one do this in a fragment?
onWindowFocusChanged just work on API18 and over, and it can't be best way... however thanks
I have set the imageview height and width to 300dp in xml but when i run your method i get the output as 450dp for height and width. Please Help
14

try this

 ImageView iv=(ImageView)findViewById(R.id.image); ViewTreeObserver vto = iv.getViewTreeObserver(); vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { public boolean onPreDraw() { finalHeight = iv.getMeasuredHeight(); finalWidth = iv.getMeasuredWidth(); Log.e("hilength","Height: " + finalHeight + " Width: " + finalWidth); return true; } }); 

1 Comment

thanks a lot. this code works too, but I guess the solution provided by Veer will be more appropriate.
5

if you set ImageView a drawable, and the ImageView's height and width type is WRAP_CONTENT, you can get the ImageView's height and width by this even you calling from onCreate()

int height = imageView.getDrawable().getIntrinsicHeight(); int width = imageView.getDrawable().getIntrinsicWidth(); 

1 Comment

This seems useless if you get the image from url.
4

try this code

int finalHeight, finalWidth; final ImageView iv = (ImageView)findViewById(R.id.scaled_image); final TextView tv = (TextView)findViewById(R.id.size_label); ViewTreeObserver vto = iv.getViewTreeObserver(); vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { public boolean onPreDraw() { iv.getViewTreeObserver().removeOnPreDrawListener(this); finalHeight = iv.getMeasuredHeight(); finalWidth = iv.getMeasuredWidth(); tv.setText("Height: " + finalHeight + " Width: " + finalWidth); return true; } }); 

it's really working...

Comments

1

The way you make reference to the image object is wrong. That's why you are given zero all the time. first create a bitmap object from imageview and get the width and height from it.

When taken a image from cam I do the following and it works like a charm.

Bitmap image2 = (Bitmap) data1.getExtras().get("data"); double width = Double.valueOf(image2.getWidth()); Log.v("WIDTH", String.valueOf(width)); double height = Double.valueOf(image2.getHeight()); Log.v("height", String.valueOf(height)); 

Hope it helps for you.

Comments

1

First you have to convert the image to mutablebitmap and the try to get the width and height of the image and mostly this will solve your issue..

 Bitmap Rbitmap = Bitmap.createBitmap(bitmap).copy( Config.ARGB_4444, true); Rbitmap.getWidth(); Rbitmap.getheight(); 

1 Comment

The question is how to get height and width of the image view not a bitmap.
1

Try this solution:

Bitmap bitmap = ((BitmapDrawable)imageView.getBackground()).getBitmap(); int width = bitmap.getWidth(); int height = bitmap.getHeight(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.