Search code examples
androidandroid-studioandroid-jetpack-composeandroid-jetpack-compose-material3

textFieldColors() deprecated in Jetpack Compose Android


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"
}

Solution

  • 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,
        ),
        ....
    }