This material API is experimental and is likely to change or to be removed in the future. error in android when using new material3 card
O

4

16

How to set elevation for material3 card? I am using new material3 card and getting error

This material API is experimental and is likely to change or to be removed in the future.

Here is code ->

    @ExperimentalMaterial3Api
    @Composable
    fun ProfileCard(
        modifier: Modifier = Modifier
    ) {
        Card(
            modifier = modifier
                .fillMaxWidth()
                .wrapContentHeight()
                .padding(all = 16.dp),
            shape = RoundedCornerShape(size = 16.dp),
            containerColor = MaterialTheme.colorScheme.surface,
            border = BorderStroke(width = 1.dp, color = MaterialTheme.colorScheme.inverseOnSurface),
            elevation = CardDefaults.outlinedCardElevation()
        ) {
    ...
        }
    }

I am not able to run app due to error caused by elevation how to set elevation?

Edit : Solved the issue by adding

elevation = CardDefaults.outlinedCardElevation(defaultElevation = 1.dp)

Why do we have to add border for CardDefaults.outlinedCardElevation why does't it show by default?

Oysterman answered 10/2, 2022 at 17:34 Comment(1)
Its Material just because of material design principlesLamere
P
15

If you find the same message in the future for another reason, one option could be to add in your build.gradle file, the following:

kotlinOptions {
    allWarningsAsErrors = false
    freeCompilerArgs += [
        '-opt-in=androidx.compose.material3.ExperimentalMaterial3Api'
    ]
}

In this way, after Sync Project with Gradle Files you will not see the warnings in your compose code.

Pt answered 13/9, 2022 at 4:11 Comment(2)
This should be correct answer. And we don't need to add @ExperimentalMaterial3ApiHotheaded
Although this makes the message go away, you should still be aware of the implications. An API marked as experimental can change or be removed at any time and thus they want you to explicitly opt in to use it so that you know that it is experimental. Now, the thing that I don't understand is why JetBrains and Google push Compose and Material 3 so much when a lot of it is experimental. Maybe they should finish their stuff before they release half baked libraries.Harrell
C
8

Update the implementation 'androidx.compose.material3:material3' to version '1.1.1' i.e., implementation 'androidx.compose.material3:material3:1.1.1' and Sync Project with Gradle Files the error will be gone.

Cereus answered 1/8, 2023 at 5:28 Comment(1)
Setting 1.1.1 version is not working for me :( Gradle sync is successfull but TopAppBar contines throwing the same errorMonophyletic
A
6

Try with the @OptIn annotation as below:

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun WoofTopAppBar(modifier: Modifier = Modifier) {
    CenterAlignedTopAppBar(
...
}

Good luck!

Anse answered 9/8, 2023 at 0:49 Comment(0)
Q
1
    @OptIn(ExperimentalMaterial3Api::class)
@Composable
fun WoofTopAppBar(modifier: Modifier = Modifier) {
    CenterAlignedTopAppBar()
}
Quezada answered 22/6 at 17:52 Comment(1)
Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?Excretion

© 2022 - 2024 — McMap. All rights reserved.