Android: I am attaching the same `textChangedListe...
# getting-started
m
Android: I am attaching the same
textChangedListener
to multiple input fields. Currently I have this:
Copy code
withdraw_input.textChangedListener {
    afterTextChanged {
        validateInput()
    }
}

reg_number.textChangedListener {
    afterTextChanged {
        validateInput()
    }
}
I cannot figure out how to extract the listener to a variable and then re-use it, instead of defining the same listener multiple times. I guess it’s just a syntax issue 🤔 Any hints? 🙂
k
Do you happen to use some Kotlin-library for this?
EditTest.textChangedListener
doesn't exist for me.
Maybe it's even a builder, because that
afterTextChanged
doesn't look like function override syntax to me.
m
Don’t think so … I’m not too good at the super short Kotlin syntax yet, so I’m actually not even sure what I’m doing.
k
Can you
ctrl+B
(go to definition) on that function?
m
It’s defined in
ListenersWithCoroutines.kt
k
You're using
anko
then 🙂
m
Oh, that’s an external library?
Didn’t start this project, so I was not aware
k
Yea it's a util library from jetbrains for android and kotlin.
m
Oh, okay 😄
k
So yea, there's some DSL trickery going on.
m
Yep. Probably gonna try a solution without anko, then
Gotta know what’s going on before using the magic tools
k
Copy code
fun textChangedListener(
	context: CoroutineContext = UI,
	init: __TextWatcher.() -> Unit
): android.text.TextWatcher = __TextWatcher(context).apply(init)
If you put that somewhere then you can create a variable
listener = textChangedListener { ... }
and reuse that.
m
What if I want to do it without the coroutines stuff, something like Shawn is suggesting?
so basically an anon inner class
k
Copy code
val listener = object : TextWatcher {
        override fun afterTextChanged(s: Editable?) {}
        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
    }

    editText.addTextChangedListener(listener)
That's going to be ugly.
m
Hmm, yes. I liked the solution I made because I only had to override a single function from TextWatcher
k
Right, that's kinda the point of using anko, makes everything way more elegant.
You can just use the snippet I posted above to do about the same.
m
Yes, I will probably do that, even though I find that code confusing as well
k
You only need the function once, just hide it somewhere in a Util.kt file 😛
If you want to know what's going on read https://kotlinlang.org/docs/reference/type-safe-builders.html
m
Haha, “never speak of it again”, I like that
It says conflicting declarations
val listener: TextWatcher and val listener: TextWatcher.() -> Unit
k
What line gives that error message? Maybe a sceenshot?
m
Oh, never mind, had some other old code that didnt compile
k
Yea that's what I figured 🙂
m
It’s been a long day - I really appreciate your help 🙂
k
My pleasure.
m
Okay, so I’ve defined the listener as you said, but cannot figure out how to attach it to the view 🤔
Oh, just had to call addTextChangedListener. Right, makes sense
Okay, I think I’ve the right solution now, even though I’m still not sure what’s going on. I have an idea, though 😉 Will look at the link you provided. Thanks again for your help! 🙂
k
I'd say read trough the entire reference if you find the time, it's really good.
Good luck!
m
Thanks 🙂