Senger CodeLab 🚀

Fit image into ImageView keep aspect ratio and then resize ImageView to image dimensions

September 29, 2026

Fit image into ImageView keep aspect ratio and then resize ImageView to image dimensions

Developing visually appealing Android applications often hinges on the correct display of images. A common challenge developers face is how to efficiently fit an image into an ImageView, keep its aspect ratio, and then resize the ImageView to the image dimensions. This isn’t just about making an image look good; it’s crucial for maintaining layout integrity, preventing image distortion, and optimizing performance across a myriad of Android devices. Without a proper understanding of ImageView properties, you might end up with stretched, cropped, or poorly scaled images that detract significantly from the user experience. This guide will walk you through the essential techniques, from fundamental XML attributes to advanced programmatic approaches and the power of image loading libraries, ensuring your visuals always appear crisp and correctly proportioned.

Mastering ImageView ScaleTypes and AdjustViewBounds

The foundation of proper image display in Android lies within the android:scaleType attribute and android:adjustViewBounds property of the ImageView. These two attributes work in tandem to control how a source image (Drawable) is scaled and positioned within the ImageView’s bounds. Understanding their nuances is critical for achieving the desired visual outcome, especially when you need to maintain the image’s inherent aspect ratio without explicitly setting fixed dimensions.

The scaleType attribute dictates how the image’s intrinsic dimensions are matched to the ImageView’s dimensions. For instance, fitCenter scales the image uniformly (maintaining aspect ratio) so that both dimensions (width and height) of the image are equal to or less than the corresponding dimension of the ImageView. The image is then centered in the ImageView. Other types like centerCrop will scale and crop to fill the ImageView, potentially losing parts of the image, while centerInside ensures the entire image is visible, but might leave empty space if the image is smaller than the ImageView. When the goal is to fit an image into an ImageView, keep aspect ratio, fitCenter or centerInside are often your primary choices.

Crucially, android:adjustViewBounds="true" plays a vital role in letting the ImageView adapt its own bounds to the image’s aspect ratio. When set to true, and combined with a scaleType that respects aspect ratio (like fitCenter), the ImageView will automatically adjust its dimensions (either width or height, depending on which one is set to wrap_content) to match the aspect ratio of the loaded image. This is often the simplest and most effective way to ensure the ImageView’s size dynamically adapts to the image content, preventing issues like excessive padding or unwanted cropping. For example, if you have an ImageView with a fixed width and wrap_content height, enabling adjustViewBounds will make the height adjust proportionally.

Programmatic Resizing for Dynamic Layouts

While XML attributes offer a straightforward solution for many scenarios, there are times when you need more granular control over how to resize ImageView to image dimensions programmatically. This is particularly relevant when images are loaded dynamically, their dimensions are unknown at compile time, or when your layout needs to respond precisely to the image’s intrinsic size after it has been loaded. Programmatic resizing ensures that your layout adapts perfectly, avoiding common pitfalls like oversized ImageViews or incorrect aspect ratios.

The key to programmatic resizing is to obtain the intrinsic dimensions of the loaded image and then apply calculated dimensions to the ImageView. This often involves waiting for the image to be fully loaded and rendered. A common pattern uses an onGlobalLayoutListener to ensure the ImageView has been measured and laid out before attempting to set its new dimensions. Once the image’s Drawable is available, you can retrieve its intrinsicWidth and intrinsicHeight. With these values, you can calculate the new width and height for the ImageView while preserving the aspect ratio, especially if one dimension (e.g., width) is fixed by the parent layout.

  1. Load the Image: Use an image loading library (like Glide or Picasso) or Android’s native methods to load the image into the ImageView. Ensure the image is fully loaded before proceeding.
  2. Get Image Intrinsic Dimensions: Once the image is loaded, retrieve its inherent width and height from the Drawable. For example: int imageWidth = imageView.getDrawable().getIntrinsicWidth(); and int imageHeight = imageView.getDrawable().getIntrinsicHeight();.
  3. Calculate New ImageView Dimensions: Determine the target width or height for the ImageView. If your ImageView has a fixed width, calculate the proportional height: newHeight = (int) (imageHeight (fixedWidth / (float) imageWidth));. Similarly, if height is fixed, calculate width.
  4. Apply New Dimensions: Set the calculated dimensions to the ImageView’s layout parameters. For example: imageView.getLayoutParams().width = fixedWidth; and imageView.getLayoutParams().height = newHeight;. Remember to call imageView.requestLayout(); to apply the changes.

