Reputation: 5043
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
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
have a good day!!
Upvotes: 2
Reputation: 363825
You can use the TextField
.
Something like:
var text by rememberSaveable { mutableStateOf("Text") }
TextField(
value = text,
onValueChange = {
text = it
},
label = { Text("Label") }
)
Additional details:
https://developer.android.com/jetpack/compose/text#enter-modify-text
Upvotes: 29
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
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
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
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