Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Jetpack compose (1.0.0-beta07): TextField - None of the following functions can be called with the arguments supplied

I started working with Jetpack compose (1.0.0-beta07) and I ran into a pretty strange problem with TextField. According to all possible documentation and instructions, I do everything right, but Android Studio constantly writes me the message None of the following functions can be called with the arguments supplied. for TextField

Below is my written code, where Studio still underlines Text (label) and text = it, but I take it that it has a problem defining TextField. The problem disappears when I replace remember {mutableStateOf ("text")} with "text", but TextField does not change the text when typing the keyboard.

import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.text.input.KeyboardType

@Composable
fun SimpleTextField(label: String = "Label", key: String = "unknown", keyboardType: KeyboardType = KeyboardType.Text){
    var text = remember {
        mutableStateOf("text")
    }

    TextField(
        value = text,
        onValueChange = {
            text = it
        },
        label = { Text(label) },
        keyboardOptions = KeyboardOptions(keyboardType = keyboardType)
    )
}

Image of error

like image 310
Honza Rössler Avatar asked May 20 '21 09:05

Honza Rössler


People also ask

How do I get TextField value in jetpack compose?

To read value entered in TextField in Android Compose, declare a variable, and assign this variable the value in the TextField whenever there is a change in the value. The same process holds for reading value entered in OutlineTextField composable.

Does jetpack compose use Recyclerview?

Jetpack compose is a modern tool kit building native UIs for Android using Kotlin. Directly in jetpack compose does not support recycler view like the android app.

What is scaffold Android jetpack compose?

A Scaffold is a layout which implements the basic material design layout structure. You can add things like a TopBar, BottomBar, FAB or a Drawer.

Does jetpack compose use views?

Jetpack Compose is designed to work with the established View-based UI approach. If you're building a new app, the best option might be to implement your entire UI with Compose. But if you're modifying an existing app, you might not want to fully migrate your app all at once.


1 Answers

You can use:

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

TextField(
    value = text.value,
    onValueChange = {
        text.value = it
    },
    label = { Text(label) },
    keyboardOptions = KeyboardOptions(keyboardType = keyboardType)
)

or:

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

TextField(
    value = text,
    onValueChange = {
        text = it
    },
    label = { Text(label) },
    keyboardOptions = KeyboardOptions(keyboardType = keyboardType)
)

You can read more info about the delegated properties in the official doc.

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

Gabriele Mariotti