How to align ImageView to the bottom, fill its width and height, and keep its aspect ratio?
Categories:
Aligning an ImageView to the Bottom, Filling Width, and Maintaining Aspect Ratio in Android

Learn how to correctly position an ImageView at the bottom of its parent, make it fill the available width and height, and preserve its original aspect ratio in Android layouts.
Achieving specific layout behaviors for ImageView
components in Android can sometimes be tricky, especially when combining multiple requirements like aligning to the bottom, filling parent dimensions, and preserving the aspect ratio. This article will guide you through the necessary XML attributes and their interactions to achieve this precise layout for your ImageView
.
Understanding the Core Attributes
To fulfill the requirements, we need to leverage several key attributes of the ImageView
and its parent layout. The primary attributes involved are android:layout_width
, android:layout_height
, android:layout_alignParentBottom
, and android:scaleType
. Each plays a crucial role in defining the final appearance and positioning of the image.
flowchart TD A[ImageView Requirements] --> B{Fill Width?} B -- Yes --> C[android:layout_width="match_parent"] B -- No --> D[android:layout_width="wrap_content" or fixed dp] A --> E{Fill Height?} E -- Yes --> F[android:layout_height="match_parent"] E -- No --> G[android:layout_height="wrap_content" or fixed dp] A --> H{Align Bottom?} H -- Yes --> I[android:layout_alignParentBottom="true"] H -- No --> J[Other alignment options] A --> K{Maintain Aspect Ratio?} K -- Yes --> L[android:scaleType="centerCrop" or "fitXY" + adjustViewBounds] K -- No --> M[android:scaleType="fitXY" (without adjustViewBounds)] C & F & I & L --> N[Combined Solution]
Decision flow for ImageView layout attributes
Implementing the Solution with RelativeLayout
A RelativeLayout
is often the most straightforward choice for positioning views relative to each other or to the parent container. To align the ImageView
to the bottom and make it fill the width and height while maintaining its aspect ratio, we'll combine layout_width="match_parent"
, layout_height="match_parent"
, layout_alignParentBottom="true"
, and scaleType="centerCrop"
or scaleType="fitCenter"
with adjustViewBounds="true"
.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/my_image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:scaleType="centerCrop"
android:src="@drawable/my_background_image" />
</RelativeLayout>
XML layout for an ImageView aligned to the bottom, filling width/height, and using centerCrop.
android:scaleType="centerCrop"
attribute is crucial here. It scales the image uniformly (maintaining aspect ratio) so that both dimensions (width and height) of the image will be equal to or greater than the corresponding dimension of the view (minus padding). The image is then centered in the view. This ensures the image fills the entire ImageView
bounds without distortion, though parts of the image might be cropped.Alternative: scaleType="fitCenter"
with adjustViewBounds="true"
If you prefer to see the entire image and are willing to have empty space (letterboxing/pillarboxing) around it if the aspect ratios don't match, you can use scaleType="fitCenter"
in conjunction with adjustViewBounds="true"
. This combination ensures the image is scaled down to fit within the ImageView
while preserving its aspect ratio, and the ImageView
itself will adjust its bounds to match the image's aspect ratio if possible, given the match_parent
constraints.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/my_image_view_fit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:src="@drawable/my_background_image" />
</RelativeLayout>
XML layout using fitCenter and adjustViewBounds to preserve aspect ratio without cropping.
android:adjustViewBounds="true"
with android:layout_width="match_parent"
and android:layout_height="match_parent"
, the ImageView
will attempt to adjust its bounds to maintain the aspect ratio. However, because both dimensions are set to match_parent
, the ImageView
will still try to fill the entire parent. The fitCenter
scaleType
will then ensure the image is scaled to fit within these bounds, potentially leaving empty space. If you want the ImageView
itself to shrink to the image's aspect ratio, you might need to use wrap_content
for one of the dimensions or place it within a ConstraintLayout
with ratio constraints.