Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the outline color of OutlinedTextField from jetpack compose?

Here is how OutlinedTextField code looks like in jetpack-compose:

OutlinedTextField(
    value = "",
    onValueChange = {},
    label = {Text("Input")}
)

The default color of the outline of this TextField is purple. I want to change the outline color along with the label obviously.

like image 342
Artaza Sameen Avatar asked Mar 03 '21 09:03

Artaza Sameen


People also ask

Is Jetpack Compose ready for production?

Jetpack Compose is stable, ready for production, and continues to add the features you've been asking us for. We've been thrilled to see tens of thousands of apps start using Jetpack Compose in production already and we can't wait to see what you'll build!

Is Jetpack Compose only for Kotlin?

Set the Name, Package name, and Save location as you normally would. Note that, in the Language dropdown menu, Kotlin is the only available option because Jetpack Compose works only with classes written in Kotlin. In the Minimum API level dropdown menu, select API level 21 or higher.

What is remember in Jetpack Compose?

Compose provides remember() , a function you can use to store a single object in memory. During the first composition run, remember() stores the initial value. In each recomposition, remember() returns the stored value so the composable can use it.


3 Answers

The default values used by the OutlinedTextField are defined in the TextFieldDefaults.outlinedTextFieldColors:

focusedBorderColor: Color = MaterialTheme.colors.primary.copy(alpha = ContentAlpha.high),
unfocusedBorderColor: Color = MaterialTheme.colors.onSurface.copy(alpha = ContentAlpha.disabled),

You can change the colors.primary and the colors.onSurface in your theme.

Otherwise you can use something like:

    OutlinedTextField(
        value = "",
        onValueChange = {},
        label = {Text("Input")},
        colors = TextFieldDefaults.outlinedTextFieldColors(
            focusedBorderColor = Green,
            unfocusedBorderColor = Yellow)
    )

enter image description here

like image 171
Gabriele Mariotti Avatar answered Oct 19 '22 03:10

Gabriele Mariotti


@Preview
@Composable
fun TelephoneEditText() {
    val textValue = remember {
        mutableStateOf("")
    }

    OutlinedTextField(
        label = {
            Text(
                text = stringResource(
                    id = R.string.phoneNumber
                ),
                style = TextStyle(
                    color = MaterialTheme.colors.primaryVariant,
                )
            )
        },
        placeholder = {
            Text(
                text = stringResource(id = R.string.phone_placeholder),
                style = TextStyle(
                    color = MaterialTheme.colors.primaryVariant,
                    textAlign = TextAlign.Center
                )
            )
        },
        colors = TextFieldDefaults.outlinedTextFieldColors(
            focusedBorderColor = MaterialTheme.colors.secondary,
            unfocusedBorderColor = MaterialTheme.colors.secondary,
            focusedLabelColor = MaterialTheme.colors.secondary,
            cursorColor = MaterialTheme.colors.primaryVariant
        ),
        keyboardOptions = KeyboardOptions.Default.copy(keyboardType = KeyboardType.Number),
        value = textValue.value,
        onValueChange = { textValue.value = it },
    )
    WhatsAppButton(textValue)
}

Colors.kt

val Yellow500 = Color(0XFFFFDE03)
val Blue700 = Color(0xFF0036FF)
val Pink500 = Color(0xFFf50057)
val Pink700 = Color(0xFFff5983)

val LightColors = lightColors(
    primary = Yellow500,
    primaryVariant = Blue700,
    secondary = Pink500,
    secondaryVariant = Pink700
)

val DarkColors = darkColors(
    primary = Yellow500,
    primaryVariant = Blue700,
    secondary = Pink700
)

enter image description here

like image 45
AlexPes Avatar answered Oct 19 '22 03:10

AlexPes


for 1.0.0 beta-1

OutlinedTextField(
    value = "",
    onValueChange = {},
    label = {Text("Input")},
    color = TextFieldDefaults.outlinedTextFieldColors(
            focusedBorderColor: Color = MaterialTheme.colors.primary.copy(alpha = 
              ContentAlpha.high),
            unfocusedBorderColor: Color = MaterialTheme.colors.onSurface.copy(alpha = 
              ContentAlpha.disabled),
            disabledBorderColor: Color = unfocusedBorderColor.copy(alpha =
              ContentAlpha.disabled),
            errorBorderColor: Color = MaterialTheme.colors.error,
    )
)

Set border colors depends on the situation using above parameter.

like image 43
ming chen Avatar answered Oct 19 '22 01:10

ming chen