class PlaceholderState


A state object that can be used to control placeholders. Placeholders are used when the content that needs to be displayed in a component is not yet available, e.g. it is loading asynchronously.

A PlaceholderState should be created for each component that has placeholder data. The state is used to coordinate all of the different placeholder effects and animations.

Placeholder has a number of different effects designed to work together. Modifier.placeholder draws a placeholder shape on top of content that is waiting to load. There can be multiple placeholders in a component. Modifier.placeholderShimmer does a shimmer animation over the whole component that includes the placeholders. There should only be one placeholderShimmer for each component.

NOTE: The order of modifiers is important. If you are adding both Modifier.placeholder and Modifier.placeholderShimmer to the same composable then the shimmer must be before in the modifier chain. Example of Text composable with both placeholderShimmer and placeholder modifiers.

import androidx.compose.foundation.layout.width
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.wear.compose.material3.Text
import androidx.wear.compose.material3.placeholder
import androidx.wear.compose.material3.placeholderShimmer
import androidx.wear.compose.material3.rememberPlaceholderState

var labelText by remember { mutableStateOf("") }
val buttonPlaceholderState = rememberPlaceholderState { labelText.isNotEmpty() }

Text(
    text = labelText,
    overflow = TextOverflow.Ellipsis,
    textAlign = TextAlign.Center,
    modifier =
        Modifier.width(90.dp)
            .placeholderShimmer(buttonPlaceholderState)
            .placeholder(buttonPlaceholderState)
)

// Simulate content loading
LaunchedEffect(Unit) {
    delay(3000)
    labelText = "A label"
}
if (!buttonPlaceholderState.isHidden) {
    LaunchedEffect(buttonPlaceholderState) { buttonPlaceholderState.animatePlaceholder() }
}

Background placeholder effects are used to mask the background of components like buttons and cards until all of the data has loaded. Use PlaceholderDefaults.placeholderButtonColors and PlaceholderDefaults.painterWithPlaceholderOverlayBackgroundBrush to draw the component background.

Once all of the components content is loaded the shimmer will stop and a wipe off animation will remove the placeholders.

Summary

Public functions

suspend Unit

Animate the placeholder according to the placeholder state.

Public properties

Boolean

Returns true if the placeholder content should be shown with no placeholders effects and false if either the placeholder or the wipe-off effect are being shown.

Boolean

Returns true if the wipe-off effect that reveals content is being shown and false if the placeholder effect should be shown.

Public functions

animatePlaceholder

Added in 1.0.0-alpha28
suspend fun animatePlaceholder(): Unit

Animate the placeholder according to the placeholder state. Cancels when the placeholder becomes inactive.

Public properties

isHidden

Added in 1.0.0-alpha28
val isHiddenBoolean

Returns true if the placeholder content should be shown with no placeholders effects and false if either the placeholder or the wipe-off effect are being shown.

isWipingOff

Added in 1.0.0-alpha28
val isWipingOffBoolean

Returns true if the wipe-off effect that reveals content is being shown and false if the placeholder effect should be shown.