2

I was coding along with the help of this video,

https://developer.android.com/codelabs/basic-android-kotlin-compose-using-state?continue=https%3A%2F%2Fdeveloper.android.com%2Fcourses%2Fpathways%2Fandroid-basics-compose-unit-2-pathway-3%23codelab-https%3A%2F%2Fdeveloper.android.com%2Fcodelabs%2Fbasic-android-kotlin-compose-using-state#1

I wrote this composable function,

@Composable
fun EditNumberField() {
    TextField(value = "", onValueChange = {})
}

This TextField() shows red error says

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

I imported this in the beginning of my code,

import androidx.compose.material3.TextField

Please give me some suggestions on how I can fix this error or suggest some alternatives if it's already been replaced by something else.

Mehedi Hasan
  • 121
  • 1
  • 8
  • 1
    You have to add `Experimental` annotations. If you have multiple usages and you want to handle through out the project, refer to this - https://stackoverflow.com/questions/70216206/handling-multiple-experimental-annotations-throughout-an-app – Abhimanyu Apr 22 '23 at 10:55
  • 1
    Yes, I can use that, But I want something stable, not experimental. – Mehedi Hasan Apr 22 '23 at 11:01
  • 1
    Yes, exactly, what is the stable alternative? ... an iPhone text field? – Carlos Saltos Jun 16 '23 at 19:12

3 Answers3

1

There seems to be something seriously wrong with Android Studio Flamingo 2022.2.1. Attempts to create a project all seem to generate TextFields with this problem.

I've managed to work round this way: To the project file build.gradle(app), in the section kotlinOptions, add the line

freeCompilerArgs += "-opt-in=androidx.compose.material3.ExperimentalMaterial3Api" (as above). Then I find I can right click on the error message. From there clicking on Show quick fixes allows the app to build,

  • 1
    OK, but what is the stable alternative? ... there is no way to create an official stable text field with Android using the jetpack compose ?!? – Carlos Saltos Jun 16 '23 at 19:13
  • It does look as if there isn't. I'm sure Android have made some sort of mistake in the current Jetpack release - something as basic as TextField can't always be experimental. I'm waiting for the next release. – John McMillan Jun 26 '23 at 13:09
1

Upgrade dependecy version for Material3 at least to 1.1.0 in build.graddle:

implementation 'androidx.compose.material3:material3:1.1.0'

More info: here

dcorcuera
  • 11
  • 3
0

Try to add to your composable item this @ExperimentalMaterial3Api.
If these warnings bother you, add this

kotlinOptions {
    allWarningsAsErrors = false
    freeCompilerArgs += [
        '-opt-in=androidx.compose.material3.ExperimentalMaterial3Api'
    ]
}
Amjad Alwareh
  • 2,926
  • 22
  • 27