Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageView.getWidth() return same value in landscape and portrait screen

Tags:

android

I want to make my ImageView's size is rectangle shaped based on its width and it's done with overiding onGlobalLayout() and put photo.getWidth(). But it is only work in portrait screen, and no in landscape screen.

Portrait is works fine :

enter image description here

In landscape, the image not rectangle shaped

enter image description here

And this is the code :

fragment_productdetail.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:orientation="horizontal">
    <ImageView
        android:id="@+id/ivImage0"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_margin="3dp"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"
        android:src="@drawable/noimage"/>
    <ImageView
        android:id="@+id/ivImage1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_margin="3dp"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"
        android:src="@drawable/noimage"/>
    <ImageView
        android:id="@+id/ivImage2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_margin="3dp"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"
        android:src="@drawable/noimage"/>
    <ImageView
        android:id="@+id/ivImage3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_margin="3dp"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"
        android:src="@drawable/noimage"/>
</LinearLayout>

FragmentProductDetail.java

ivPhotos[0] = (ImageView) inflatedView.findViewById(R.id.ivImage0);
ivPhotos[1] = (ImageView) inflatedView.findViewById(R.id.ivImage1);
ivPhotos[2] = (ImageView) inflatedView.findViewById(R.id.ivImage2);
ivPhotos[3] = (ImageView) inflatedView.findViewById(R.id.ivImage3);

for (final ImageView photo : ivPhotos) {
    photo.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            photo.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            photo.getLayoutParams().height = photo.getWidth();
        }
    });
}

Then i try put Log.i("zihad", ""+photo.getWidth()); inside onGlobalLayout() to get the width in landscape, then it gives me same value of width in portrait. So what should i do to make it works in portrait and landscape?

*sorry for bad english, i ask here also to improve my english

like image 549
zihadrizkyef Avatar asked Apr 13 '26 11:04

zihadrizkyef


1 Answers

Quick Tip:

I am not sure, if this is what you want. From what I assume, I think you might require a layout with Square ImageView. If you are trying to change the layout after rendering it, you should call photo.requestLayout(); before setting its height.

An alternative solution would be to use a SquareImageView. To achieve this, just create a View which extends ImageView and set the height by overriding its onMeasure like this.

public class MyImageView extends ImageView {

 @Override
 public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  int width = getMeasuredWidth();
  setMeasuredDimension(width, width);
}

Now in your layout, substitute ImageView for MyImageView(with complete package name).

like image 104
Ashik Vetrivelu Avatar answered Apr 15 '26 23:04

Ashik Vetrivelu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!