Simple Android grid example using RecyclerView with GridLayoutManager like the old GridView
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
RecyclerView with GridLayoutManager is the recommended replacement for legacy GridView in modern Android apps. It offers better performance, flexible layouts, and clearer adapter patterns. A solid setup includes predictable item sizing, spacing, click handling, and efficient binding behavior.
Basic Grid Layout Setup
Define the RecyclerView in XML.
Initialize with GridLayoutManager in activity or fragment.
This gives a simple three-column grid similar to classic grid behavior.
Adapter and ViewHolder Implementation
Keep bind logic focused and avoid expensive work in onBindViewHolder.
A callback-based click API keeps adapter reusable.
Add Consistent Grid Spacing
GridLayoutManager does not add classic GridView spacing automatically. Use ItemDecoration.
Apply once:
This improves visual consistency across screens.
Efficient Image Grids
If grid items include images, use an async image library instead of manual bitmap decode.
Avoid decoding bitmaps on main thread. Main-thread decode causes dropped frames and poor scroll performance.
Span Customization
You can mimic mixed-size tile layouts by customizing span size.
This is a common pattern for featured-card sections.
Migration Tips from Old GridView
When migrating:
- Move click logic into adapter callbacks.
- Replace old adapter with view holder pattern.
- Add explicit spacing decoration.
- Validate accessibility and focus behavior.
Migration is usually easiest when one reusable grid component is created and shared across screens.
Common Pitfalls
- Inflating item views incorrectly with wrong parent parameters.
- Doing expensive transforms in
onBindViewHolder. - Forgetting spacing decoration and getting cramped layouts.
- Failing to handle data updates with proper adapter notifications.
- Ignoring accessibility labels and touch targets in grid items.
Summary
- Use
RecyclerViewplusGridLayoutManageras the modernGridViewreplacement. - Keep adapter binding lightweight and callback-driven.
- Add explicit spacing and stable item sizing for consistent UI.
- Use async image loading for media-heavy grids.
- Build one reusable grid pattern and apply it consistently across app screens.
Related reading
- Simple Android RecyclerView example
- Simplest way to throw an error/exception with a custom message in Swift?
- Simplest way to throw an error/exception with a custom message in Swift?
- Simulate force touch / 3D touch on iPhone 6S or iPhone 6S Plus simulators
- Simulate low network connectivity for Android
- Simulator error FBSSystemServiceDomain code 4
- Since Xcode 8 and iOS10, views are not sized properly on viewDidLayoutSubviews
- single function to dismiss all open view controllers
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.