How to get the width and heigh of an imageview - android

I have this XML code:
<LinearLayout
android:id="#+id/linearLayoutInner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#layout/gallery_image_background"
/>
Then this code:
LinearLayout linearLayoutInner = (LinearLayout) findViewById(R.id.linearLayoutInner);
ImageView imageView = new ImageView(thisActivityContext);
imageView.setImageResource(R.drawable.example);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
imageView.setLayoutParams(lp);
linearLayoutInner.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
linearLayoutInner.addView(imageView);
I then call an own function that is meant to scale the bitmap image until one of the sides reach the edge (i.e. so if the original bitmat is twice as wide as tall, it will keep the proportion inside the imageview, something that is apparently not supported by any scaletype setting):
SharedCode.sharedUtilScaleImage(imageView);
And here comes the problem. That function needs to know the size of the view containing the bitmap drawable. if imageView behaves correctly, it should use MATCH_PARENT and hence give the width/height of the linearLayoutInner. However, the following code returns zero:
int heightParent = max(imageView.getLayoutParams().height, imageView.getHeight());
int widthParent = max(imageView.getLayoutParams().width, imageView.getWidth());
How do I solve this? And why am I returned 0 instead of the correct height/width?

You're probably calling the code too early, before the View's onMeasure() has been called. Until that happens, its size is unknown.
final ImageView iv....
ViewTreeObserver vto = iv.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
//Measure
iv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});

final ImageView imageView = (ImageView )findViewById(R.id.image_test);
ViewTreeObserver vto = imageView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
imageView .getViewTreeObserver().removeGlobalOnLayoutListener(this);
imageView.getHeight(); // This will return actual height.
imageView.getWidth(); // This will return actual width.
}
});

Seem like you might be calling from onCreate(). u need to wait for activity window to attached and then call getWidth() andgetHeight() on imageView.You can try calling getWidth() and getHeight() from onWindowFocusChanged() method of your activity.
Edit
#Override
public void onWindowFocusChanged(boolean hasFocus){
int width=imageView.getWidth();
int height=imageView.getHeight();
}

Related

Final width of Imageview

When I set an ImageView bitmap on create of an Activity. At what point can I read the final width of the ImageView?
What seems to happen is the bitmap is set in the onCreate() method and immediately is assigned the width of 965 which I can see when entering the onLayout method of the ImageView, then more of the layout/creation occurs and the ImageView width changes to 909 (I guess this is due to the adjustment to the screen size?).
What I want to do is read the final value of 909 to set the width of another view programmatically.
I've tried the addOnGlobalLayoutListener of the ImageView to try to listen of the completion of the layout, however reading the getMeasuredWidth() method of the ImageView still returns the 965 value.
What is the final event called once the layout calculations are completing which will allow me to set the width of another view during the creation.
You need to wait for Activity window to be attached and then get the ImageView width. To do that, you can override onWindowFocusChanged() method of the Activity:
#Override
public void onWindowFocusChanged(boolean hasFocus){
int imageWidth = yourImageView.getWidth();
}
As a second suggestion:
You can also add a OnPreDrawListener and use his onPreDraw() method
At this point, all views in the tree have been measured and given a
frame
ViewTreeObserver vto = yourImageView.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
int imageWidth = yourImageView.getMeasuredWidth();
return true;
}
});
You can access the final width in the onStart() once the onCreate() has finished.

Android ImageView some kind of size changed event?

I have an ImageView which has layout_width of fill_parent. I want to get the width of this ImageView in order to calculate the size of the thumbnail. The problem is that imageView.getWidth() and imageView.getHeight() both return 0 because the layout hasn't been sat yet.
My question is, does the ImageView has some kind of SizeChanged event or something of the kind? How do I react as soon as the ImageView width has been established?
Update:
I solved my problem this way. As I actually only needed the width of the ImageView which is set to fill_parent, what I actually needed was the device window size:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int targetW = metrics.widthPixels;
You can hook into a number of View tree events using a ViewTreeObserver. Specifically, either an OnGlobalLayoutListener or an OnPreDrawListener should work for you depending on what you are doing with the size.
Here's an example of using a ViewTreeObserverto get a View's width once everything has been laid out:
final ImageView myImageView = (ImageView) findViewById(R.id.image);
final ViewTreeObserver observer = myImageView.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int height = myImageView.getHeight();
// Do something with the height
// Remove the layout listener so we don't waste time on future passes
observer.removeOnGlobalLayoutListener(this);
}
});
According to this post's answer:
ImageView.getWidth() returns 0
You will need to call it from the following function:
public void onWindowFocusChanged(boolean hasFocus)
This is because the window has not yet been attached and as you have noticed your imageview will have no dimensions yet.

Android: ImageView getting negative width

I'm not sure why, but when I set the width of an imageview (called c), it returns a negative value.
ivPiano is also an imageView.
ViewTreeObserver vto = llPiano.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
llPiano.getViewTreeObserver().removeGlobalOnLayoutListener(this);
c.setTop(ivPiano.getTop());
c.setLeft(ivPiano.getLeft());
ShowMessageLong(ivPiano.getWidth());
c.getLayoutParams().width = ivPiano.getWidth();
c.getLayoutParams().height= ivPiano.getHeight();
c.bringToFront();
ShowMessageLong(c.getWidth());
}
});
ShowMessageLong() just creates and shows a toast. The first time (ivPiano's width) shows 1200, then second one (c's width) returns -22. Any ideas why? Thanks in advance!!

Dimension of my Scaled ImageView

How can I determine the dimensions of my Scaled ImageView? I tried using getWidth() & getHeight it always returns 0. My xml layout for the ImageView is as follows:
<ImageView
android:id="#+id/imgView"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="#drawable/my_image" />
Please suggest something.
You are probably calling getWidth() method too early. The width and height of your ImageView will not be available until the View has been rendered on the screen. Try getting them within onWindowFocusChanged:
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
int width = iv.getWidth();
int height = iv.getHeight();
// ...
}
You won't get those dimensions in onCreate. The view isn't measured yet and the hierarchy is not set. Those will work but you need to use them somewhere after the app has drawn the screen or the alternative is to set a ViewTreeObserver to notify you when the layout is set.ViewTreeObserver
Here is how its done:
call getVieeTreeObserver() to get a reference to the viewTreeObserver in your activity.
then add a onGlobalLayoutListener:
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
#Override
public void onGlobalLayout()
{
ViewTreeObserver obs = MyActivity.this.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
}
});
Inside this onGlobalLayout method you can use your view and get its width and height and do whatever with them.

Android layoutParams getWidth() getHeight()

These methods return correct values when retrieved dynamically for eg. in onCreate() , but the only exception is when the dimension is specified in the xml layout file as undefined ,
like 'wrap content' etc. Is there a way to retrieve such values too ???
This should return the correct width and height. onCreate is called before the layout of the child views are done. So the width and height is not calculated yet. To get the height and width. Put this on the onCreate method
LinearLayout layout = (LinearLayout)findViewById(R.id.YOUD VIEW ID);
ViewTreeObserver vto = layout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
hs.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int width = layout.getMeasuredWidth();
int height = layout.getMeasuredHeight();
}
});

Categories

Resources