Working with Images, Icons, and Cards
# CHAPTER 6
Working with Images, Icons, and Cards
1. Introduction
Visual elements like images, icons, and cards are the heartbeat of modern app design. A wall of text is boring, but a well-placed image or a beautifully elevated card can instantly make an app feel premium and engaging. In this chapter, we will explore how to display images usingImageView, incorporate scalable vector icons, and group content into elegant, elevated surfaces using MaterialCardView.
2. Learning Objectives
By the end of this chapter, you will be able to:-
Display images in your app using
ImageViewandscaleType.
- Import and use scalable vector assets for sharp, lightweight icons.
- Understand elevation and drop shadows in Material Design.
-
Use
MaterialCardViewto create beautiful, rounded UI components.
- Build a real-world Ecommerce Product Card.
3. ImageView Basics
TheImageView component is used to display images (like PNGs, JPEGs, or WebP) or vector graphics.
#### Scale Types
Because screen sizes vary, your image might not perfectly fit its ImageView container. The android:scaleType attribute dictates how the image adapts:
-
centerCrop: Scales the image uniformly so that both dimensions (width and height) of the image will be equal to or larger than the corresponding dimension of the view. It crops the excess. (Highly recommended for profile pictures and hero images).
-
fitCenter: Scales the image uniformly to fit inside the view without cropping, centering it.
-
centerInside: Centers the image inside the view, shrinking it if it's larger than the view.
4. Vector Assets and Icons
Instead of using multiple PNG files for different screen densities, Android allows you to use VectorDrawables (XML files based on SVG).- They scale perfectly without losing quality.
- They have a tiny file size.
-
You can easily change their color (tint) using
android:tint.
How to add an icon in Android Studio:
Right-click on the res/drawable folder -> New -> Vector Asset -> Click the "Clip Art" icon to choose from hundreds of built-in Material icons.
5. MaterialCardView
A Card is a sheet of material that serves as an entry point to more detailed information. Cards have rounded corners and an elegant drop shadow (elevation). To use it, ensure you are using the Material Components theme.Key properties of MaterialCardView:
-
app:cardCornerRadius: Defines how round the corners are.
-
app:cardElevation: Defines the height of the drop shadow.
-
app:strokeColor&app:strokeWidth: For outlined cards.
6. Mini Project: Ecommerce Product Card UI
Let's combineMaterialCardView, ImageView, and TextView to create a beautiful product card.
7. Design Principles
- Use High-Quality Assets: Low-resolution images ruin even the best layouts.
- Consistent Corner Radii: If your cards have an 8dp corner radius, ensure your dialogs and buttons follow a similar visual language.
- Subtle Elevation: Avoid excessive elevation (shadows). Elevation implies hierarchy; use it to show that a card is "lifted" above the background, not floating in outer space.
8. Common Mistakes
-
Oversized Images: Loading massive 4K images into a tiny
ImageViewwithout resizing them programmatically can causeOutOfMemoryerrors and crash your app.
-
Missing
scaleType: Failing to define a scale type often results in awkward white spaces around your image.
9. Best Practices
- Use Vector Assets for all flat icons. Use WebP or highly compressed JPEGs for photos.
-
Use
app:tintto colorize icons dynamically to support Light/Dark mode transitions seamlessly.
10. Exercises
-
1.
Create a
MaterialCardViewwith a thick blue stroke outline instead of a drop shadow.
-
2.
Change the
scaleTypeof anImageViewfromcenterCroptofitCenterand observe the difference.
11. UI Design Challenges
Challenge: Design a "Music Track" item. It should be a horizontalMaterialCardView. On the left, display the album art (ImageView). Next to it, stack the Song Title and Artist Name. On the far right, add a Vector Asset icon of a "Play" button.
12. MCQ Quiz with Answers
Which scaleType ensures the image completely fills the ImageView without distorting its aspect ratio, but may crop the edges?
What is the main advantage of using Vector Assets over PNGs for icons?
13. Interview Questions
-
Q: What causes an
OutOfMemoryerror in Android, and how canImageViewcontribute to it?
- Q: Explain the concept of Elevation in Material Design.
- Q: How do VectorDrawables work under the hood in Android?
14. FAQs
Q: How do I load images from a URL/Internet into an ImageView? A: You should not do this manually. Instead, use a powerful third-party image loading library like Glide or Coil. They handle downloading, caching, and memory management automatically!15. Summary
In this chapter, we learned how to elevate our UI—literally and figuratively. We exploredImageView and its vital scaleType attribute, transitioned to scalable Vector Assets for icons, and learned how to group content beautifully using MaterialCardView with rounded corners and drop shadows.