Cornelias
Cornelias

Reputation: 35

Jetpack Compose experimental material API

I am new to Jetpack Compose and following Google's codelabs. I tried some material3 functions and get this error message :

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

And it suggested me to use this option:

@OptIn(ExperimentalMaterial3Api::class)

When I look releases it says it is stable but still got experimental message. How to fix tihs error or above code is mandatory?

The code I tried to use is this:

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.heightIn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.basiclayouts.ui.theme.BasicLayoutsTheme

@Composable
fun SearchBar(
    modifier: Modifier = Modifier
) {
    TextField( // error message 
        value = "",
        onValueChange = {},
        leadingIcon = {
            Icon(
                imageVector = Icons.Default.Search,
                contentDescription = null
            )
        },
        colors = TextFieldDefaults.textFieldColors( // error message
        ),
        placeholder = {
            Text(stringResource(R.string.placeholder_search))
        },
        modifier = modifier
            .fillMaxWidth()
            .heightIn(min = 56.dp)
    )
}

I looked other people who encounter same problem but didn't find solution about it. Also checked Google docs https://developer.android.com/reference/kotlin/androidx/compose/foundation/layout/ExperimentalLayoutApi#ExperimentalLayoutApi() but there is no explanation here as well.

Upvotes: 3

Views: 1463

Answers (1)

dileepkumar67
dileepkumar67

Reputation: 21

Add an annotation either @OptIn(ExperimentalMaterial3Api::class) or @ExperimentalMaterial3Api to your SearchBar composable function. Adding these annotations will resolve your error.

Upvotes: 2

Related Questions