32

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!

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Maddy
  • 4,525
  • 4
  • 33
  • 53

6 Answers6

47

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
Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69
25

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

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
3

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

Andrey Danilov
  • 6,194
  • 5
  • 32
  • 56
2
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
)
Ranjeet
  • 393
  • 2
  • 10
0

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!!

Yogendra
  • 4,817
  • 1
  • 28
  • 21
-1
 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.

Mr. Tayyab MuGhal
  • 1,791
  • 2
  • 10
  • 11
  • 3
    Hello! While this code may solve the question, [including an explanation](https://meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Brian61354270 Apr 24 '20 at 13:32