import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.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;
public class Login extends AppCompatActivity {
EditText Lname, Password;
TextView Regit;
Button Login, Cancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Lname = (EditText) findViewById(R.id.txtLname);
Password = (EditText) findViewById(R.id.txtPassword);
Login = (Button) findViewById(R.id.btnLogin);
Cancel = (Button) findViewById(R.id.btnCancel);
Regit = (TextView) findViewById(R.id.txtRegit);
Regit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Login.this, Registration.class);
startActivity(intent);
}
});
Cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Lname.setText("");
Password.setText("");
}
});
Login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
load();
}
});
}
public void run() {
Intent x = new Intent(Login.this, MainPage.class);
startActivity(x);
}
public void LoginSuccess() {
Toast.makeText(this, "Successfully Logged in!", Toast.LENGTH_SHORT).show();
run();
}
public void load(){
final SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(getBaseContext());
SharedPreferences.Editor editor = pref.edit();
String value=(pref.getString("Name", Lname.getText().toString()));
String value2=(pref.getString("Password", Password.getText().toString()));
if (value.equals(Lname) & value2.equals(Password)){
LoginSuccess();
} else{
Toast.makeText(this, "Last Name or Password Incorrect or Does not Exist!", Toast.LENGTH_SHORT).show();
}
}
}
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.regex.Pattern;
public class Registration extends AppCompatActivity {
EditText Lname, Fname, Mname, BDate, Email, Password;
String Lnamee, Fnamee, Mnamee, Bdatee, Emails, Passwords;
TextView Login;
Button Register, Cancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
Lname = (EditText) findViewById(R.id.txtFamilyName);
Fname = (EditText) findViewById(R.id.txtFirstName);
Mname = (EditText) findViewById(R.id.txtMiddleName);
BDate = (EditText) findViewById(R.id.txtBDay);
Email = (EditText) findViewById(R.id.txtEMail);
Password = (EditText) findViewById(R.id.txtPassword);
Register = (Button) findViewById(R.id.btnRegister);
Cancel = (Button) findViewById(R.id.btnCancel);
Login = (TextView) findViewById(R.id.txtLogin);
Login.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent = new Intent(Registration.this, Login.class);
startActivity(intent);
}
});
Cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Lname.setText("");
Fname.setText("");
Mname.setText("");
BDate.setText("");
Email.setText("");
Password.setText("");
}
});
Register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
register();
}
});
}
public void register() {
initialize();
if (!validate()) {
Toast.makeText(this, "Sign-up has Failed", Toast.LENGTH_SHORT).show();
} else {
onSignUpSuccess();
}
}
public void onSignUpSuccess() {
save();
Toast.makeText(this, "Successfully Registered!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Registration.this, Login.class);
startActivity(intent);
}
public boolean validate() {
boolean valid = true;
if (Lnamee.isEmpty() || Pattern.compile("[a-zA-Z]*+").matcher(Lnamee).matches()) {
Lname.setError("Enter letters only!");
valid = false;
}
if (Mnamee.isEmpty() || Pattern.compile("[a-zA-Z]*+ ").matcher(Mnamee).matches()) {
Mname.setError("Enter letters only!");
valid = false;
}
if (Fnamee.isEmpty() || Pattern.compile("[a-zA-Z]*+").matcher(Fnamee).matches()) {
Fname.setError("Enter letters only!");
valid = false;
}
if (Emails.isEmpty() || Pattern.compile("[a-zA-Z0-9]" + "\\@" + "[a-zA-Z]" + "\\." + "[a-zA-Z]").matcher(Emails).matches()){
Email.setError("Enter valid e-mail address!");
valid = false;
}
if (Passwords.isEmpty() || Passwords.length() < 8){
Password.setError("Password must be 8 characters!");
valid = false;
}
return valid;
}
public void initialize(){
Lnamee = Lname.getText().toString().trim();
Mnamee = Mname.getText().toString().trim();
Fnamee = Fname.getText().toString().trim();
Emails = Email.getText().toString().trim();
Passwords = Password.getText().toString().trim();
}
public void save() {
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = pref.edit();
String LastNameReg = Lname.getText().toString();
String PWReg = Password.getText().toString();
editor.putString("Name", LastNameReg);
editor.putString("Password", PWReg);
editor.commit();
}
}
The expected behavior of the program would be, from the registration: when the user enters the Family Name or Last Name(txtFamilyName) & Password(txtPassword) it will be stored in sharedpreferences, and it will be use as a data to be entered for user to login(Login.class) and enter MainPage().
During registration(Registration.class), when I entered my Family or Last Name & Password, and use it on the Login.class, it does not enter to MainPage even though the required fields are correct.
It occurs to me that you are comparing the value of an EditView
object with the actual username / password strings. You also don't want to be using the Lname and Password values as "default" values. In this case, if there is no username / password saved it will always identify as logged in.
The changes made below should suffice.
public void load() {
final SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(getBaseContext());
SharedPreferences.Editor editor = pref.edit();
String value=(pref.getString("Name", ""));
String value2=(pref.getString("Password", ""));
if (value.equals(Lname.getText().toString()) & value2.equals(Password.getText().toString())){
LoginSuccess();
} else{
Toast.makeText(this, "Last Name or Password Incorrect or Does not Exist!", Toast.LENGTH_SHORT).show();
}
}