I’m facing very good problem :internet-problems: ...
# compose
j
I’m facing very good problem internet problems I’ve AndroidView which contains EditText. I’m not hoisting the state instead letting editText hold its own state. But sometimes I want to set text from out side, lets say I want to undo the last text so how do I do it?
b
pass in
text
and key the remembered internal one off the provided
text
so it recalculates when its changed.
j
It doesn't work, when I try to set a text in update block of AndroidView in editText using setText method it adds text at first position and a cursor remains at 0 index always
l
I guess it’s an issue with
EditText.setText()
itself. Using it to set text doesn’t alter cursor position and we then explicitly have to call
EditText.setSelection()
to reposition it at the end. There’s no alternative I can think of
Copy code
@Composable
fun EditText(
    text: String,
    modifier: Modifier = Modifier
) {

    AndroidView(
        modifier = modifier,
        factory = {
            EditText(it)
        },
        update = {
            it.setText(text)
            it.setSelection(it.length())
        }
    )
}
There is an alternative
EditText.append()
(inheriting from the extended
TextView
) which appends to the existing and places the cursor properly. But that’s only “append” and not “set”. So won’t recommend it anyways 😅
j
okay let me check this one