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

AndroidImageHeightWidth

Android Problem Overview


╔══════════════════════════════════════════════╗   ^
║ 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

Android Solutions


Solution 1 - Android

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 - Android

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 - Android

I could get image width and height by its drawable;

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

Solution 4 - Android

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 - Android

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?

Solution 6 - Android

your xml file :

 <ImageView android:id="@+id/imageView"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:src="@drawable/image"
               android:scaleType="fitXY"
               android:adjustViewBounds="true"/>

your java file:

ImageView imageView = (ImageView)findViewById(R.id.imageView);
     int width = imageView.getDrawable().getIntrinsicWidth();
     int   height = imageView.getDrawable().getIntrinsicHeight();

Solution 7 - Android

I think you can let the Android OS take care of this for you. Set the scale type on the ImageView to fitXY and the image it displays will be sized to fit the current size of the view.

<ImageView 
    android:layout_width="90px" 
    android:layout_height="60px"
    android:scaleType="fitXY" />

Solution 8 - Android

The simplest way is to get the width and height of an ImageView in onWindowFocusChanged method of the activity

 @Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);

    height = mImageView.getHeight();
    width = mImageView.getWidth();

}

Solution 9 - Android

If you have created multiple images dynamically than try this one:

// initialize your images array

private ImageView myImages[] = new ImageView[your_array_length];

// create programatically and add to parent view

 for (int i = 0; i < your_array_length; i++) {
    			myImages[i] = new ImageView(this);
    			myImages[i].setId(i + 1);
    			myImages[i].setBackgroundResource(your_array[i]);
    			RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
    					frontWidth[i], frontHeight[i]);
    			((MarginLayoutParams) params).setMargins(frontX_axis[i],
    					frontY_axis[i], 0, 0);
    			myImages[i].setAdjustViewBounds(true);
    			myImages[i].setLayoutParams(params);
    
    			if (getIntent() != null && i != your_array,length) {
    				final int j = i;
    				myImages[j].getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
    				    public boolean onPreDraw() {
    				    	myImages[j].getViewTreeObserver().removeOnPreDrawListener(this);
    				finalHeight = myImages[j].getMeasuredHeight();
           				finalWidth = myImages[j].getMeasuredWidth();
    				your_textview.setText("Height: " + finalHeight + " Width: " + finalWidth);
    				        return true;
    				    }
    				});
    			}
    			your_parent_layout.addView(myImages[i], params);
    		}

// That's it. Happy Coding.

Solution 10 - Android

my friend by this u are not getting height of image stored in db.but you are getting view height.for getting height of image u have to create bitmap from db,s image.and than u can fetch height and width of imageview

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAZ_View Question on Stackoverflow
Solution 1 - AndroidKevin CoppockView Answer on Stackoverflow
Solution 2 - AndroidAZ_View Answer on Stackoverflow
Solution 3 - AndroidRashidView Answer on Stackoverflow
Solution 4 - Androidelement6View Answer on Stackoverflow
Solution 5 - AndroidPedro LoureiroView Answer on Stackoverflow
Solution 6 - Androiduser6264840View Answer on Stackoverflow
Solution 7 - AndroidIan LeslieView Answer on Stackoverflow
Solution 8 - AndroidAqibView Answer on Stackoverflow
Solution 9 - AndroidHiren PatelView Answer on Stackoverflow
Solution 10 - Androidchikka.anddevView Answer on Stackoverflow