2.3.4. Display A List of Images Using Cards
2.3.4. Display A List of Images Using Cards
Link: https://developer.android.com/codelabs/basic-android-kotlin-training-display-list-cards
In the previous codelab, you created an Affirmations app that displays a list of text in a
RecyclerView.
In this follow-up codelab, you add an inspiring image to each affirmation of your app. You will
display the text and image for each affirmation within a card, using the MaterialCardView
widget from the Material Components for Android library. Then you will finish the app by
polishing the UI to create a more cohesive and beautiful user experience. This is a screenshot of
the completed app:
Prerequisites
• Can add image resources to an app.
• Comfortable modifying an XML layout.
• Able to create an app that displays a list of text in a RecyclerView.
• Able to create an adapter for a RecyclerView.
Both stringResourceId and imageResourceId are integer values. Although this looks okay,
the caller could accidentally pass in the arguments in the wrong order (imageResourceId first
instead of stringResourceId).
To avoid this, you can use Resource annotations. Annotations are useful because they add
additional info to classes, methods, or parameters. Annotations are always declared with an @
symbol. In this case, add the @StringRes annotation to your string resource ID property and
@DrawableRes annotation to your drawable resource ID property. Then you will get a warning if
you supply the wrong type of resource ID.
Affirmation.kt
package com.example.affirmations.model
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
1. Open Datasource.kt. You should see an error for each instantiation of Affirmation.
2. For each Affirmation, add the resource ID of an image as an argument, such as
R.drawable.image1.
Datasource.kt
package com.example.affirmations.data
import com.example.affirmations.R
import com.example.affirmations.model.Affirmation
class Datasource() {
list_item.xml
<TextView
android:id="@+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
3. Inside the LinearLayout, before the TextView, add an ImageView with a resource ID
of item_image.
4. Set the ImageView‘s width to match_parent and height to 194dp. Depending on screen
size, this value should show a few cards on screen at any given time.
5. Set the scaleType to centerCrop.
6. Set the importantForAccessibility attribute to no since the image is used for
decorative purposes.
<ImageView
android:layout_width="match_parent"
android:layout_height="194dp"
android:id="@+id/item_image"
android:importantForAccessibility="no"
android:scaleType="centerCrop" />
Below the initialization of the textView property, add a val called imageView. Use
findViewById() to find the reference to the ImageView with ID item_image and assign it to the
imageView property.
ItemAdapter.kt
3. Polishing the UI
So far you've built a functional app that consists of a list of affirmation strings and images. In
this section, you'll see how small changes in the code and XML can make the app look more
polished.
Add padding
To start with, add some whitespace between the items in the list.
Tip: You can make layout changes in the XML as shown here, or you can make them in the
Attributes panel in Design view, whichever you prefer.
1. Open item_list.xml (app > res > layout > item_list.xml) and add 16dp padding to the
existing LinearLayout.
list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:textAppearance="?attr/textAppearanceHeadline6" />
list_item.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/item_image"
android:layout_width="match_parent"
android:layout_height="194dp"
android:importantForAccessibility="no"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:textAppearance="?attr/textAppearanceHeadline6" />
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
Change the app theme colors
The default app theme color may not be as calming as some other choices you could make. In
this task, you will change the app theme color to blue. You can then change it again using your
own ideas!
You can find pre-defined shades of blue from the Material Design color palette from this link.
In this codelab, you will be using the following colors from the Material Design palette:
• blue_200: #FF90CAF9
• blue_500: #FF2196F3
• blue_700: #FF1976D2
Define colors used within your app in a centralized place: the colors.xml file.
<color name="blue_200">#FF90CAF9</color>
<color name="blue_500">#FF2196F3</color>
<color name="blue_700">#FF1976D2</color>
Now that you have new color resources, you can use them in your theme.
<item name="colorPrimary">@color/blue_500</item>
<item name="colorPrimaryVariant">@color/blue_700</item>
5. Run the app. You should see the app bar color is changed to blue.
Update the dark theme colors
It's good to choose more desaturated colors for the dark theme of the app.
1. Open the dark theme themes.xml file (themes > themes.xml (night)).
2. Add or change the colorPrimary and colorPrimaryVariant theme attributes as
follows:
<item name="colorPrimary">@color/blue_200</item>
<item name="colorPrimaryVariant">@color/blue_500</item>
5. Your app switches to the Dark Theme. Verify that it looks like the screenshot below:
Note: app bar color. You may be wondering why your specified primary color for the dark
theme is not showing in the app bar. The dark app bar is by design. In dark theme, the app bar
and other large areas are by default shown with a dark background (colorSurface) instead of
the primary color. This is because Material dark theme recommends less use of bright colors on
large surfaces. Buttons or other small accents will show the defined primary color.
6. At this point, you can also remove unused colors in your colors.xml file (for example,
the purple color resources used in the default app theme).
12. In the Confirm Icon Path dialog, click Finish. It's OK to overwrite the existing icons.
13. For best practices, you can move the new vector drawables
ic_launcher_foreground.xml and ic_launcher_background.xml into a new
resource directory called drawable-anydpi-v26. Adaptive icons were introduced in API
26, so these resources will only be used on devices running API 26 and above (for any
dpi).
14. Delete the drawable-v24 directory if there's nothing left there.
15. Run your app and notice the beautiful new app icon in the app drawer!
16. As a last step, don't forget to reformat the Kotlin and XML files in the project so your
code is cleaner and follows style guidelines.
4. Solution code
The solution code for the Affirmations app is in the GitHub repository below:
3. On the GitHub page for the project, click the Code button, which brings up a popup.
4. In the popup, click the Download ZIP button to save the project to your computer. Wait
for the download to complete.
5. Locate the file on your computer (likely in the Downloads folder).
6. Double-click the ZIP file to unpack it. This creates a new folder that contains the project
files.
3. In the file browser, navigate to where the unzipped project folder is located (likely in
your Downloads folder).
4. Double-click on that project folder.
5. Wait for Android Studio to open the project.
6. Click the Run button to build and run the app. Make sure it builds as expected.
5. Summary
• To display additional content in a RecyclerView, modify the underlying data model class
and data source. Then update the list item layout and adapter to set that data onto the
views.
• Use resource annotations to help ensure that the right type of resource ID is passed into a
class constructor.
• Use the Material Components for Android library to have your app more easily follow
the recommended Material Design guidelines.
• Use MaterialCardView to display content in a Material card.
• Small visual tweaks to your app in terms of color and spacing can make the app look
more polished and consistent.
6. Learn more
• Create a list with RecyclerView
• RecyclerView class
• RecyclerView Adapters
• RecyclerView ViewHolder
• Lists in Material Design
• Cards in Material Design
• MaterialCardView
• Getting Started with Material Components for Android
• Android Styling: Themes vs. Styles
• Adaptive icons
7. Challenge task
Note: All challenges are optional and not required for the next activity. They use and may
challenge what you've learned in this and previous related codelabs.