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?