Malik
Malik

Reputation: 5043

How to use EditText or TextInput widget in Jetpack compose?

I was exploring Jetpack compose by trying a few widgets like Image and EditText.

For text input, it has EditableText. I have tried below code but it is not showing anything in UI

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            loadUi()
        }
    }

    @Composable
    fun loadUi() {
        CraneWrapper {
            MaterialTheme {
                val state = +state { EditorState("") }
                EditableText(
                    value = state.value,
                    onValueChange = { state.value = it },
                    editorStyle = EditorStyle(
                        textStyle = TextStyle(
                            fontSize = (50f)
                        )
                    )
                )
            }
        }
    }
}

What I am missing here? Any help would be appreciated!

Upvotes: 36

Views: 46264

Answers (6)

Yogendra
Yogendra

Reputation: 5258

Below are some composable text fields. Please select what are you looking for :

@Composable
fun SimpleFilledTextFieldSample() {
    var text by remember { mutableStateOf("Hello") }
    TextField(
        value = text,
        onValueChange = { text = it },
        label = { Text("Label") }
    )
}


@Composable
fun SimpleOutlinedTextFieldSample() {
    var text by remember { mutableStateOf("") }
    OutlinedTextField(
        value = text,
        onValueChange = { text = it },
        label = { Text("Label") }
    )
}


@Composable
fun StyledTextField() {
    var value by remember { mutableStateOf("Hello\nWorld\nInvisible") }

    TextField(
        value = value,
        onValueChange = { value = it },
        label = { Text("Enter text") },
        maxLines = 2,
        textStyle = TextStyle(color = Color.Blue, fontWeight = FontWeight.Bold),
        modifier = Modifier.padding(20.dp)
    )
}


@Composable
fun PasswordTextField() {
    var password by rememberSaveable { mutableStateOf("") }
    TextField(
        value = password,
        onValueChange = { password = it },
        label = { Text("Enter password") },
        visualTransformation = PasswordVisualTransformation(),
        keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password)
    )
}

Read More :

Compose Text >> https://developer.android.com/jetpack/compose/text

Videos >> https://www.youtube.com/watch?v=_qls2CEAbxI&ab_channel=AndroidDevelopers

Output: enter image description here

enter image description here

have a good day!!

Upvotes: 2

Gabriele Mariotti
Gabriele Mariotti

Reputation: 363825

You can use the TextField.

Something like:

var text by rememberSaveable { mutableStateOf("Text") }

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

enter image description here

Additional details:

https://developer.android.com/jetpack/compose/text#enter-modify-text

Upvotes: 29

Taslim Oseni
Taslim Oseni

Reputation: 6263

As stated in Gabriele Mariotti's answer, this is the right way to do it:

var text by rememberSaveable { mutableStateOf("Text") }

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

If however, you encounter an error that states:

Type 'TypeVariable(T)' has no method 'getValue(MainActivity, KProperty<*>)' and thus it cannot serve as a delegate

Simply add these two imports to your file and you'd be good:

import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue

Upvotes: 51

Mr. Tayyab MuGhal
Mr. Tayyab MuGhal

Reputation: 2331

 val state = +state {
                    EditorModel("Edit Text")
                }
                TextField(
                    value = state.value,
                    onValueChange = {
                        state.value = it
                    },
                    textStyle = TextStyle(
                        fontSize = (30.sp),
                        color = Color.DarkGray
                    ),
                    keyboardType = KeyboardType.Text
                )

Try this one.

Upvotes: -1

Ranjeet
Ranjeet

Reputation: 403

TextField(
    value = state.value,
    onValueChange = { new ->
        state.value = if (new.text.any { it == '\n' }) {
            state.value
        } else {
            new
        }
    },
    keyboardType = KeyboardType.Text,
    imeAction = ImeAction.Search,
    textStyle = TextStyle(color = Color.DarkGray),
    onImeActionPerformed = onImeActionPerformed
)

Upvotes: 2

Andrey Danilov
Andrey Danilov

Reputation: 6592

Sorry for late answer. API was changed a bit, so your code now should look like this:

@Composable
fun loadUi() {
    val state = +state { EditorModel("smth") }
    TextField(
        value = state.value,
        onValueChange = { state.value = it },
        editorStyle = EditorStyle(
            textStyle = TextStyle(
                fontSize = (50.sp)
            )
        )
    )
}

Also you could miss widget, because it doesnt have default background and is almost invisible by default if you have empty string

Upvotes: 3

Related Questions