Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextField with hint text in jetpack compose

I want to create textfield with hint text in jetpackcompose. Any example how create textfield using jectpack? Thanks

like image 513
affan ahmad Avatar asked Nov 15 '19 19:11

affan ahmad


People also ask

What is textfield in jetpack compose?

TextField in Jetpack Compose What's TextField? TextField is a user interface control that is used to allow the user to enter the text. This widget is used to get the data from the user as numbers or text. A simple example of TextField is Login page. We get the username and password using TextField widget. What are options available in TextField?

Does jetpack compose support material design?

Compose is built to support material design principles. Many of its UI elements implement material design out of the box. In this article, we will explain how you can create Material design Text Input Fields using Jetpack Compose.

What are textfield and textfield in compose?

At a higher level, Compose provides Text and TextField , which are composables following Material Design guidelines. It’s recommended to use them as they have the right look and feel for users on Android, and includes other options to simplify their customization without having to write a lot of code.

How do I add a hint to a textfield?

We can just create a function with all parameters of the current TextField and add a hint: String parameter. The pitfall of this approach is we are passing the hint as a string so if we want to style the hint we should add extra parameters to the TextFieldWithHint (e.g hintStyle, hintModifier, hintSoftWrap, ...)


3 Answers

compose_version = '1.0.0-beta07'

Use parameter placeholder to show hint

TextField(value = "", onValueChange = {}, placeholder = { Text("Enter Email") })

Use parameter label to show floating label

TextField(value = "", onValueChange = {}, label = { Text("Enter Email") })
like image 29
Syed Hussain Mehdi Avatar answered Oct 08 '22 06:10

Syed Hussain Mehdi


You can use

  • the label parameter in the TextField composable to display a floating label
  • the placeholder parameter to display a placeholder when the text field is in focus and the input text is empty.

You can also use them together.

Something like:

var text by remember { mutableStateOf("text") }

OutlinedTextField(
        value = text, 
        onValueChange = {
             text = it
        },
        label = { 
           Text("Label") 
        }
)

enter image description here or

TextField(
    value = text, 
    onValueChange = {
         text = it
    },
    label = { 
         Text("Label") 
    },
    placeholder = { 
         Text("Placeholder") 
    }
)

enter image description here

like image 74
Gabriele Mariotti Avatar answered Oct 08 '22 07:10

Gabriele Mariotti


You can create hintTextField in jetpackCompose like below code:

@Composable
fun HintEditText(hintText: @Composable() () -> Unit) {
    val state = state { "" } // The unary plus is no longer needed. +state{""}
    val inputField = @Composable {
        TextField(
            value = state.value,
            onValueChange = { state.value = it }
        )
    }
    if (state.value.isNotEmpty()) {
        inputField()
    } else {
        Layout(inputField, hintText) { measurable, constraints ->
        val inputfieldPlacable = measurable[inputField].first().measure(constraints)
        val hintTextPlacable = measurable[hintText].first().measure(constraints)
        layout(inputfieldPlacable.width, inputfieldPlacable.height) {
                inputfieldPlacable.place(0.ipx, 0.ipx)
                hintTextPlacable.place(0.ipx, 0.ipx)
        } }
    }
}

Call @Compose function like below:

HintEditText @Composable {
                                Text(
                                    text = "Enter Email",
                                    style = TextStyle(
                                        color = Color.White,
                                        fontSize = 18.sp
                                    )
                                )
                            }
like image 12
Anas Mehar Avatar answered Oct 08 '22 06:10

Anas Mehar