0

I am creating a simple login form with SharedPreferences.I have put some basic user validation but it is not working. When I enter the username and password and click login button, nothing gets displayed. It should if it is wrong username and password. Below is the code for same.

  val userName = findViewById(R.id.user) as EditText
        val password = findViewById(R.id.pass) as EditText
        val b1 = findViewById(R.id.btn1) as Button

        b1.setOnClickListener {

            if (userName.equals("")  || password.equals(""))
            {
                Toast.makeText(this, "Username or Password blank", Toast.LENGTH_LONG).show()
            }
            else if (userName.equals("John") and password.equals("123"))
            {
                val editor = getSharedPreferences("name", Context.MODE_PRIVATE).edit()
                editor.putString("name", userName.getText().toString())
                editor.apply()
                val intent = Intent(this, Main2Activity::class.java)
                intent.putExtra("name", userName.toString())
                startActivity(intent)

            }

      else
           {
            Toast.makeText(this, "Username or Password Do not Match", Toast.LENGTH_LONG).show()
           }


        }
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
thusy ram
  • 1
  • 4

8 Answers8

1

The matches will be the best option for this kind of validation things

sUsername = userName.text;
if (sUsername.matches("")) {
    Toast.makeText(this, "You did not enter a username", Toast.LENGTH_SHORT).show();
    return;
}

reference question : Check if EditText is empty.

Damodhar
  • 1,219
  • 8
  • 17
0

Try this one

if (userName.text.isNullOrEmpty()){
                userName.error = "Enter username"
            }
            else if (password.text.isNullOrEmpty()){
                password.error = "Enter password"
            }
            else if (userName.text.toString() == "john" && password.text.toString() == "123"){

                val editor = getSharedPreferences("name", Context.MODE_PRIVATE).edit()
                editor.putString("name", userName.getText().toString())
                editor.apply()
                val intent = Intent(this, Main2Activity::class.java)
                intent.putExtra("name", userName.toString())
                startActivity(intent)

            }
Jimale Abdi
  • 2,574
  • 5
  • 26
  • 33
0
b1.setOnClickListener {

            if (userName.text.toString().equals("") || password.text.toString().equals("")) {
                Toast.makeText(this, "Username or Password blank", Toast.LENGTH_LONG).show()
            } else if (userName.text.toString().equals("John") and password.text.toString().equals("123")) {
                 val editor = getSharedPreferences("name", Context.MODE_PRIVATE).edit()
                            editor.putString("name", userName.getText().toString())
                            editor.apply()
                           val intent = Intent(this, Main2Activity::class.java)
                           intent.putExtra("name", userName.toString())
                           startActivity(intent)
            }
            else {
                Toast.makeText(this, "Username or Password Do not Match", Toast.LENGTH_LONG).show()
            }
        }
Subin Babu
  • 1,515
  • 2
  • 24
  • 50
0
b1.setOnClickListener {view ->

       if (userName.text.isNullOrEmpty()  || password.text.isNullOrEmpty()){
            Toast.makeText(this, "Username or Password blank", Toast.LENGTH_LONG).show()
        }else if (userName.text.equals("John") and password.text.equals("123")){
            val editor = getSharedPreferences("name", Context.MODE_PRIVATE).edit()
            editor.putString("name", userName.getText().toString())
            editor.apply()
            val intent = Intent(this, Main2Activity::class.java)
            intent.putExtra("name", userName.toString())
            startActivity(intent)

        }

    }
Atif AbbAsi
  • 5,633
  • 7
  • 26
  • 47
0

I assume you have added

apply plugin: 'kotlin-android-extensions'.

in your app level gradle. Using this there is not need to use findViewById. You can directly access views from layout.

So if you have Edittext with id edit.

then in your code you can directly use that as below

if(edit.text.isEmpty())
{
// Your code
}

or

if(edit.text.equals("")
{
// Your code
}

Hope it helps!

Vir Rajpurohit
  • 1,869
  • 1
  • 10
  • 23
0

This should work

b1.setOnClickListener {
    if (userName.text.isEmpty() || password.text.isEmpty())
    {
        Toast.makeText(this, "Username or Password blank", Toast.LENGTH_LONG).show()
    }
    else if (userName.text == "John" && password.text == "123")
    {
        val editor = getSharedPreferences("name", Context.MODE_PRIVATE).edit()
        editor.putString("name", userName.getText().toString())
        editor.apply()
        val intent = Intent(this, Main2Activity::class.java)
        intent.putExtra("name", userName.toString())
        startActivity(intent)
    }
}
Vlad
  • 7,997
  • 3
  • 56
  • 43
0

You are not converting your Edittext into string you have just assigned the id to your Edittext and it would still be treated as a Edittext field. You have to pass the string value, not a Editext. To convert it into string, your if condition must be like this:

 if (userName.text.toString().trim().equals("")  || password.text.toString().trim().equals("")) {
 Toast.makeText(this, "Username or Password blank", Toast.LENGTH_LONG).show()
            }

Or you can do it while assigning the id, like so:

val uName = userName.text.toString().trim()
val userPass = password.text.toString().trim()

then your if condition will be like this:

if (uName.equals("")  || userPass.equals("")) {
 Toast.makeText(this, "Username or Password blank", Toast.LENGTH_LONG).show()
            }
USMAN osman
  • 952
  • 7
  • 13
-1

Try like the following.

b1.setOnClickListener {
   val name:String = userName.text.toString()
   val pass:String = password.text.toString()

   if (name.trim().length == 0  || pass.trim().length == 0){
         Toast.makeText(this, "Username or Password blank", Toast.LENGTH_LONG).show()
   }
   else if (name.equals("John") and pass.equals("123")){
         val editor = getSharedPreferences("name", Context.MODE_PRIVATE).edit()
         editor.putString("name", name)
         editor.apply()
         val intent = Intent(this, Main2Activity::class.java)
         intent.putExtra("name", name)
         startActivity(intent)

   }
   else{
        Toast.makeText(this, "Username or Password Do not Match", Toast.LENGTH_LONG).show()
   }

}
Jakir Hossain
  • 3,830
  • 1
  • 15
  • 29