skip to Main Content
TextField(value = "", onValueChange = {},
    leadingIcon = {
                  Icon( Icons.Default.Search, contentDescription = "")
    },
    placeholder = {
                  Text(text = stringResource(id = R.string.placeholder_search))
    },
    colors = TextFieldDefualts.textFieldColors(),
    modifier = modifier
        .heightIn(min = 56.dp)
        .fillMaxWidth())
}

And also it show error message as "This material API is experimental and is likely to change or to be removed in the future."

TextFieldDefaults.textFieldColors() is deprecated and asked me to move it to in Material3 library and after moving to material3, still it shows textFieldColors() deprecated.. Any help would be a great help

dependencies added in build.gradle

dependencies {
def composeBom = platform('androidx.compose:compose-bom:2023.10.01')
implementation(composeBom)
androidTestImplementation(composeBom)

implementation 'androidx.core:core-ktx:1.12.0'
implementation "androidx.compose.ui:ui"
implementation 'androidx.compose.material3:material3'
implementation 'androidx.compose.material3:material3-window-size-class:1.1.2'
implementation "androidx.compose.material:material-icons-extended"
implementation "androidx.compose.ui:ui-tooling-preview"
implementation "com.google.android.material:material:1.11.0"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.2'
implementation 'androidx.activity:activity-compose:1.8.2'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation "androidx.compose.ui:ui-test-junit4"
debugImplementation "androidx.compose.ui:ui-tooling"
}

2

Answers


  1. Chosen as BEST ANSWER

    As TextFieldDefaults.textFieldColors() deprecated in Material3 library. we need to migrate to the following call for applying colors in Compose TextField as TextFieldDefaults.colors()

    And instead of backgroundColor = as follows :

    `TextField(value = "", onValueChange = {},
        ......
        colors = TextFieldDefaults.textFieldColors(
            backgroundColor = MaterialTheme.colorScheme.color1,
        ),
        ....
    }`
    

    we need to migrate to as follows :

    `TextField(value = "", onValueChange = {},
        ......
        colors = TextFieldDefaults.colors(
            //setting the text field background when it is focused
            focusedContainerColor = MaterialTheme.colorScheme.color1,
    
            //setting the text field background when it is unfocused or initial state
            unfocusedContainerColor = MaterialTheme.colorScheme.color1,
    
            //setting the text field background when it is disabled
            disabledContainerColor = MaterialTheme.colorScheme.color1,
        ),
        ....
    }`
    

    • Remove Redundant Dependency dependency from your build.gradle file.
    implementation "com.google.android.material:material:1.11.0"
    

    It’s not necessary when using Material3.

    • Ensure you’re importing from the Material3 library for TextFieldColors:
    import androidx.compose.material3.TextFieldColors
    

    • Instead of using TextFieldDefaults.textFieldColors(), leverage Material3’s ColorScheme API to create a TextFieldColors instance:
    val colors = MaterialTheme.colorScheme.toTextFieldColors()
    

    See here –

    import androidx.compose.material3.MaterialTheme
    import androidx.compose.material3.TextFieldColors
    // ... other imports
    
    TextField(
       // ... other TextField properties
       colors = MaterialTheme.colorScheme.toTextFieldColors(),
       // ... other TextField properties
    )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search