Playing with TextField in Compose — Android Declarative UI
5 min readJul 18, 2021
Since the jetpack compose UI is about to get stable. I thought of exploring and trying to get my hands on it.
In this post, we will be learning the different types of text fields we use in any given project.
While creating this project I am using
- Android Studio Arctic Fox | 2020.3.1 Beta 5
- Compose Version 1.0.0-beta09
Prerequisite
***Must be familiar with the basics of Jetpack Compose***
Here is the list of the following types of Text fields made with Compose UI
Table of Contents
- Basic TextField
- Outline TextField
- Outline TextField with a single line
- Password TextField
- Underline TextField
- TextField with no underline
- Border TextField
- Border TextField with background
- Border TextField with gradient background
- Rounded TextField
- Rounded TextField with Gradient Border
- Trailing Icon TextField with Gradient Border
- Leading Icon TextField with Gradient Border
- CutCornerShape TextField
TextField(
value = text,
modifier = inputModifier,
textStyle = inputTextStyle,
onValueChange = {
onValueChange(it)
}
)
OutlinedTextField(
value = text,
modifier = inputModifier,
textStyle = inputTextStyle,
onValueChange = {
onValueChange(it)
}
)
OutlinedTextField(
value = text,
modifier = inputModifier,
textStyle = inputTextStyle,
onValueChange = {
onValueChange(it)
},
singleLine = true
)
var showPassword by remember { mutableStateOf(false) }
OutlinedTextField(
value = text,
modifier = inputModifier,
textStyle = inputTextStyle,
onValueChange = {
onValueChange(it)
},
visualTransformation = if (showPassword) {
VisualTransformation.None
} else {
PasswordVisualTransformation()
}, trailingIcon = {
if (showPassword) {
IconButton(onClick = { showPassword = false }) {
VectorIcon(imageVector = Icons.Filled.Visibility)
}
} else {
IconButton(onClick = { showPassword = true }) {
VectorIcon(imageVector = Icons.Filled.VisibilityOff)
}
}
}
)
TextField(
value = text,
modifier = inputModifier,
onValueChange = {
onValueChange(it)
},
colors = TextFieldDefaults.textFieldColors(
backgroundColor = Color.Transparent,
),
textStyle = inputTextStyle
)
TextField(
value = text,
onValueChange = {
onValueChange(it)
},
modifier = inputModifier,
colors = TextFieldDefaults.textFieldColors(
backgroundColor = Purple200,
focusedIndicatorColor = Color.Transparent,
unfocusedIndicatorColor = Color.Transparent
),
textStyle = inputTextStyle
)
TextField(
value = text,
onValueChange = {
onValueChange(it)
},
modifier = inputModifier.border(BorderStroke(width = 2.dp, color = Purple500)),
colors = TextFieldDefaults.textFieldColors(
backgroundColor = Color.Transparent,
focusedIndicatorColor = Color.Transparent,
unfocusedIndicatorColor = Color.Transparent
),
textStyle = inputTextStyle
)
TextField(
value = text,
onValueChange = {
onValueChange(it)
},
modifier = inputModifier.border(BorderStroke(width = 2.dp, color = Purple500)),
colors = TextFieldDefaults.textFieldColors(
backgroundColor = Purple200.copy(alpha = 0.5f),
focusedIndicatorColor = Color.Transparent,
unfocusedIndicatorColor = Color.Transparent
),
textStyle = inputTextStyle
)
TextField(
value = text,
onValueChange = {
onValueChange(it)
},
modifier = inputModifier
.border(
BorderStroke(
width = 2.dp,
color = Purple500
)
)
.background(
brush = Brush.horizontalGradient(
listOf(Purple200, Purple700)
)
),
colors = TextFieldDefaults.textFieldColors(
backgroundColor = Purple200.copy(alpha = 0.5f),
focusedIndicatorColor = Color.Transparent,
unfocusedIndicatorColor = Color.Transparent
),
textStyle = inputTextStyle
)
TextField(
value = text,
onValueChange = {
onValueChange(it)
},
modifier = inputModifier.border(
BorderStroke(width = 2.dp, color = Purple500),
shape = RoundedCornerShape(50)
),
colors = TextFieldDefaults.textFieldColors(
backgroundColor = Color.Transparent,
focusedIndicatorColor = Color.Transparent,
unfocusedIndicatorColor = Color.Transparent
),
textStyle = inputTextStyle
)
TextField(
value = text,
onValueChange = {
onValueChange(it)
},
modifier = inputModifier.border(
BorderStroke(
width = 4.dp,
brush = Brush.horizontalGradient(listOf(Purple200, Purple500))
),
shape = RoundedCornerShape(50)
),
colors = TextFieldDefaults.textFieldColors(
backgroundColor = Color.Transparent,
focusedIndicatorColor = Color.Transparent,
unfocusedIndicatorColor = Color.Transparent
),
textStyle = inputTextStyle
)
TextField(
value = text,
onValueChange = {
onValueChange(it)
},
trailingIcon = {
VectorIcon(imageVector = Icons.Default.Clear)
},
modifier = inputModifier.border(
BorderStroke(
width = 4.dp,
brush = Brush.horizontalGradient(listOf(Purple200, Purple500))
),
shape = RoundedCornerShape(50)
),
colors = TextFieldDefaults.textFieldColors(
backgroundColor = Color.Transparent,
focusedIndicatorColor = Color.Transparent,
unfocusedIndicatorColor = Color.Transparent
),
textStyle = inputTextStyle
)
TextField(
value = text,
onValueChange = {
onValueChange(it)
},
leadingIcon = {
VectorIcon(imageVector = Icons.Default.Email)
},
modifier = inputModifier.border(
BorderStroke(
width = 4.dp,
brush = Brush.horizontalGradient(listOf(Purple200, Purple500))
),
shape = RoundedCornerShape(50)
),
colors = TextFieldDefaults.textFieldColors(
backgroundColor = Color.Transparent,
focusedIndicatorColor = Color.Transparent,
unfocusedIndicatorColor = Color.Transparent
),
textStyle = inputTextStyle
)
TextField(
value = text,
onValueChange = {
onValueChange(it)
},
modifier = inputModifier.border(
BorderStroke(
width = 4.dp,
brush = Brush.horizontalGradient(listOf(Purple200, Purple500))
),
shape = CutCornerShape(10)
),
colors = TextFieldDefaults.textFieldColors(
backgroundColor = Color.Transparent,
focusedIndicatorColor = Color.Transparent,
unfocusedIndicatorColor = Color.Transparent
),
textStyle = inputTextStyle
)
Hit 👏 if you have learned something new.You can also check out my other articles.