https://kotlinlang.org logo
#compose
Title
# compose
s

sen kumar

02/27/2021, 5:05 AM
How to use TextWatcher - methods -
Copy code
override fun afterTextChanged(editable: Editable?)
in a TextField component in Compose?
d

Dominaezzz

02/27/2021, 11:42 AM
So the simplest thing I can think of is do this.
Copy code
LaunchedEffect(textFieldText) {
    // do afterTextChanged
}
Where
textFieldText
is the variable you pass into TextField.
There are other ways depending on your use case, but I'd need more info on what you're doing.
s

sen kumar

02/27/2021, 6:57 PM
Thanks Dominic - this is good - But LaunchedEffect() gets called for every character I type - So within this LaunchedEffect(text!!) - I need to do an operation after entire text has been entered. so want an equivalent of afterTextChanged() so only after entire text is entered I can get a handle
d

Dominaezzz

02/27/2021, 6:59 PM
How do you know when the text has stopped changing though? You don't really know when the user is done typing.
s

sen kumar

02/27/2021, 6:59 PM
and do my operation only once instead of multiple times as every character gets entered in the Textfield
hmmm.
In conventional case
Copy code
tField!!.addTextChangedListener(object : TextWatcher {
    override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}

    override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {

    }

    override fun afterTextChanged(editable: Editable) {}
})
I think afterTextChanged get called once focus is removed - am I right?
d

Dominaezzz

02/27/2021, 7:01 PM
Ohhhhh, focus removal, there's probably a modifier for that.
(Sorry, haven't done Android in a long time)
s

sen kumar

02/27/2021, 7:02 PM
I understand that we don’t know when the user has stopped typing - I am trying to see if performance can be improved here instead of doing of going thru mutiple times. I will explore focus modifier .
Thanks - Launchedeffect was very helpful
d

Dominaezzz

02/27/2021, 7:19 PM
There's also
snapshotFlow { }
which will allow you to optimise with flow operators.
151 Views