0

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?

3 Answers 3

3

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); } }); 
Sign up to request clarification or add additional context in comments.

3 Comments

(Upvoted) At what point is this onMeasure called? (If it is only called once, I guess I could create some code that would wait.) I need to be able to pass in ImageView references on demand. The heigh/width of the imageview/linearlayout should never change after the initial creation. (The bimaps change of course corresponding to click events on another control.)
@Tom onMeasure() is called once before the View is drawn the first time, and it is called again whenever the size changes.
I am having my ImageView change to 0 after a configuration change. I tried this code, but it is still not working for some reason. Anyone else have a configuration change issue that might have used this successfully? Trying to resize a bitmap, but can't with an ImageView=0.
3
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. } }); 

1 Comment

(Upvoted) Thanks for the answer. I still find it odd that the height/width is not even accessible on onWindowFocusChanged(), but I will try this method instead, and try combine it with my existing code somehow.
1

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(); } 

1 Comment

(Upvoted) It made no difference calling from onWindowFocusChanged :(

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.