How to get the width and height of an android.widget.ImageView?

156,411

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?

Share:
156,411

Related videos on Youtube

AZ_
Author by

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, 2020

Comments

  • AZ_
    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
      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
      bigstones over 13 years
      can't you just use android:scaleType="centerInside"?
    • GregNash
      GregNash almost 13 years
      I 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
      Rich over 10 years
      @nightcracker: That's not ASCII.
    • orlp
      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
      audiojared almost 5 years
      This 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_
    AZ_ over 13 years
    I 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_
    AZ_ over 13 years
    very nice code brother and logical +1 for that. you can also achieve this by setting ImageView property android:adjustViewBounds="true" to true :)
  • AZ_
    AZ_ over 13 years
    I 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
    Pedro Loureiro over 13 years
    I 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
    Geetanjali almost 13 years
    hey but while debugging pointer don't go minside onPreDraw().Why is it so?
  • mAndroid
    mAndroid over 12 years
    Hi, 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
    android developer about 11 years
    In 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
    sagus_helgy over 10 years
    I think you need to add vto.removeOnPreDrawListener(this); line inside onPreDraw()
  • KK_07k11A0585
    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
    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
    Chris almost 10 years
    how do you assign values to finalHeight and finalWidth inside an inner class without making them final?
  • eddy
    eddy almost 10 years
    Cool!! Thank you very much. Do you think you could help me with this question : stackoverflow.com/questions/25549679/…
  • AZ_
    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
    Milad gh over 8 years
    onWindowFocusChanged just work on API18 and over, and it can't be best way... however thanks
  • Jean-François Corbett
    Jean-François Corbett almost 8 years
    This answer is being discussed on meta.
  • stumped
    stumped almost 8 years
    The result is really big for me. Is it in dp or other units?
  • Pragya Mendiratta
    Pragya Mendiratta about 6 years
    @kcoppock : how can we get the exact height of image in imageview ?
  • Xavi Jimenez
    Xavi Jimenez over 4 years
    Really helpful!
  • rommex
    rommex about 4 years
    Wrong. 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