Open In App

TextField with Hint Text in Android using Jetpack Compose

Last Updated : 30 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In Jetpack Compose, a TextField is a UI element that lets users type in text as an input. This input can then be stored and used for various desired functions. In General, there is no hint for a TextField. However, we can customize TextField to display hints to the user.

TextField with Hint Text

 

So in this article, we will show you how you could display hints in TextField in Android using Jetpack Compose. Follow the below steps once the IDE is ready.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




package com.geeksforgeeks.jcdecorationbox
  
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material.*
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
  
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
  
            // Creating a Simple Scaffold
            // Layout for the application
            Scaffold(
  
                // Creating a Top Bar
                topBar = { TopAppBar(title = { Text("GFG | Decoration Box", color = Color.White) }, backgroundColor = Color(0xff0f9d58)) },
  
                // Creating Content
                content = {
  
                    // Creating a Column Layout
                    Column(Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) {
  
                        // Creating a variable to store the TextField value
                        var value by remember { mutableStateOf(TextFieldValue("")) }
                          
                          // Creating a Basic TextField bu adding 
                          // innerTextField that will display the Text hint
                          BasicTextField(
                            value = value,
                            onValueChange = { value = it },
                            decorationBox = { innerTextField ->
                                Row(
                                    Modifier
                                        .background(Color.LightGray, RoundedCornerShape(percent = 30))
                                        .padding(16.dp)
                                        .fillMaxWidth()
                                ) {
  
                                    if (value.text.isEmpty()) {
                                        Text("Enter Something...")
                                    }
                                    // <-- Add this
                                    innerTextField() 
                                }
                            },
                        )
                    }
                }
            )
        }
    }
}


Output:

You can see that we are able to display the hint as shown in the below output video.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads