user1877082
user1877082

Reputation: 471

Textfields in android

I hope someone can help! I'm fairly new to android and am having a problem with textfields. I have 3 textfields that require the user to enter info, when the register button is clicked a message will let the user know they are registered and redirect them to login activity. My problem is that I want to check if the user has completed all 3 textfields and if not display an error message and allow them to try again. I can display the error message but even when all textfields are not complete if the reg button is clicked it still tells user they are registered and redirects user to the login activity, I have posted my code if someone could tell me were I am going wrong, thanks in advance!

@Override
  public void onClick(View arg0) {
   // TODO Auto-generated method stub
   String data1 = inputName.getText().toString();
        if(data1.trim().equals("")){
            Toast.makeText(RegisterActivity.this, "Please enter your name",
                    Toast.LENGTH_SHORT).show();
}
   String data2 = inputEmail.getText().toString();
        if(data2.trim().equals("")){
            Toast.makeText(RegisterActivity.this, "please enter email address",
                    Toast.LENGTH_SHORT).show();
        }
   String data3 = inputPassword.getText().toString();
        if(data3.trim().equals("")){
            Toast.makeText(RegisterActivity.this, "Please enter a password",
          Toast.LENGTH_SHORT).show();
        }

   if(data1 != null && data2 != null && data3!= null){
   mySQLiteAdapter.insert(data1, data2, data3);
   updateList();
   Toast.makeText(RegisterActivity.this, "You are now registered",
           Toast.LENGTH_SHORT).show();

   startActivity(new Intent(getApplicationContext(), LoginActivity.class));
   inputName.setText("");
   inputEmail.setText("");
   inputPassword.setText("");
  }
        else Toast.makeText(RegisterActivity.this, "Error please try again!",
               Toast.LENGTH_SHORT).show();
  }

   };

Upvotes: 0

Views: 256

Answers (3)

user1877082
user1877082

Reputation: 471

Thanks for your helpful answers, i think i have solved my problem! hope my solution helps someone else!

@Override
  public void onClick(View arg0) {
   // TODO Auto-generated method stub
   String data1 = inputName.getText().toString();
   String data2 = inputEmail.getText().toString();
   String data3 = inputPassword.getText().toString();

   boolean invalid = false;

    if(data1.equals("")){
        invalid = true;
        Toast.makeText(getApplicationContext(), "Username Missing", Toast.LENGTH_SHORT).show();
    }else if(data2.equals("")){
        invalid = true;
        Toast.makeText(getApplicationContext(), "Email ID Missing", Toast.LENGTH_SHORT).show();
    }else if(data3.equals("")){
        invalid = true;
        Toast.makeText(getApplicationContext(), "Password Missing", Toast.LENGTH_SHORT).show();
    }

    if(invalid == false){
        mySQLiteAdapter.insert(data1, data2, data3);
           updateList();
           Toast.makeText(RegisterActivity.this, "You are now registered",
                   Toast.LENGTH_SHORT).show();

        Intent i_register = new Intent(RegisterActivity.this, LoginActivity.class);
        startActivity(i_register);

        inputName.setText("");
        inputEmail.setText("");
        inputPassword.setText("");
        finish();
    }   

   /*mySQLiteAdapter.insert(data1, data2, data3);
   updateList();
   Toast.makeText(RegisterActivity.this, "You are now registered",
           Toast.LENGTH_SHORT).show();

   startActivity(new Intent(getApplicationContext(), LoginActivity.class));*/


  }

   };

Upvotes: 0

StarsSky
StarsSky

Reputation: 6711

You can olso use TextUtils.isEmpty(string)

   if (!TextUtils.isEmpty(data1) && !TextUtils.isEmpty(data2) && !TextUtils.isEmpty(data2)) {
        Log.d(TAG, "All three Strings are not empty or null!");
    }

Upvotes: 0

salezica
salezica

Reputation: 76889

Here:

if(data1 != null && data2 != null && data3!= null)

Those are not null, just empty strings (""). This is always true.

Upvotes: 3

Related Questions