Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 775
Malik Motani Avatar asked Jun 27 '19 11:06

Malik Motani


People also ask

How do you get value from TextField 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.


3 Answers

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
like image 62
Taslim Oseni Avatar answered Oct 16 '22 09:10

Taslim Oseni


With 1.0.x you can 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

like image 20
Gabriele Mariotti Avatar answered Oct 16 '22 10:10

Gabriele Mariotti


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

like image 3
Andrey Danilov Avatar answered Oct 16 '22 10:10

Andrey Danilov