{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

BackgroundKt

public final class BackgroundKt


Summary

Public methods

static final @NonNull Modifier
background(
    @NonNull Modifier receiver,
    @NonNull Color color,
    @NonNull Shape shape
)

Draws shape with a solid color behind the content.

static final @NonNull Modifier
background(
    @NonNull Modifier receiver,
    @NonNull Brush brush,
    @NonNull Shape shape,
    float alpha
)

Draws shape with brush behind the content.

Public methods

background

public static final @NonNull Modifier background(
    @NonNull Modifier receiver,
    @NonNull Color color,
    @NonNull Shape shape
)

Draws shape with a solid color behind the content.

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Text
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp

Text(
    "Text with background",
    Modifier.background(color = Color.Magenta).padding(10.dp)
)
Parameters
@NonNull Color color

color to paint background with

@NonNull Shape shape

desired shape of the background

background

public static final @NonNull Modifier background(
    @NonNull Modifier receiver,
    @NonNull Brush brush,
    @NonNull Shape shape,
    float alpha
)

Draws shape with brush behind the content.

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.CutCornerShape
import androidx.compose.material.Text
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp

val gradientBrush = Brush.horizontalGradient(
    colors = listOf(Color.Red, Color.Blue, Color.Green),
    startX = 0.0f,
    endX = 500.0f
)
Text(
    "Text with gradient back",
    Modifier.background(brush = gradientBrush, shape = CutCornerShape(8.dp))
        .padding(10.dp)
)
Parameters
@NonNull Brush brush

brush to paint background with

@NonNull Shape shape

desired shape of the background

float alpha

Opacity to be applied to the brush, with 0 being completely transparent and 1 being completely opaque. The value must be between 0 and 1.