Search code examples
androidandroid-layoutandroid-imageview

Square ImageView


I am trying to show an ImageView which is always square in shape inside a dialog. The dimensions may vary depending upon the resolution of the display(Width in portrait to be precise) but the ImageView is required to form the largest Square possible to accomodate a square image inside it. This is my XML code for the same.

<ImageView
    android:id="@+id/dialog_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="centerInside"
    android:adjustViewBounds="true"
    />

But the problem is that the Square image is set dynamically at runtime and the ImageView ends up being a rectangle with breadth as the Square Image size. What can i do to achieve the same?

EDIT: To give an idea of what i am trying to achieve is like what is shown when a contact is selected in contacts application and the profile picture is clicked. It zooms out and remains as a square on top of the activity.


Solution

  • Set the Image view height at run time but first get the display width with this code

    Display  display = getWindowManager().getDefaultDisplay();
    int swidth = display.getWidth();
    

    and now set the height of image view like this

    LayoutParams params = eventImage.getLayoutParams();
    params.width = LayoutParams.FILL_PARENT;
    params.height = swidth ;
    eventImage.setLayoutParams(params);