Wex
Wex

Reputation: 1

checking if the text contains a character and if it is the same as other text Android studio kotlin

I want when I click @button to check if there is an '@' sign in EditText @email and if EditText @password and @password2 have identical text.

I know that I can use the TextWatcher to check if a given character exists, but I don't know how it works.

Upvotes: -2

Views: 38

Answers (2)

Dinux
Dinux

Reputation: 1219

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.

Upvotes: 0

Younes Charfaoui
Younes Charfaoui

Reputation: 1171

So let us break the task; in order to see if the @ symbol is present in the text, you can do the following:

val emailEditText = findViewById<EditText>(R.id.email)
val email = emailEditText.text.toString()

if (email.contains("@")) {
    // '@' symbol is present in the email EditText
} else {
    // '@' symbol is not present in the email EditText
}

Now in order to see if password edit texts have the same text, we can do the following:

// you can use data binding here also
val passwordEditText = findViewById<EditText>(R.id.password)
val password = passwordEditText.text.toString()

val password2EditText = findViewById<EditText>(R.id.password2)
val password2 = password2EditText.text.toString()

if (password == password2) {
    // passwords match
} else {
    // passwords do not match
}

So we can combine everything when the user click on your button like that:

val emailEditText = findViewById<EditText>(R.id.email)
val passwordEditText = findViewById<EditText>(R.id.password)
val password2EditText = findViewById<EditText>(R.id.password2)
val button = findViewById<Button>(R.id.button)

button.setOnClickListener {
    
    val email = emailEditText.text.toString()

    if (email.contains("@")) {
        // '@' symbol is present in the email EditText
    } else {
        // '@' symbol is not present in the email EditText
        Toast.makeText(this, "Email should contain '@' symbol", Toast.LENGTH_SHORT).show()
        return@setOnClickListener
    }

    val password = passwordEditText.text.toString()
    val password2 = password2EditText.text.toString()

    if (password == password2) {
        // passwords match
        Toast.makeText(this, "Passwords match", Toast.LENGTH_SHORT).show()
    } else {
        // passwords do not match
        Toast.makeText(this, "Passwords do not match", Toast.LENGTH_SHORT).show()
    }
}

You don't have to use TextWatcher when you want to handle only click events, but if you want to make continuous observing of the EditText, then you should use it!

Upvotes: 0

Related Questions