How to get the width and height of an android.widget.ImageView?
Solution 1
My answer on this question might help you:
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;
}
});
You can then add your image scaling work from within the onPreDraw() method.
Solution 2
I just set this property and now Android OS is taking care of every thing.
android:adjustViewBounds="true"
Use this in your layout.xml where you have planted your ImageView :D
Solution 3
I could get image width and height by its drawable;
int width = imgView.getDrawable().getIntrinsicWidth();
int height = imgView.getDrawable().getIntrinsicHeight();
Solution 4
Post to the UI thread works for me.
final ImageView iv = (ImageView)findViewById(R.id.scaled_image);
iv.post(new Runnable() {
@Override
public void run() {
int width = iv.getMeasuredWidth();
int height = iv.getMeasuredHeight();
}
});
Solution 5
The reason the ImageView's dimentions are 0 is because when you are querying them, the view still haven't performed the layout and measure steps. You only told the view how it would "behave" in the layout, but it still didn't calculated where to put each view.
How do you decide the size to give to the image view? Can't you simply use one of the scaling options natively implemented?
Related videos on Youtube
AZ_
I am working in an IT industry as a software engineer. I am user friendly, cooperative, democratic, witty and open source ;-)
Updated on June 07, 2020Comments
-
AZ_ almost 4 years
╔══════════════════════════════════════════════╗ ^ ║ ImageView ╔══════════════╗ ║ | ║ ║ ║ ║ | ║ ║ Actual image ║ ║ | ║ ║ ║ ║ |60px height of ImageView ║ ║ ║ ║ | ║ ║ ║ ║ | ║ ╚══════════════╝ ║ | ╚══════════════════════════════════════════════╝ <------------------------------------------------> 90px width of ImageView
I have an image view with some default height and width, images are stored in db and I want to scale Image according to Imageview height width. As I don't want it give default values because when ever I change it's height and width I also have to change it in code.
I am trying to get the height and width of ImageView but 0 is returned to me in both cases.
int height = ((ImageView) v.findViewById(R.id.img_ItemView)).getHeight();
this returns me 0 even it has default height and width
-
Pedro Loureiro over 13 years«As I don't want t give default values because when ever I change its height and width I also have to change it in code» - which one can change? the image view or the images stored?
-
bigstones over 13 yearscan't you just use
android:scaleType="centerInside"
? -
GregNash almost 13 yearsI was having a similar problem, but I found that the answers posted here didn't help me. I posted my own question, which was answered [here][1]. [1]:stackoverflow.com/questions/6590031/…
-
Rich over 10 years@nightcracker: That's not ASCII.
-
orlp over 10 years@Јοеу When I said "ASCII" I meant "ASCII art", which encompasses more art than that consisting of merely the ASCII character set.
-
audiojared almost 5 yearsThis doesn't directly answer the question, but if you're wondering why getHeight()/getWidth() are returning 0, they will always return 0 if you call those methods before the view has been "drawn" i.e if you were to call these methods in onCreate() it would return 0
-
-
AZ_ over 13 yearsI know the height and width of image but I want to know the Height and width of ImageView so that I can scale it accordingly. I hope you get my point.
-
AZ_ over 13 yearsvery nice code brother and logical +1 for that. you can also achieve this by setting ImageView property android:adjustViewBounds="true" to true :)
-
AZ_ over 13 yearsI have given default height and width to ImageView but when I try to get them in code it returns me 0. I think I have to inflate ImageView first. but how to ?
-
Pedro Loureiro over 13 yearsI address that in my answer. Read it again :) I think your view is already inflated. Can you see it? if you can, then it's inflated.
-
Geetanjali almost 13 yearshey but while debugging pointer don't go minside onPreDraw().Why is it so?
-
mAndroid over 12 yearsHi, this is great but how would I set variables that I can use elsewhere in the activity? If I set instance variables they are not available until onCreate has finished.
-
android developer about 11 yearsIn case the imageView has a match_parent for its width, and adjustViewBounds set to true, it always returns the full size instead of the one being shown. Is there any other way?
-
sagus_helgy over 10 yearsI think you need to add vto.removeOnPreDrawListener(this); line inside onPreDraw()
-
KK_07k11A0585 about 10 years@kcoppock i am showing one alert dialog and onPreDraw method is calling everytime. Any idea how to avoid that
-
Kevin Coppock about 10 years@KK_07 see the edit as per Sufferer's comment. Just have to remove the listener after you're done with it.
-
Chris almost 10 yearshow do you assign values to finalHeight and finalWidth inside an inner class without making them final?
-
eddy almost 10 yearsCool!! Thank you very much. Do you think you could help me with this question : stackoverflow.com/questions/25549679/…
-
AZ_ over 9 years@jww I don't really reply to negative voters :p, First read about getWidth and getMeasured in Android documentation. then come and down vote people.
-
Milad gh over 8 yearsonWindowFocusChanged just work on API18 and over, and it can't be best way... however thanks
-
Jean-François Corbett almost 8 yearsThis answer is being discussed on meta.
-
stumped almost 8 yearsThe result is really big for me. Is it in dp or other units?
-
Pragya Mendiratta about 6 years@kcoppock : how can we get the exact height of image in imageview ?
-
Xavi Jimenez over 4 yearsReally helpful!
-
rommex about 4 yearsWrong. Querying width and height from UI doesn't guarantee that those values are ready. The view inflation just happened to be finished in your sample, but it may very well be otherwise