I'm using Compose's BasicTextField, and when I drag the cursor, the text doesn't scroll.
var text by remember { mutableStateOf("123456789123456") }
Box(contentAlignment = Alignment.Center) {
BasicTextField(
modifier = Modifier
.width(100.dp)
.height(40.dp)
.background(color = Color.LightGray)
.wrapContentHeight(align = Alignment.CenterVertically),
textStyle = TextStyle.Default.copy(
fontSize = 14.sp, fontWeight = FontWeight.Bold,
textAlign = TextAlign.Center,
),
value = text,
singleLine = true,
onValueChange = { text = it },
)
}
When I drag the cursor, I want the text to scroll along with it. Instead, this is what's happening:
I'm using Compose's BasicTextField, and when I drag the cursor, the text doesn't scroll.
var text by remember { mutableStateOf("123456789123456") }
Box(contentAlignment = Alignment.Center) {
BasicTextField(
modifier = Modifier
.width(100.dp)
.height(40.dp)
.background(color = Color.LightGray)
.wrapContentHeight(align = Alignment.CenterVertically),
textStyle = TextStyle.Default.copy(
fontSize = 14.sp, fontWeight = FontWeight.Bold,
textAlign = TextAlign.Center,
),
value = text,
singleLine = true,
onValueChange = { text = it },
)
}
When I drag the cursor, I want the text to scroll along with it. Instead, this is what's happening:
Share Improve this question edited Mar 4 at 10:26 tyg 16.7k4 gold badges39 silver badges49 bronze badges asked Mar 4 at 2:18 seven yearsseven years 133 bronze badges 5 |1 Answer
Reset to default 1I'm a bit surprised myself that your code does not work, but switching to the new BasicTextField that uses a TextFieldState instead of a String value fixes it:
val state = rememberTextFieldState("123456789123456")
BasicTextField(
modifier = Modifier
.width(100.dp)
.height(40.dp)
.background(color = Color.LightGray)
.wrapContentHeight(align = Alignment.CenterVertically),
textStyle = TextStyle.Default.copy(
fontSize = 14.sp, fontWeight = FontWeight.Bold,
textAlign = TextAlign.Center,
),
state = state,
lineLimits = TextFieldLineLimits.SingleLine,
)
Use state.text
to access the text field's content. It's backed by a MutableState so you can easily observe it for changes in Compose or by using a snapshotFlow
outside of Compose.
.horizontalScroll(rememberScrollState())
in modifier if issue still persists – Megh Lath Commented Mar 4 at 7:46horizontalScroll
help. Using the new BasicTextField as outlined in my answer, however, seems to help. – tyg Commented Mar 4 at 10:19