We use the Image composable to show pictures in our app. Pictures make apps more interesting and easier to understand.
Image composable in Android Kotlin
Image(
painter = painterResource(id = R.drawable.image_name),
contentDescription = "Description of image"
)painter tells which image to show.
contentDescription helps people using screen readers understand the image.
Image(
painter = painterResource(id = R.drawable.ic_launcher_foreground),
contentDescription = "App logo"
)Image(
painter = painterResource(id = R.drawable.profile_pic),
contentDescription = "User profile picture"
)Image( painter = painterResource(id = R.drawable.background), contentDescription = null )
This app shows the app logo image in the center of the screen. The image has a description for accessibility.
import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.Image import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Surface import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.res.painterResource import androidx.compose.ui.tooling.preview.Preview import com.example.myapp.R class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { MyApp() } } } @Composable fun MyApp() { Surface( modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background ) { Box(modifier = Modifier.fillMaxSize()) { Image( painter = painterResource(id = R.drawable.ic_launcher_foreground), contentDescription = "App logo", modifier = Modifier.align(Alignment.Center) ) } } } @Preview(showBackground = true) @Composable fun DefaultPreview() { MyApp() }
Always provide contentDescription for accessibility unless the image is purely decorative.
You can use different painter sources like painterResource for local images or rememberImagePainter for loading from the internet (with extra libraries).
Use Modifier to change image size, shape, or position.
The Image composable shows pictures in your app.
Use painterResource to load images from your app resources.
Always add contentDescription for users who need screen readers.
Practice
Image composable in Android Jetpack Compose?Solution
Step 1: Understand the role of Image composable
The Image composable is designed to show pictures or images in the app's user interface.Step 2: Compare with other options
Playing audio, creating buttons, and handling input are done by other composables, not Image.Final Answer:
To display pictures or images in the app UI -> Option BQuick Check:
Image composable = display pictures [OK]
- Confusing Image with Button composable
- Thinking Image plays sounds
- Assuming Image handles user input
logo in an Image composable?Solution
Step 1: Recall correct Image syntax
The Image composable requires a painter, which is created by painterResource with the drawable resource ID.Step 2: Check parameters
Image(painter = painterResource(id = R.drawable.logo), contentDescription = "App logo") uses painterResource(id = R.drawable.logo) and contentDescription, which is correct and recommended for accessibility.Final Answer:
Image(painter = painterResource(id = R.drawable.logo), contentDescription = "App logo") -> Option DQuick Check:
Use painterResource and contentDescription [OK]
- Using wrong parameter names like resource or src
- Omitting contentDescription
- Passing imageId instead of painter
Image(painter = painterResource(id = R.drawable.ic_launcher), contentDescription = null)
Solution
Step 1: Understand contentDescription null effect
Setting contentDescription to null means the image is decorative and screen readers will skip it.Step 2: Check image display behavior
The image still displays normally on screen; only accessibility tools ignore it.Final Answer:
The image will display but screen readers will ignore it -> Option AQuick Check:
contentDescription=null means decorative image [OK]
- Thinking app crashes without contentDescription
- Assuming image won't show
- Believing default description is added automatically
Image(painter = painterResource(), contentDescription = "Icon")
Solution
Step 1: Check painterResource usage
painterResource requires the drawable resource ID to be passed with the named parameter 'id'.Step 2: Identify missing 'id' parameter
The code calls painterResource without providing the required 'id' parameter, causing a syntax error.Final Answer:
Missing named parameter 'id' in painterResource call -> Option CQuick Check:
painterResource(id = ...) is required [OK]
- Omitting the 'id' parameter in painterResource
- Setting contentDescription to null incorrectly
- Thinking painterResource is invalid inside Image
profile_pic with a circular shape and a description for accessibility. Which code snippet correctly achieves this?Solution
Step 1: Use painterResource with id and contentDescription
Image(painter = painterResource(id = R.drawable.profile_pic), contentDescription = "User profile picture", modifier = Modifier.clip(CircleShape)) correctly uses painterResource with id and provides a meaningful contentDescription.Step 2: Apply circular clipping with Modifier.clip(CircleShape)
This modifier clips the image into a circle shape, which is the desired effect.Final Answer:
Image(painter = painterResource(id = R.drawable.profile_pic), contentDescription = "User profile picture", modifier = Modifier.clip(CircleShape)) -> Option AQuick Check:
Use painterResource + contentDescription + Modifier.clip(CircleShape) [OK]
- Passing drawable resource directly as painter
- Omitting contentDescription or setting it null
- Using incorrect modifier or parameter names
Ready to prove it?
Pass the test and this pattern moves unknown
