Good Afternoon, I want to trigger my button to change colours after there is text in my editText field im wondering how do you do this check constantly, instead of the one time the fragment boots up?
My fragment code
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
navc = Navigation.findNavController(view)
registerObservers()
setListeners(view)
onTextChanged()
}
override fun onCreate(savedInstanceState: Bundle?) {
setHasOptionsMenu(true)
super.onCreate(savedInstanceState)
requireActivity().onBackPressedDispatcher.addCallback(this) {
navc!!.navigate(R.id.orderListFragment)
}
}
private fun registerObservers() {
accountViewModel.profile.observe(viewLifecycleOwner) {
}
}
private fun onTextChanged() {
if (restaurantEmail.editableText.isNotEmpty()) {
sendHtmlcode.backgroundTintList = ColorStateList.valueOf(resources.getColor(R.color.standardOrange))
}else{
println("Testingprint")
sendHtmlcode.backgroundTintList = ColorStateList.valueOf(resources.getColor(R.color.standardGrey))
}
It colours up the first time without text but after that it doesnt check again and I dont understand why not
CodePudding user response:
You can use doOnTextChanged
function which trigger everytime text in EditText changes. It internally uses a TextWatcher
.
editText.doOnTextChanged { text, start, before, count ->
onTextChanged()
}
CodePudding user response:
you can use doOnTextChange
this is a kotlin extension fuction of textView.
import androidx.core.widget.doOnTextChanged
yourEditText.doOnTextChange { text, start, before, count ->
// here you check if the input is empty
onTextChanged(text.isNotEmpty())
}
private fun onTextChanged(isNotEmpty: Boolean) {
if (isNotEmpty) {
sendHtmlcode.backgroundTintList = ColorStateList.valueOf(resources.getColor(R.color.standardOrange))
}else{
println("Testingprint")
sendHtmlcode.backgroundTintList = ColorStateList.valueOf(resources.getColor(R.color.standardGrey))
}
}