Jetpack Compose: Getting Started

Aug 1 2023 · Kotlin 1.8.10, Android 13, Android Studio Flamingo

Part 3: Jetpack Controls

11. Customize Your TextField

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: Part 2 Quiz: Layout Your App Next episode: 12. Displaying Images

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

You have looked at how to add a TextField Composable to the app. You have also looked at how to use some of the parameters available on the TextField Composable to customize its appearance.

Adding Icons to the TextField

Let us add the leading and trailing icons to the SearchBar TextField.

TextField(
        modifier = Modifier
            .fillMaxWidth(),
        value = "",
        onValueChange = {  },
        leadingIcon = {
            Icon(
                painter = painterResource(id = R.drawable.ic_baseline_search_24),
                contentDescription = "Search Icon",
            )
        },
        leadingIcon = {
          Icon(
              painter = painterResource(id = R.drawable.ic_search),
              contentDescription = "search") },

        trailingIcon = {
          Icon(
              painter = painterResource(id = R.drawable.ic_filter),
              contentDescription = "filter") },

        placeholder = {
          Text(
              text = "Search for food ...",
              modifier = Modifier
                  .fillMaxWidth(),
              textAlign = TextAlign.Center,
          )
        }
)

TextField Colors

You can customize the colors of the TextField using the colors parameter. This parameter is of type TextFieldColors and has a default value of TextFieldDefaults.textFieldColors().

Changing the TextField Colors

Let us change the colors of the TextField.

TextField(
        modifier = Modifier
            .fillMaxWidth(),
        value = "",
        onValueChange = {  },
        leadingIcon = {
            Icon(
                painter = painterResource(id = R.drawable.ic_baseline_search_24),
                contentDescription = "Search Icon",
            )
        },
        leadingIcon = {
          Icon(
              painter = painterResource(id = R.drawable.ic_search),
              contentDescription = "search") },

        trailingIcon = {
          Icon(
              painter = painterResource(id = R.drawable.ic_filter),
              contentDescription = "filter") },

        placeholder = {
          Text(
              text = "Search for food ...",
              modifier = Modifier
                  .fillMaxWidth(),
              textAlign = TextAlign.Center,
          )
        },
        colors = TextFieldDefaults.textFieldColors(
            backgroundColor = Color.White,
            cursorColor = Color.Black,
            placeholderColor = Color.Black,
            // leave for last. delete above
            focusedIndicatorColor = Color.Transparent,
            unfocusedIndicatorColor = Color.Transparent,
            disabledIndicatorColor = Color.Transparent,
        )
)

Conclusion

Our TextField is looking great. We have added icons to the TextField and also changed the colors of the TextField.