This approach gives you complete control, allowing you to fine-tune how the ImageView adapts to different image sizes and ensures optimal use of screen real estate. It’s particularly useful when dealing with images that have highly variable dimensions, such as user-generated content.

Infographic here
Leveraging Image Loading Libraries for Seamless Integration -----------------------------------------------------------

Modern Android development heavily relies on robust image loading libraries like Glide and Picasso to handle the complexities of fetching, caching, and displaying images. These libraries not only simplify the process but also offer powerful features to fit an image into an ImageView, keep aspect ratio, and then resize the ImageView to image dimensions with minimal code. They abstract away much of the manual work involved in bitmap management, threading, and memory optimization, leading to more stable and performant applications.

When using libraries like Glide, achieving aspect ratio preservation and dynamic ImageView resizing becomes incredibly streamlined. For instance, Glide’s fitCenter() and centerInside() transformations directly correspond to the ImageView.ScaleType properties, handling the scaling logic internally. Furthermore, Glide can automatically detect and use the ImageView’s dimensions, or you can specify exact target dimensions. When combined with android:adjustViewBounds="true" on your ImageView, Glide will often load the image and allow the ImageView to resize itself accordingly once the image’s intrinsic dimensions are known. This significantly reduces the boilerplate code you would otherwise write to manage layout changes and dimension calculations manually.

Picasso, another widely used library, offers similar capabilities through methods like .fit() and .centerInside(). The .fit() method attempts to resize the image to fit the ImageView’s dimensions while maintaining the aspect ratio, which is perfect for ensuring your images look correct without distortion. Both libraries are engineered to efficiently handle image transformations and memory management, crucial aspects for any application displaying multiple images. According to a study by Android Arsenal, these libraries are among the most popular choices for image handling due to their performance benefits and ease of use, making them indispensable tools for effective image display.

Advanced Scenarios and Best Practices

Even with standard techniques, certain advanced scenarios or common pitfalls require specific attention to ensure you consistently fit an image into an ImageView, keep aspect ratio and then resize ImageView to image dimensions correctly. One such scenario involves handling ImageViews within complex layouts, such as those inside a RecyclerView or a ConstraintLayout, where parent constraints can interfere with the ImageView’s ability to adjust its bounds freely.

When an ImageView’s width or height is set to match_parent, it will attempt to fill its parent, potentially overriding the effect of adjustViewBounds="true" or aspect ratio calculations if the parent’s aspect ratio doesn’t match the image. In such cases, using ConstraintLayout with aspect ratio constraints (e.g., app:layout_constraintDimensionRatio="H,16:9") on the ImageView can be a powerful alternative. Question & Answer :

How to fit an image of random size to an ImageView?
When:

  • Initially ImageView dimensions are 250dp * 250dp
  • The image’s larger dimension should be scaled up/down to 250dp
  • The image should keep its aspect ratio
  • The ImageView dimensions should match scaled image’s dimensions after scaling

E.g. for an image of 100*150, the image and the ImageView should be 166*250.
E.g. for an image of 150*100, the image and the ImageView should be 250*166.

If I set the bounds as

<ImageView android:id="@+id/picture" android:layout_width="250dp" android:layout_height="250dp" android:layout_gravity="center_horizontal" android:layout_marginTop="20dp" android:adjustViewBounds="true" /> 

images fit properly in the ImageView, but the ImageView is always 250dp * 250dp.

May not be answer for this specific question, but if someone is, like me, searching for answer how to fit image in ImageView with bounded size (for example, maxWidth) while preserving Aspect Ratio and then get rid of excessive space occupied by ImageView, then the simplest solution is to use the following properties in XML:

android:scaleType="centerInside" android:adjustViewBounds="true"