Build apps faster with our new App builder! Check it out →

AppCard

Android

Component in Wear Material Compose

Opinionated Wear Material [Card] that offers a specific 5 slot layout to show information about an application, e.g. a notification. AppCards are designed to show interactive elements from multiple applications. They will typically be used by the system UI, e.g. for showing a list of notifications from different applications. However it could also be adapted by individual application developers to show information about different parts of their application.

The first row of the layout has three slots, 1) a small optional application [Image] or [Icon] of size [CardDefaults.AppImageSize]x[CardDefaults.AppImageSize] dp, 2) an application name (emphasised with the [CardColors.appColor()] color), it is expected to be a short start aligned [Text] composable, and 3) the time that the application activity has occurred which will be shown on the top row of the card, this is expected to be an end aligned [Text] composable showing a time relevant to the contents of the [Card].

The second row shows a title, this is expected to be a single row of start aligned [Text].

The rest of the [Card] contains the content which can be either [Text] or an [Image]. If the content is text it can be single or multiple line and is expected to be Top and Start aligned.

If more than one composable is provided in the content slot it is the responsibility of the caller to determine how to layout the contents, e.g. provide either a row or a column.

Last updated:

Installation

dependencies {
   implementation("androidx.wear.compose:compose-material:1.5.0-alpha04")
}

Overloads

@Composable
fun AppCard(
    onClick: () -> Unit,
    appName: @Composable RowScope.() -> Unit,
    time: @Composable RowScope.() -> Unit,
    title: @Composable RowScope.() -> Unit,
    modifier: Modifier = Modifier,
    enabled: Boolean = true,
    appImage: @Composable (RowScope.() -> Unit)? = null,
    backgroundPainter: Painter = CardDefaults.cardBackgroundPainter(),
    contentColor: Color = MaterialTheme.colors.onSurfaceVariant,
    appColor: Color = contentColor,
    timeColor: Color = contentColor,
    titleColor: Color = MaterialTheme.colors.onSurface,
    content: @Composable ColumnScope.() -> Unit,
)

Parameters

namedescription
onClickWill be called when the user clicks the card
appNameA slot for displaying the application name, expected to be a single line of start aligned text of [Typography.title3]
timeA slot for displaying the time relevant to the contents of the card, expected to be a short piece of end aligned text.
titleA slot for displaying the title of the card, expected to be one or two lines of start aligned text of [Typography.button]
modifierModifier to be applied to the card
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.
appImageA slot for a small ([CardDefaults.AppImageSize]x[CardDefaults.AppImageSize] ) [Image] associated with the application.
backgroundPainterA painter used to paint the background of the card. A card will normally have a gradient background. Use [CardDefaults.cardBackgroundPainter()] to obtain an appropriate painter
contentColorThe default color to use for content() slot unless explicitly set.
appColorThe default color to use for appName() and appImage() slots unless explicitly set.
timeColorThe default color to use for time() slot unless explicitly set.
titleColorThe default color to use for title() slot unless explicitly set.
contentSlot for composable body content displayed on the Card

Code Examples

AppCardWithIcon

@Composable
fun AppCardWithIcon() {
    AppCard(
        onClick = {},
        appName = { Text("AppName") },
        appImage = {
            Icon(
                painter = painterResource(id = R.drawable.ic_airplanemode_active_24px),
                contentDescription = "airplane",
                modifier =
                    Modifier.size(CardDefaults.AppImageSize)
                        .wrapContentSize(align = Alignment.Center),
            )
        },
        title = { Text("AppCard") },
        time = { Text("now") },
    ) {
        Text("Some body content")
        Text("and some more body content")
    }
}

AppCardWithImage

@Composable
fun AppCardWithImage() {
    AppCard(
        onClick = {},
        appName = { Text("App name") },
        appImage = {
            Icon(
                painter = painterResource(id = R.drawable.ic_airplanemode_active_24px),
                contentDescription = "airplane",
                modifier =
                    Modifier.size(CardDefaults.AppImageSize)
                        .wrapContentSize(align = Alignment.Center),
            )
        },
        title = {
            Text(
                text = "Title with maximum two lines",
                maxLines = 2,
            )
        },
        time = { Text("now") },
    ) {
        Spacer(Modifier.height(6.dp))
        Image(
            modifier =
                Modifier.padding(end = 28.dp).aspectRatio(16f / 9f).clip(RoundedCornerShape(16.dp)),
            painter = painterResource(R.drawable.card_background),
            contentScale = ContentScale.Crop,
            contentDescription = null
        )
    }
}
by @alexstyl