ImageView - have height match width?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
If you want an Android ImageView to stay square, the cleanest solution depends on your layout system. In modern layouts, ConstraintLayout with a 1:1 ratio is usually the best choice, while older projects sometimes need a small custom view that forces height to equal width during measurement.
Use ConstraintLayout For A Square ImageView
With ConstraintLayout, you can declare a ratio directly in XML:
The key parts are:
- Both dimensions are
0dp, which means constraints control them. - Start and end constraints define the width.
- '
app:layout_constraintDimensionRatio="1:1"forces height to match width.'
This is the most maintainable solution for modern Android UIs.
Why adjustViewBounds Is Not Enough
Some developers try:
That preserves the intrinsic aspect ratio of the image content, but it does not guarantee a square view. If the drawable is not square, the ImageView will not become square either.
So if the requirement is "height must equal width," use layout constraints or custom measurement, not just adjustViewBounds.
Custom SquareImageView
If you are not using ConstraintLayout, create a custom subclass:
Then use it in layout XML:
This overrides measurement so the height spec follows the width spec.
Width-Driven Versus Height-Driven Layouts
Most square-image layouts are width-driven because width is easier to constrain in a scrolling list or grid. If you instead want width to match height, you would reverse the measurement logic or design the parent layout so height is the driving dimension.
In a RecyclerView grid, square thumbnails are especially common. ConstraintLayout ratio or a custom square view both work well there, depending on your existing layout stack.
Choosing The Right scaleType
Once the view is square, the image content still needs a scaling rule. Common choices are:
- '
centerCropto fill the square and crop overflow.' - '
fitCenterto show the whole image inside the square.' - '
centerInsidewhen you do not want upscaling.'
For photo galleries or product grids, centerCrop is usually the most visually consistent option.
Jetpack Compose Equivalent
If you are using Compose, the same idea is much simpler:
aspectRatio(1f) is the Compose version of a square constraint.
Common Pitfalls
The biggest mistake is assuming adjustViewBounds makes the view square. It preserves the drawable's aspect ratio, which is a different behavior.
Another common problem is using a custom square view without thinking about parent constraints. If the parent gives an unspecified or conflicting width, the result may not match the intended design.
Developers also sometimes force a square view but forget to set an appropriate scaleType, so the image appears stretched or unexpectedly letterboxed.
Finally, in scrolling lists, avoid overly complex nested layouts just to create square thumbnails. ConstraintLayout ratios or a small custom view are both lighter and clearer.
Summary
- In classic Android layouts,
ConstraintLayoutwith1:1ratio is usually the cleanest solution. - '
adjustViewBoundspreserves image aspect ratio but does not force a square view.' - A custom
SquareImageViewworks well in older or non-constraint layouts. - Pick a
scaleTypesuch ascenterCropbased on how the image should fill the square. - In Compose, use
Modifier.aspectRatio(1f)for the same effect.
Related reading
- Impact of reducing max.poll.records in Kafka Consumer configuration
- Impact of Xcode build options Enable bitcode Yes/No
- Implement a queue in which push_rear, pop_front and get_min are all constant time operations
- Implementation of a hits in last second/minute/hour data structure
- ImageView in circular through XML
- Immutable/Mutable Collections in Swift
- Implementation of Levenshtein distance for mysql/fuzzy search?
- Implementing Babai's quasi-polynomial graph isomorphism?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.