For an application that I'm creating, I wanted to use some Material Design 3 elements, including the OutlinedTextFieldDefaults import but, for some reason, the import isn't registering within Android Studio, which means that the function that uses those references isn't working.
For reference, I'm using the following Android Studio version: Android Studio Giraffe | 2022.3.1 Patch 3
Below will show the import in question and then the block of code where they're needed.
import androidx.compose.material3.OutlinedTextFieldDefaults
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun LyricInputForm(
lyricDetails: LyricDetails,
modifier: Modifier = Modifier,
onValueChange: (LyricDetails) -> Unit = {},
enabled: Boolean = true
) {
Column(
modifier = modifier,
verticalArrangement = Arrangement.spacedBy(dimensionResource(id = R.dimen.padding_medium))
) {
OutlinedTextField(
value = lyricDetails.songName,
onValueChange = { onValueChange(lyricDetails.copy(songName = it)) },
label = { Text(stringResource(R.string.song_name_req)) },
colors = OutlinedTextFieldDefaults.colors(
focusedContainerColor = MaterialTheme.colorScheme.secondaryContainer,
unfocusedContainerColor = MaterialTheme.colorScheme.secondaryContainer
),
modifier = Modifier.fillMaxWidth(),
enabled = enabled,
singleLine = true
)
OutlinedTextField(
value = lyricDetails.artistName,
onValueChange = { onValueChange(lyricDetails.copy(artistName = it)) },
label = { Text(stringResource(R.string.artist_name_req)) },
colors = OutlinedTextFieldDefaults.colors(
focusedContainerColor = MaterialTheme.colorScheme.secondaryContainer,
unfocusedContainerColor = MaterialTheme.colorScheme.secondaryContainer
),
modifier = Modifier.fillMaxWidth(),
enabled = enabled,
singleLine = true
)
OutlinedTextField(
value = lyricDetails.lyricData,
onValueChange = { onValueChange(lyricDetails.copy(lyricData = it)) },
label = { Text(stringResource(R.string.lyric_data_req)) },
colors = OutlinedTextFieldDefaults.colors(
focusedContainerColor = MaterialTheme.colorScheme.secondaryContainer,
unfocusedContainerColor = MaterialTheme.colorScheme.secondaryContainer
),
modifier = Modifier.fillMaxWidth(),
enabled = enabled,
singleLine = true
)
if (enabled) {
Text(
text = stringResource(required_fields),
modifier = Modifier.padding(start = dimensionResource(id = R.dimen.padding_medium))
)
}
}
}
From trying to find a solution, I've found that keeping the @OptIn(ExperimentalMaterial3Api::class) prevents the 'OutlinedTextField' blocks from seeing the Experimental API message that appears, but I can't understand why that import seems to work fine and the other one does not.
EDIT: There are other functions within the same file that are linked to this one, so if they need to be seen as well, I can provide them but at the moment, this is the main issue within the file itself.
You are not using the latest version of the material3 library:
Add or update this dependency in your Gradle file:
implementation 'androidx.compose.material3:material3:1.1.2'