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.
ImageView class is used to display any kind of image resource in the android application either it can be android. graphics. Bitmap or android. graphics.
You can make a view clickable, as a button, by adding the android:onClick attribute in the XML layout. For example, you can make an image act like a button by adding android:onClick to the ImageView.
We will be building a simple application in which we will be displaying an ImageView and when we click on that ImageView we will get into a new activity or simply we can say that we are going to use ImageView as a button to switch between different activities.
You can add ImageView and VideoView in RelativeLayout and set ImageView to invisible and VideoView to visible and vice-versa and you can play video on onClick.
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);
Create a separate class for defining the size dynamically for your ImageView that should inherit ImageView class as shown below:
public class SquareImageView extends ImageView {
public SquareImageView(Context context) {
super(context);
}
public SquareImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
int width = getMeasuredWidth();
setMeasuredDimension(width, width);
}
}
The setMeasuredDimension(width, width); will automatically set your height as the width.
In xml file use this class as a view in place of ImageView as shown below:
<com.packagepath.SquareImageView
android:id="@+id/Imageview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With