0

According to program when i enter the correct credentials and click the log in button it should open the another activity and if i enter the wrong credentials it will show a toast message "Incorrect Credentials", but even if i write the correct credentials it still display toast message instead of redirecting to another activity`

`

package com.shivanshu.activitylifecycle

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast

class NewActivity : AppCompatActivity(){

    lateinit var etPhoneNumber: EditText
    lateinit var etPassword : EditText
    lateinit var btnLogIn: Button
    lateinit var txtForgotPassword: TextView
    lateinit var txtRegisterYourself: TextView
    val validPassword = "8743950822"
    val validPhoneNumber = "Tony"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_new)

        title = "Log in"

        etPhoneNumber = findViewById(R.id.etPhoneNumber)
        etPassword = findViewById(R.id.etPassword)
        btnLogIn = findViewById(R.id.btnLogIn)
        txtForgotPassword = findViewById(R.id.txtForgotPassword)
        txtRegisterYourself = findViewById(R.id.txtRegisterYourself)

        btnLogIn.setOnClickListener{
            val password = etPassword.getText().toString()
            val phoneNumber = etPhoneNumber.getText().toString()

            if((phoneNumber == validPhoneNumber) && (password == validPassword)){
                val intent = Intent(this@NewActivity,MainActivity::class.java)
                startActivity(intent)
            }else {
                Toast.makeText(
                    this@NewActivity,
                    "Incorrect Credentials",
                    Toast.LENGTH_LONG
                ).show()

            }
        }
    }

}
Shivanshu
  • 1
  • 1
  • See the link at the top for the correct way to compare two strings for equality. – Code-Apprentice Mar 22 '22 at 18:00
  • ugg...nvm, that might not be right. Have you tried to debug your code to see why the `if` condition is always false? – Code-Apprentice Mar 22 '22 at 18:02
  • 1
    The code looks fine. Perhaps debugging this would let you know which condition is failing. Why dont you use .equals() method to see if that works – Mehroze Yaqoob Mar 22 '22 at 18:05
  • Oh!. I think the problem might be that phoneNumber has extra space at the end. Replace these 2 lines val password = etPassword.getText().toString() val phoneNumber = etPhoneNumber.getText().toString() with this val password = etPassword.text.trim() val phoneNumber = etPhoneNumber.text.trim() – Mehroze Yaqoob Mar 22 '22 at 18:08
  • @MehrozeYaqoob tried what you suggest but problem still not solved – Shivanshu Mar 22 '22 at 18:21
  • Read [this article](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) for tips on debugging. Also, I suggest using the debugger in Android Studio to set a breakpoint and inspect values of your variables to see what is going wrong. – Code-Apprentice Mar 22 '22 at 19:33
  • @Code-Apprentice thanks bro your Debugger idea works i made a silly mistake i initialise validPassword with number i.e 8743950822 – Shivanshu Mar 23 '22 at 08:47

0 Answers0