If you want to check the conditions when a button is clicked, then you don't have to use TextWatcher. You can use the following code to check if the text in the EditText contains the @
character:
//Access the EditText using its id
val userEmail = findViewById<EditText>(R.id.YOUR_EDITTEXT_ID)
//Get the text entered by the user in the EditText
val userEmailValue = userEmail.text.toString()
//Check if the entered mail id contains the @ character
if (userEmailValue.contains("@")) {
//Code to be executed if @ exists
}
else {
//Code to be executed if @ does not exist
}
Then, if you want to check if the two passwords entered by the user are same, you can use the following code:
//Access the EditTexts as before
val userPswd1 = findViewById<EditText>(R.id.YOUR_PASSWORD_ID1)
val userPswd2 = findViewById<EditText>(R.id.YOUR_PASSWORD_ID2)
//Access the EditText values as before
val userPswdValue1 = userPswd1.text.toString()
val userPswdValue2 = userPswd2.text.toString()
//Check if both the entered passwords are the same
if (userPswdValue1 == userPswdValue2) {
//Code to be executed if both passwords are same
} else {
//Code to be executed if both passwords are not same
}
You can put the above code for checking if @
exists and for checking if the passwords are same in a button click listener, like below:
//Access the Button similar to the EditText
val loginBtn = findViewById<Button>(R.id.YOUR_BUTTON_ID)
//Execute code when the button is clicked
button.setOnClickListener {
//Your code
}
Finally, combining the entire code together:
//Access the Button
val loginBtn = findViewById<Button>(R.id.YOUR_BUTTON_ID)
//Access the EditTexts
val userEmail = findViewById<EditText>(R.id.YOUR_EDITTEXT_ID)
val userPswd1 = findViewById<EditText>(R.id.YOUR_PASSWORD_ID1)
val userPswd2 = findViewById<EditText>(R.id.YOUR_PASSWORD_ID2)
//Execute code when the button is clicked
button.setOnClickListener {
//Get the text entered by the user in the EditText
val userEmailValue = userEmail.text.toString()
//Check if the entered mail id contains the @ character
if (userEmailValue.contains("@")) {
//Code to be executed if @ exists
}
else {
//Code to be executed if @ does not exist
}
//Get the passwords entered by the user
val userPswdValue1 = userPswd1.text.toString()
val userPswdValue2 = userPswd2.text.toString()
//Check if both the entered passwords are the same
if (userPswdValue1 == userPswdValue2) {
//Code to be executed if both passwords are same
}
else {
//Code to be executed if both passwords are not same
}
}
You can TextWatcher if you want to execute the code as soon as the user starts typing in the EditText
.