How to set card elevation in jetpack compose with Material3

23.9k views Asked by At

I've created the simple card in jetpack compose, Here I set elevation but it shows a type mismatch.

Card(
            shape = RoundedCornerShape(20.dp),elevation = 10.dp
        ) {
            Box(modifier = Modifier.height(200.dp)) {
                Image(painter = painter, contentDescription = contentDescription,
                contentScale = ContentScale.Crop)
            }

    }

An image of an error appearing in an editor. The error reads, "Type mismatch. Required: CardElevation. Found: Dp."

4

There are 4 answers

4
Gabriele Mariotti On BEST ANSWER

You are using M3 (androidx.compose.material3) Card and the elevation attribute requires a CardElevation object:

Something like:

Card(
    shape = RoundedCornerShape(20.dp),
    elevation = CardDefaults.cardElevation(
        defaultElevation = 10.dp
    )
)
0
Nikhil Katekhaye On

We can use the following way to give elevation to card in Android compose

Card(elevation = CardDefaults.cardElevation(
    defaultElevation = 10.dp))

And import

import androidx.compose.material3.Card
1
tamegajr On

I was able to use this syntax :

Card (elevation = CardDefaults.cardElevation(10.dp))

1
Oyeinkuro Jesil On
Card(modifier = modifier.fillMaxWidth(),
       shape = RoundedCornerShape(15.dp),
       elevation = CardDefaults.cardElevation(
      defaultElevation = 5.dp))
)