ImageCard

Android

Component in Wear Material 3 Compose

Wear Material 3 [Card] that takes a container painter for drawing a background image, and offers a single slot to take any content.

The [ImageCard] is Rectangle-shaped with rounded corners by default.

Cards can be enabled or disabled. A disabled card will not respond to click events.

Last updated:

Installation

dependencies {
   implementation("androidx.wear.compose:compose-material3:1.0.0-alpha36")
}

Overloads

@Composable
fun ImageCard(
    onClick: () -> Unit,
    containerPainter: Painter,
    modifier: Modifier = Modifier,
    onLongClick: (() -> Unit)? = null,
    onLongClickLabel: String? = null,
    enabled: Boolean = true,
    shape: Shape = CardDefaults.shape,
    colors: CardColors = CardDefaults.imageCardColors(),
    border: BorderStroke? = null,
    contentPadding: PaddingValues = CardDefaults.ImageContentPadding,
    interactionSource: MutableInteractionSource? = null,
    transformation: SurfaceTransformation? = null,
    content: @Composable ColumnScope.() -> Unit,
): Unit

Parameters

namedescription
onClickWill be called when the user clicks the card
containerPainterThe [Painter] to use to draw the container image of the [ImageCard], such as returned by [CardDefaults.containerPainter].
modifierModifier to be applied to the card
onLongClickCalled when this card is long clicked (long-pressed). When this callback is set, [onLongClickLabel] should be set as well.
onLongClickLabelSemantic / accessibility label for the [onLongClick] action.
enabledControls the enabled state of the card. When false, this card will not be clickable and there will be no ripple effect on click. Wear cards do not have any specific elevation or alpha differences when not enabled - they are simply not clickable.
shapeDefines the card's shape. It is strongly recommended to use the default as this shape is a key characteristic of the Wear Material Theme
colors[CardColors] that will be used to resolve the colors used for this card in different states. See [CardDefaults.cardColors].
borderA BorderStroke object which is used for drawing outlines.
contentPaddingThe spacing values to apply internally between the container and the content
interactionSourcean optional hoisted [MutableInteractionSource] for observing and emitting [Interaction]s for this card. You can use this to change the card's appearance or preview the card in different states. Note that if null is provided, interactions will still happen internally.
transformationTransformation to be used when card appears inside a container that needs to dynamically change its content separately from the background.
contentThe main slot for a content of this card
@Composable
fun ImageCard(
    onClick: () -> Unit,
    containerPainter: Painter,
    title: @Composable RowScope.() -> Unit,
    modifier: Modifier = Modifier,
    onLongClick: (() -> Unit)? = null,
    onLongClickLabel: String? = null,
    time: @Composable (() -> Unit)? = null,
    subtitle: @Composable (ColumnScope.() -> Unit)? = null,
    enabled: Boolean = true,
    shape: Shape = CardDefaults.shape,
    colors: CardColors = CardDefaults.imageCardColors(),
    border: BorderStroke? = null,
    contentPadding: PaddingValues = CardDefaults.ImageContentPadding,
    interactionSource: MutableInteractionSource? = null,
    transformation: SurfaceTransformation? = null,
    content: @Composable (() -> Unit)? = null,
): Unit

Parameters

namedescription
onClickWill be called when the user clicks the card
containerPainterThe [Painter] to use to draw the container image of the [ImageCard], such as returned by [CardDefaults.containerPainter].
titleA slot for displaying the title of the card, expected to be one or two lines of text.
modifierModifier to be applied to the card
onLongClickCalled when this card is long clicked (long-pressed). When this callback is set, [onLongClickLabel] should be set as well.
onLongClickLabelSemantic / accessibility label for the [onLongClick] action.
timeAn optional slot for displaying the time relevant to the contents of the card, expected to be a short piece of text. Depending on whether we have a [content] or not, can be placed at the end of the [title] line or above it.
subtitleAn optional slot for displaying the subtitle of the card, expected to be one line of text.
enabledControls the enabled state of the card. When false, this card will not be clickable and there will be no ripple effect on click. Wear cards do not have any specific elevation or alpha differences when not enabled - they are simply not clickable.
shapeDefines the card's shape. It is strongly recommended to use the default as this shape is a key characteristic of the Wear Material Theme
colors[CardColors] that will be used to resolve the colors used for this card in different states. See [CardDefaults.cardColors].
borderA BorderStroke object which is used for drawing outlines.
contentPaddingThe spacing values to apply internally between the container and the content
interactionSourcean optional hoisted [MutableInteractionSource] for observing and emitting [Interaction]s for this card. You can use this to change the card's appearance or preview the card in different states. Note that if null is provided, interactions will still happen internally.
transformationTransformation to be used when card appears inside a container that needs to dynamically change its content separately from the background.
contentThe optional body content of the card. If not provided then title and subtitle are expected to be provided

Code Examples

ImageCardSample

@Composable
fun ImageCardSample() {
    ImageCard(
        onClick = { /* Do something */ },
        containerPainter =
            CardDefaults.containerPainter(image = painterResource(id = R.drawable.backgroundimage)),
    ) {
        Text("Image card")
    }
}

ImageCardWithTimeAndTitleSample

@Composable
fun ImageCardWithTimeAndTitleSample() {
    ImageCard(
        onClick = { /* Do something */ },
        containerPainter =
            CardDefaults.containerPainter(image = painterResource(id = R.drawable.backgroundimage)),
        title = { Text("Card title") },
        subtitle = { Text("Subtitle") },
        time = { Text("Now") },
        contentPadding = CardDefaults.ImageContentPadding,
        modifier = Modifier.semantics { contentDescription = "Background image" }
    ) {
        Text("Card content")
    }
}
by @alexstyl