Surface
Functions summary
Unit |
@ComposableMaterial surface is the central metaphor in material design. |
Cmn
|
Unit |
@ComposableMaterial surface is the central metaphor in material design. |
Cmn
|
Unit |
@ComposableMaterial surface is the central metaphor in material design. |
Cmn
|
Unit |
@ComposableMaterial surface is the central metaphor in material design. |
Cmn
|
Functions
Surface
@Composable
@NonRestartableComposable
fun Surface(
modifier: Modifier = Modifier,
shape: Shape = RectangleShape,
color: Color = MaterialTheme.colorScheme.surface,
contentColor: Color = contentColorFor(color),
tonalElevation: Dp = 0.dp,
shadowElevation: Dp = 0.dp,
border: BorderStroke? = null,
content: @Composable () -> Unit
): Unit
Material surface is the central metaphor in material design. Each surface exists at a given elevation, which influences how that piece of surface visually relates to other surfaces and how that surface is modified by tonal variance.
See the other overloads for clickable, selectable, and toggleable surfaces.
The Surface is responsible for:
-
Clipping: Surface clips its children to the shape specified by
shape -
Borders: If
shapehas a border, then it will also be drawn. -
Background: Surface fills the shape specified by
shapewith thecolor. IfcolorisColorScheme.surfacea color overlay will be applied. The color of the overlay depends on thetonalElevationof this Surface, and theLocalAbsoluteTonalElevationset by any parent surfaces. This ensures that a Surface never appears to have a lower elevation overlay than its ancestors, by summing the elevation of all previous Surfaces. -
Content color: Surface uses
contentColorto specify a preferred color for the content of this surface - this is used by theTextandIconcomponents as a default color.
If no contentColor is set, this surface will try and match its background color to a color defined in the theme ColorScheme, and return the corresponding content color. For example, if the color of this surface is ColorScheme.surface, contentColor will be set to ColorScheme.onSurface. If color is not part of the theme palette, contentColor will keep the same value set above this Surface.
To manually retrieve the content color inside a surface, use LocalContentColor.
-
Blocking touch propagation behind the surface.
Surface sample:
import androidx.compose.material3.Surface import androidx.compose.material3.Text Surface { Text("Text on Surface") }
| Parameters | |
|---|---|
modifier: Modifier = Modifier |
Modifier to be applied to the layout corresponding to the surface |
shape: Shape = RectangleShape |
Defines the surface's shape as well its shadow. |
color: Color = MaterialTheme.colorScheme.surface |
The background color. Use |
contentColor: Color = contentColorFor(color) |
The preferred content color provided by this Surface to its children. Defaults to either the matching content color for |
tonalElevation: Dp = 0.dp |
When |
shadowElevation: Dp = 0.dp |
The size of the shadow below the surface. To prevent shadow creep, only apply shadow elevation when absolutely necessary, such as when the surface requires visual separation from a patterned background. Note that It will not affect z index of the Surface. If you want to change the drawing order you can use |
border: BorderStroke? = null |
Optional border to draw on top of the surface |
content: @Composable () -> Unit |
The content to be displayed on this Surface |
Surface
@Composable
@NonRestartableComposable
fun Surface(
onClick: () -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
shape: Shape = RectangleShape,
color: Color = MaterialTheme.colorScheme.surface,
contentColor: Color = contentColorFor(color),
tonalElevation: Dp = 0.dp,
shadowElevation: Dp = 0.dp,
border: BorderStroke? = null,
interactionSource: MutableInteractionSource? = null,
content: @Composable () -> Unit
): Unit
Material surface is the central metaphor in material design. Each surface exists at a given elevation, which influences how that piece of surface visually relates to other surfaces and how that surface is modified by tonal variance.
This version of Surface is responsible for a click handling as well as everything else that a regular Surface does:
This clickable Surface is responsible for:
-
Clipping: Surface clips its children to the shape specified by
shape -
Borders: If
shapehas a border, then it will also be drawn. -
Background: Surface fills the shape specified by
shapewith thecolor. IfcolorisColorScheme.surfacea color overlay may be applied. The color of the overlay depends on thetonalElevationof this Surface, and theLocalAbsoluteTonalElevationset by any parent surfaces. This ensures that a Surface never appears to have a lower elevation overlay than its ancestors, by summing the elevation of all previous Surfaces. -
Content color: Surface uses
contentColorto specify a preferred color for the content of this surface - this is used by theTextandIconcomponents as a default color. If nocontentColoris set, this surface will try and match its background color to a color defined in the themeColorScheme, and return the corresponding content color. For example, if thecolorof this surface isColorScheme.surface,contentColorwill be set toColorScheme.onSurface. Ifcoloris not part of the theme palette,contentColorwill keep the same value set above this Surface. -
Click handling. This version of surface will react to the clicks, calling
onClicklambda, updating theinteractionSourcewhenandroidx.compose.foundation.interaction.PressInteractionoccurs, and showing ripple indication in response to press events. If you don't need click handling, consider using the Surface function that doesn't requireonClickparam. If you need to set a custom label for theonClick, apply aModifier.semantics { onClick(label = "YOUR_LABEL", action = null) }to the Surface. -
Semantics for clicks. Just like with
Modifier.clickable, clickable version of Surface will produce semantics to indicate that it is clicked. No semantic role is set by default, you may specify one by passing a desiredandroidx.compose.ui.semantics.Rolewith aModifier.semantics.
To manually retrieve the content color inside a surface, use LocalContentColor.
Clickable surface sample:
import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember var count by remember { mutableStateOf(0) } Surface(onClick = { count++ }) { Text("Clickable Surface. Count: $count") }
| Parameters | |
|---|---|
onClick: () -> Unit |
callback to be called when the surface is clicked |
modifier: Modifier = Modifier |
Modifier to be applied to the layout corresponding to the surface |
enabled: Boolean = true |
Controls the enabled state of the surface. When |
shape: Shape = RectangleShape |
Defines the surface's shape as well its shadow. A shadow is only displayed if the |
color: Color = MaterialTheme.colorScheme.surface |
The background color. Use |
contentColor: Color = contentColorFor(color) |
The preferred content color provided by this Surface to its children. Defaults to either the matching content color for |
tonalElevation: Dp = 0.dp |
When |
shadowElevation: Dp = 0.dp |
The size of the shadow below the surface. Note that It will not affect z index of the Surface. If you want to change the drawing order you can use |
border: BorderStroke? = null |
Optional border to draw on top of the surface |
interactionSource: MutableInteractionSource? = null |
an optional hoisted |
content: @Composable () -> Unit |
The content to be displayed on this Surface |
Surface
@Composable
@NonRestartableComposable
fun Surface(
checked: Boolean,
onCheckedChange: (Boolean) -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
shape: Shape = RectangleShape,
color: Color = MaterialTheme.colorScheme.surface,
contentColor: Color = contentColorFor(color),
tonalElevation: Dp = 0.dp,
shadowElevation: Dp = 0.dp,
border: BorderStroke? = null,
interactionSource: MutableInteractionSource? = null,
content: @Composable () -> Unit
): Unit
Material surface is the central metaphor in material design. Each surface exists at a given elevation, which influences how that piece of surface visually relates to other surfaces and how that surface is modified by tonal variance.
This version of Surface is responsible for a toggling its checked state as well as everything else that a regular Surface does:
This toggleable Surface is responsible for:
-
Clipping: Surface clips its children to the shape specified by
shape -
Borders: If
shapehas a border, then it will also be drawn. -
Background: Surface fills the shape specified by
shapewith thecolor. IfcolorisColorScheme.surfacea color overlay may be applied. The color of the overlay depends on thetonalElevationof this Surface, and theLocalAbsoluteTonalElevationset by any parent surfaces. This ensures that a Surface never appears to have a lower elevation overlay than its ancestors, by summing the elevation of all previous Surfaces. -
Content color: Surface uses
contentColorto specify a preferred color for the content of this surface - this is used by theTextandIconcomponents as a default color. If nocontentColoris set, this surface will try and match its background color to a color defined in the themeColorScheme, and return the corresponding content color. For example, if thecolorof this surface isColorScheme.surface,contentColorwill be set toColorScheme.onSurface. Ifcoloris not part of the theme palette,contentColorwill keep the same value set above this Surface. -
Click handling. This version of surface will react to the check toggles, calling
onCheckedChangelambda, updating theinteractionSourcewhenandroidx.compose.foundation.interaction.PressInteractionoccurs, and showing ripple indication in response to press events. If you don't need check handling, consider using a Surface function that doesn't requireonCheckedChangeparam. -
Semantics for toggle. Just like with
Modifier.toggleable, toggleable version of Surface will produce semantics to indicate that it is checked. No semantic role is set by default, you may specify one by passing a desiredandroidx.compose.ui.semantics.Rolewith aModifier.semantics.
To manually retrieve the content color inside a surface, use LocalContentColor.
Toggleable surface sample:
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.ui.text.style.TextAlign var checked by remember { mutableStateOf(false) } Surface( checked = checked, onCheckedChange = { checked = !checked }, color = if (checked) { MaterialTheme.colorScheme.surfaceVariant } else { MaterialTheme.colorScheme.surface }, ) { Text(text = if (checked) "ON" else "OFF", textAlign = TextAlign.Center) }
| Parameters | |
|---|---|
checked: Boolean |
whether or not this Surface is toggled on or off |
onCheckedChange: (Boolean) -> Unit |
callback to be invoked when the toggleable Surface is clicked |
modifier: Modifier = Modifier |
Modifier to be applied to the layout corresponding to the surface |
enabled: Boolean = true |
Controls the enabled state of the surface. When |
shape: Shape = RectangleShape |
Defines the surface's shape as well its shadow. A shadow is only displayed if the |
color: Color = MaterialTheme.colorScheme.surface |
The background color. Use |
contentColor: Color = contentColorFor(color) |
The preferred content color provided by this Surface to its children. Defaults to either the matching content color for |
tonalElevation: Dp = 0.dp |
When |
shadowElevation: Dp = 0.dp |
The size of the shadow below the surface. Note that It will not affect z index of the Surface. If you want to change the drawing order you can use |
border: BorderStroke? = null |
Optional border to draw on top of the surface |
interactionSource: MutableInteractionSource? = null |
an optional hoisted |
content: @Composable () -> Unit |
The content to be displayed on this Surface |
Surface
@Composable
@NonRestartableComposable
fun Surface(
selected: Boolean,
onClick: () -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
shape: Shape = RectangleShape,
color: Color = MaterialTheme.colorScheme.surface,
contentColor: Color = contentColorFor(color),
tonalElevation: Dp = 0.dp,
shadowElevation: Dp = 0.dp,
border: BorderStroke? = null,
interactionSource: MutableInteractionSource? = null,
content: @Composable () -> Unit
): Unit
Material surface is the central metaphor in material design. Each surface exists at a given elevation, which influences how that piece of surface visually relates to other surfaces and how that surface is modified by tonal variance.
This version of Surface is responsible for a selection handling as well as everything else that a regular Surface does:
This selectable Surface is responsible for:
-
Clipping: Surface clips its children to the shape specified by
shape -
Borders: If
shapehas a border, then it will also be drawn. -
Background: Surface fills the shape specified by
shapewith thecolor. IfcolorisColorScheme.surfacea color overlay may be applied. The color of the overlay depends on thetonalElevationof this Surface, and theLocalAbsoluteTonalElevationset by any parent surfaces. This ensures that a Surface never appears to have a lower elevation overlay than its ancestors, by summing the elevation of all previous Surfaces. -
Content color: Surface uses
contentColorto specify a preferred color for the content of this surface - this is used by theTextandIconcomponents as a default color. If nocontentColoris set, this surface will try and match its background color to a color defined in the themeColorScheme, and return the corresponding content color. For example, if thecolorof this surface isColorScheme.surface,contentColorwill be set toColorScheme.onSurface. Ifcoloris not part of the theme palette,contentColorwill keep the same value set above this Surface. -
Click handling. This version of surface will react to the clicks, calling
onClicklambda, updating theinteractionSourcewhenandroidx.compose.foundation.interaction.PressInteractionoccurs, and showing ripple indication in response to press events. If you don't need click handling, consider using the Surface function that doesn't requireonClickparam. -
Semantics for selection. Just like with
Modifier.selectable, selectable version of Surface will produce semantics to indicate that it is selected. No semantic role is set by default, you may specify one by passing a desiredandroidx.compose.ui.semantics.Rolewith aModifier.semantics.
To manually retrieve the content color inside a surface, use LocalContentColor.
Selectable surface sample:
import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.ui.text.style.TextAlign var selected by remember { mutableStateOf(false) } Surface(selected = selected, onClick = { selected = !selected }) { Text(text = if (selected) "Selected" else "Not Selected", textAlign = TextAlign.Center) }
| Parameters | |
|---|---|
selected: Boolean |
whether or not this Surface is selected |
onClick: () -> Unit |
callback to be called when the surface is clicked |
modifier: Modifier = Modifier |
Modifier to be applied to the layout corresponding to the surface |
enabled: Boolean = true |
Controls the enabled state of the surface. When |
shape: Shape = RectangleShape |
Defines the surface's shape as well its shadow. A shadow is only displayed if the |
color: Color = MaterialTheme.colorScheme.surface |
The background color. Use |
contentColor: Color = contentColorFor(color) |
The preferred content color provided by this Surface to its children. Defaults to either the matching content color for |
tonalElevation: Dp = 0.dp |
When |
shadowElevation: Dp = 0.dp |
The size of the shadow below the surface. Note that It will not affect z index of the Surface. If you want to change the drawing order you can use |
border: BorderStroke? = null |
Optional border to draw on top of the surface |
interactionSource: MutableInteractionSource? = null |
an optional hoisted |
content: @Composable () -> Unit |
The content to be displayed on this Surface |