I want that the user can make a list of his hobbies. I dont know how long this list is. So I have a linear layout with an edit text in it and and a button where he can add an edittext in the linearlayout, but i dont like that the user has to click the button everytime he wants to add a hobbie, so i want that a new edit text is added when the user wrote something in the last edit text. My problam is that i dont now how to track when the edit text is no longer empty, to add another edit text
For adding a edit text i have this function:
fun addet() {
val et_hobby = EditText(this)
et_hobby.textSize = 18f
et_hobby.hint = "your Hobbie"
et_hobby.minEms = 3
linearLayout.addView(et_hobby)
etarray.add(et_hobby)
}
and my idea was, that i want to trigger this function, when etarry.last.isNotEmpty is true. I guess i need a listner for that but i dont know which.
Can youn please help me?
You can listen to text changes inside EditText using the following listener:
etarry.last.addTextChangedListener(object :TextWatcher{
override fun afterTextChanged(s: Editable?) {
if(s.toString().trim().length > 0){
addIt();
}else{
//You can remove it if you want!
}
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
})
EDIT 1:
You may also check if you only have one empty EditText after the current one, otherwise, you will add a new EditText every time you write a character in your last EditText