0

I want the editText firstname and the email field editText to be filled in correctly. The validation button which is disabled initially should become enabled (especially for the email address in name@blabla.com format). I tried with textWatcher but I didn't understand how to use it

editText1.addTextChangedListener(object: TextWatcher {
        override fun onTextChanged(s:CharSequence, start:Int, before:Int, count:Int) {
            if (s.toString().trim({ it <= ' ' }).isEmpty())
            {
                button.setEnabled(false)
            }
            else
            {
                button.setEnabled(true)
            }
        }
        override fun beforeTextChanged(s:CharSequence, start:Int, count:Int,
                              after:Int) {
            // TODO Auto-generated method stub
        }
        override fun afterTextChanged(s: Editable) {
            // TODO Auto-generated method stub
        }
    })
Sweta Jain
  • 3,248
  • 6
  • 30
  • 50
zou naa
  • 1
  • 4

2 Answers2

0

You basically need to use, LiveData for the validation checks.

create a dataclass, something like

data class DataCheck(
    val emailError: Int? = null,
    val displaynameError: Int? = null,
    val isDataValid: Boolean = false
)

use LiveData object, say,

private var _dataValidity = MutableLiveData<DataCheck>()
val dataValidity : LiveData<DataCheck>
   get()=_dataValidity

whenever an error occurs assign errors to _dataValidity and observe dataValidity. if no error occurs and after all the variables such as email and name have been checked you can assign isDataValid to true and after that do as you wish.

dataValidity.observe(viewLifecycleOwner, Observer{dataCheck->
     if(dataCheck.emailError!=null){TODO("Handle Email Error Here")}
     if(dataCheck.displaynameError!=null){TODO("Handle Display Name Error Here")}
     if(dataCheck.emailError == null && dataCheck.displayName == null && 
     dataCheck.isDataValid){TODO("What to do when data is valid.")}
})

Use text watcher, and if there are errors assign them to the null values.

Try using the code below, I use this code to verify whether E-mail is valid or not.

//Email validation check
    private fun isEmailValid(email: String): Boolean {
        return if (username.contains('@')) {
            Patterns.EMAIL_ADDRESS.matcher(username).matches()
        } else {
            username.isNotBlank()
        }
    }

    editText1.addTextChangedListener(object: TextWatcher {
        override fun onTextChanged(s:CharSequence, start:Int, before:Int, count:Int) {
            if(isEmailValid(s.toString())){
                Log.d("This", "Valid User Name")
            } else {
                Log.e("This", "Invalid User Name")
            }
        }
        override fun beforeTextChanged(s:CharSequence, start:Int, count:Int,
                                       after:Int) {}
        override fun afterTextChanged(s: Editable) {}
    })
lets start coding
  • 1,839
  • 1
  • 10
  • 19
0

You can use like this as well

editText1.doOnTextChanged {email, _, _, _ -> button.isEnabled = isValidEmail(email}

write a function to validate Email and return boolean

add dependency implementation "androidx.core:core-ktx:1.2.0"

Saba Zafar
  • 31
  • 3