EXPERIMENT 7
AIM: Develop an application that makes use of database.
UserName, Email, Password));
1. RegisterActivity.java Snackbar.make(buttonRegister, "User
created successfully! Please Login ",
Snackbar.LENGTH_LONG).show();
package com.loopwiki.loginregisterwithsqlite; new Handler().postDelayed(new
Runnable() {
import android.os.Bundle; @Override
import android.os.Handler; public void run() {
import android.support.annotation.Nullable; finish();
import android.support.design.widget.Snackbar; }
import android.support.design.widget.TextInputLayout; }, Snackbar.LENGTH_LONG);
import android.support.v7.app.AppCompatActivity; }else {
import android.view.View;
import android.widget.Button; //Email exists with email input provided so
import android.widget.EditText; show error user already exist
import android.widget.TextView; Snackbar.make(buttonRegister, "User
already exists with same email ",
Snackbar.LENGTH_LONG).show();
public class RegisterActivity extends AppCompatActivity }
{ }
}
//Declaration EditTexts });
EditText editTextUserName; }
EditText editTextEmail;
EditText editTextPassword; //this method used to set Login TextView click event
private void initTextViewLogin() {
//Declaration TextInputLayout TextView textViewLogin = (TextView)
TextInputLayout textInputLayoutUserName; findViewById(R.id.textViewLogin);
TextInputLayout textInputLayoutEmail; textViewLogin.setOnClickListener(new
TextInputLayout textInputLayoutPassword; View.OnClickListener() {
@Override
//Declaration Button public void onClick(View view) {
Button buttonRegister; finish();
}
//Declaration SqliteHelper });
SqliteHelper sqliteHelper; }
@Override //this method is used to connect XML views to its
protected void onCreate(@Nullable Bundle Objects
savedInstanceState) { private void initViews() {
super.onCreate(savedInstanceState); editTextEmail = (EditText)
setContentView(R.layout.activity_register); findViewById(R.id.editTextEmail);
sqliteHelper = new SqliteHelper(this); editTextPassword = (EditText)
initTextViewLogin(); findViewById(R.id.editTextPassword);
initViews(); editTextUserName = (EditText)
buttonRegister.setOnClickListener(new findViewById(R.id.editTextUserName);
View.OnClickListener() { textInputLayoutEmail = (TextInputLayout)
@Override findViewById(R.id.textInputLayoutEmail);
public void onClick(View view) { textInputLayoutPassword = (TextInputLayout)
if (validate()) { findViewById(R.id.textInputLayoutPassword);
String UserName = textInputLayoutUserName = (TextInputLayout)
editTextUserName.getText().toString(); findViewById(R.id.textInputLayoutUserName);
String Email = buttonRegister = (Button)
editTextEmail.getText().toString(); findViewById(R.id.buttonRegister);
String Password = }
editTextPassword.getText().toString();
//This method is used to validate input given by user
//Check in the database is there any user public boolean validate() {
associated with this email boolean valid = false;
if (!sqliteHelper.isEmailExists(Email)) {
//Get values from EditText fields
//Email does not exist now add new user to String UserName =
database editTextUserName.getText().toString();
sqliteHelper.addUser(new User(null, String Email = editTextEmail.getText().toString();
String Password = //Declaration EditTexts
editTextPassword.getText().toString(); EditText editTextEmail;
EditText editTextPassword;
//Handling validation for UserName field
if (UserName.isEmpty()) { //Declaration TextInputLayout
valid = false; TextInputLayout textInputLayoutEmail;
textInputLayoutUserName.setError("Please enter TextInputLayout textInputLayoutPassword;
valid username!");
} else { //Declaration Button
if (UserName.length() > 5) { Button buttonLogin;
valid = true;
textInputLayoutUserName.setError(null); //Declaration SqliteHelper
} else { SqliteHelper sqliteHelper;
valid = false;
textInputLayoutUserName.setError("Username @Override
is to short!"); protected void onCreate(Bundle savedInstanceState) {
} super.onCreate(savedInstanceState);
} setContentView(R.layout.activity_login);
sqliteHelper = new SqliteHelper(this);
//Handling validation for Email field initCreateAccountTextView();
if (! initViews();
android.util.Patterns.EMAIL_ADDRESS.matcher(Email).m
atches()) { //set click event of login button
valid = false; buttonLogin.setOnClickListener(new
textInputLayoutEmail.setError("Please enter valid View.OnClickListener() {
email!"); @Override
} else { public void onClick(View view) {
valid = true;
textInputLayoutEmail.setError(null); //Check user input is correct or not
} if (validate()) {
//Handling validation for Password field //Get values from EditText fields
if (Password.isEmpty()) { String Email =
valid = false; editTextEmail.getText().toString();
textInputLayoutPassword.setError("Please enter String Password =
valid password!"); editTextPassword.getText().toString();
} else {
if (Password.length() > 5) { //Authenticate user
valid = true; User currentUser =
textInputLayoutPassword.setError(null); sqliteHelper.Authenticate(new User(null, null, Email,
} else { Password));
valid = false;
textInputLayoutPassword.setError("Password is //Check Authentication is successful or not
to short!"); if (currentUser != null) {
} Snackbar.make(buttonLogin, "Successfully
} Logged in!", Snackbar.LENGTH_LONG).show();
return valid;
} //User Logged in Successfully Launch You
} home screen activity
/* Intent intent=new
Intent(LoginActivity.this,HomeScreenActivity.class);
2. LoginActivity.java startActivity(intent);
finish();*/
package com.loopwiki.loginregisterwithsqlite; } else {
import android.content.Intent; //User Logged in Failed
import android.os.Bundle; Snackbar.make(buttonLogin, "Failed to log
import android.support.design.widget.Snackbar; in , please try again", Snackbar.LENGTH_LONG).show();
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity; }
import android.text.Html; }
import android.text.Spanned; }
import android.view.View; });
import android.widget.Button; }
import android.widget.EditText;
import android.widget.TextView; //this method used to set Create account TextView
text and click event( maltipal colors
public class LoginActivity extends AppCompatActivity { // for TextView yet not supported in Xml so i have
done it programmatically)
private void initCreateAccountTextView() { //Handling validation for Password field
TextView textViewCreateAccount = (TextView) if (Password.isEmpty()) {
findViewById(R.id.textViewCreateAccount); valid = false;
textViewCreateAccount.setText(fromHtml("<font textInputLayoutPassword.setError("Please enter
color='#0c0099'>I don't have account yet.Create valid password!");
One</font>")); } else {
textViewCreateAccount.setOnClickListener(new if (Password.length() > 5) {
View.OnClickListener() { valid = true;
@Override textInputLayoutPassword.setError(null);
public void onClick(View view) { } else {
Intent intent = new Intent(LoginActivity.this, valid = false;
RegisterActivity.class); textInputLayoutPassword.setError("Password is
startActivity(intent); to short!");
} }
}); }
}
return valid;
//this method is used to connect XML views to its }
Objects }
private void initViews() {
editTextEmail = (EditText) OUTPUT:
findViewById(R.id.editTextEmail);
editTextPassword = (EditText)
findViewById(R.id.editTextPassword);
textInputLayoutEmail = (TextInputLayout)
findViewById(R.id.textInputLayoutEmail);
textInputLayoutPassword = (TextInputLayout)
findViewById(R.id.textInputLayoutPassword);
buttonLogin = (Button)
findViewById(R.id.buttonLogin);
//This method is for handling fromHtml method
deprecation
@SuppressWarnings("deprecation")
public static Spanned fromHtml(String html) {
Spanned result;
if (android.os.Build.VERSION.SDK_INT >=
android.os.Build.VERSION_CODES.N) {
result = Html.fromHtml(html,
Html.FROM_HTML_MODE_LEGACY);
} else {
result = Html.fromHtml(html);
}
return result;
}
//This method is used to validate input given by user
public boolean validate() {
boolean valid = false;
//Get values from EditText fields
String Email = editTextEmail.getText().toString();
String Password =
editTextPassword.getText().toString();
//Handling validation for Email field
if (!
android.util.Patterns.EMAIL_ADDRESS.matcher(Email).m
atches()) {
valid = false;
textInputLayoutEmail.setError("Please enter valid
email!");
} else {
valid = true;
textInputLayoutEmail.setError(null);
}