Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add border on one side of the card using jetpack compose

If i use

 .border(BorderStroke(1.dp, color = purple))

then the entire card gets a border on all 4 sides. I want the border to be only on the left side of the card. How can i achieve this?

like image 909
MChak Avatar asked Nov 04 '25 03:11

MChak


2 Answers

For the Card() you could use Modifier.drawBehind() like this

    val cornerRadius = 16.dp

    Card(
        shape = RoundedCornerShape(cornerRadius),
        backgroundColor = backgroundColor,
        modifier = Modifier.padding(8.dp).drawBehind {

            //Bottom line
            drawLine(
                color = Color.Cyan,
                start = Offset(x = cornerRadius.toPx(), y = size.height),
                end = Offset(x = size.width - cornerRadius.toPx(), y = size.height),
                strokeWidth = 1.dp.toPx()
            )

            //Bottom-left corner
            drawArc(
                color = Color.Cyan,
                startAngle = 180f,
                sweepAngle = -90f,
                useCenter = false,
                topLeft = Offset(x = 0f, y = size.height - cornerRadius.toPx() * 2),
                size = Size(cornerRadius.toPx() * 2, cornerRadius.toPx() * 2),
                style = Stroke(width = 1.dp.toPx())
            )

            //Bottom-right corner
            drawArc(
                color = Color.Cyan,
                startAngle = 360f,
                sweepAngle = 90f,
                useCenter = false,
                topLeft = Offset(x = size.width - cornerRadius.toPx() * 2, y = size.height - cornerRadius.toPx() * 2),
                size = Size(cornerRadius.toPx() * 2, cornerRadius.toPx() * 2),
                style = Stroke(width = 1.dp.toPx())
            )

        }
    ) {
        Box(modifier = Modifier.padding(16.dp)) {
            content()
        }
    }

enter image description here

like image 74
Dmitri Chernysh Avatar answered Nov 06 '25 20:11

Dmitri Chernysh


You can use Modifier.drawwithContent and draw a line with offset as half of the stroke width to move start position from out of composable to 0,0 position

fun Modifier.drawOneSideBorder(
    width: Dp,
    color: Color,
    shape: Shape = RectangleShape
) = this
    .clip(shape)
    .drawWithContent {
        val widthPx = width.toPx()
        drawContent()
        drawLine(
            color = color,
            start = Offset(widthPx / 2, 0f),
            end = Offset(widthPx / 2, size.height),
            strokeWidth = widthPx
        )
    }

Usage

fun Modifier.drawOneSideBorder(
    width: Dp,
    color: Color,
    shape: Shape = RectangleShape
) = this
    .clip(shape)
    .drawWithContent {
        val widthPx = width.toPx()
        drawContent()
        drawLine(
            color = color,
            start = Offset(widthPx / 2, 0f),
            end = Offset(widthPx / 2, size.height),
            strokeWidth = widthPx
        )
    }

Result

enter image description here

like image 36
Thracian Avatar answered Nov 06 '25 18:11

Thracian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!