Jetpack Compose: Getting Started

Aug 1 2023 · Kotlin 1.8.10, Android 13, Android Studio Flamingo

Part 3: Jetpack Controls

12. Displaying Images

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 11. Customize Your TextField Next episode: 13. Checkboxes & Radio Buttons

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

A very popular way of keeping your UI lively is by displaying images. In Jetpack Compose, we can use the Image Composable to display images in the app.

Painter

Let us add an image to the app. In our TestPage Composable, we will add an image.

@Composable
fun TestPage() {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp)
    ) {
        Image(
            painter = painterResource(id = R.drawable.ic_burger),
            contentDescription = "Burger Image",
            modifier = Modifier
                .fillMaxWidth()
                .padding(bottom = 16.dp)
        )
    }
}

contentDescription

The contentDescription parameter is used to specify the content description of the image. It is of type String? and has a default value of null.

contentDescription

@Composable
fun TestPage() {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp)
    ) {
        Image(
            painter = painterResource(id = R.drawable.ic_burger),
            contentDescription = stringResource(id = R.string.burger_image),
            modifier = Modifier
                .fillMaxWidth()
                .padding(bottom = 16.dp)
        )
    }
}

contentScale

The contentScale parameter is used to specify the content scale of the image. It is of type ContentScale and has a default value of Fit.

contentScale

@Composable
fun TestPage() {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp)
    ) {
        Image(
            painter = painterResource(id = R.drawable.ic_burger),
            contentDescription = stringResource(id = R.string.burger_image),
            contentScale = ContentScale.Crop,
            modifier = Modifier
                .fillMaxWidth()
                .padding(bottom = 16.dp)
        )
    }
}

colorFilter

The colorFilter parameter is used to specify the color filter of the image. It is of type ColorFilter? and has a default value of null.

colorFilter

@Composable
fun TestPage() {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp)
    ) {
        Image(
            painter = painterResource(id = R.drawable.ic_burger),
            contentDescription = stringResource(id = R.string.burger_image),
            contentScale = ContentScale.Crop,
            colorFilter = ColorFilter.tint(
                color = Color.Red, 
                blendMode = BlendMode.Difference),
            modifier = Modifier
                .fillMaxWidth()
                .padding(bottom = 16.dp)
        )
    }
}

Conclusion

In this lesson, we learned how to use the Image Composable to display images in the app. We also learned about the various parameters of the Image Composable.