Advertisement
ipungpurwono

Untitled

Jan 28th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.31 KB | None | 0 0
  1. package com.banyucenter.atlasapp;
  2.  
  3. import android.os.Bundle;
  4. import android.os.Handler;
  5. import android.support.annotation.Nullable;
  6. import android.support.design.widget.Snackbar;
  7. import android.support.design.widget.TextInputLayout;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.EditText;
  12. import android.widget.TextView;
  13.  
  14. public class RegisterActivity extends AppCompatActivity {
  15.  
  16.     //Declaration EditTexts
  17.     EditText editTextUserName;
  18.     EditText editTextEmail;
  19.     EditText editTextPassword;
  20.  
  21.     //Declaration TextInputLayout
  22.     TextInputLayout textInputLayoutUserName;
  23.     TextInputLayout textInputLayoutEmail;
  24.     TextInputLayout textInputLayoutPassword;
  25.  
  26.     //Declaration Button
  27.     Button buttonRegister;
  28.  
  29.     //Declaration SqliteHelper
  30.     SqliteHelper sqliteHelper;
  31.  
  32.     @Override
  33.     protected void onCreate(@Nullable Bundle savedInstanceState) {
  34.         super.onCreate(savedInstanceState);
  35.         setContentView(R.layout.activity_register);
  36.         sqliteHelper = new SqliteHelper(this);
  37.         initTextViewLogin();
  38.         initViews();
  39.         buttonRegister.setOnClickListener(new View.OnClickListener() {
  40.             @Override
  41.             public void onClick(View view) {
  42.                 if (validate()) {
  43.                     String UserName = editTextUserName.getText().toString();
  44.                     String Email = editTextEmail.getText().toString();
  45.                     String Password = editTextPassword.getText().toString();
  46.  
  47.                     //Check in the database is there any user associated with  this email
  48.                     if (!sqliteHelper.isEmailExists(Email)) {
  49.  
  50.                         //Email does not exist now add new user to database
  51.                         sqliteHelper.addUser(new User(null, UserName, Email, Password));
  52.                         Snackbar.make(buttonRegister, "User created successfully! Please Login ", Snackbar.LENGTH_LONG).show();
  53.                         new Handler().postDelayed(new Runnable() {
  54.                             @Override
  55.                             public void run() {
  56.                                 finish();
  57.                             }
  58.                         }, Snackbar.LENGTH_LONG);
  59.                     }else {
  60.  
  61.                         //Email exists with email input provided so show error user already exist
  62.                         Snackbar.make(buttonRegister, "User already exists with same email ", Snackbar.LENGTH_LONG).show();
  63.                     }
  64.  
  65.  
  66.                 }
  67.             }
  68.         });
  69.     }
  70.  
  71.     //this method used to set Login TextView click event
  72.     private void initTextViewLogin() {
  73.         TextView textViewLogin = (TextView) findViewById(R.id.textViewLogin);
  74.         textViewLogin.setOnClickListener(new View.OnClickListener() {
  75.             @Override
  76.             public void onClick(View view) {
  77.                 finish();
  78.             }
  79.         });
  80.     }
  81.  
  82.     //this method is used to connect XML views to its Objects
  83.     private void initViews() {
  84.         editTextEmail = (EditText) findViewById(R.id.editTextEmail);
  85.         editTextPassword = (EditText) findViewById(R.id.editTextPassword);
  86.         editTextUserName = (EditText) findViewById(R.id.editTextUserName);
  87.         textInputLayoutEmail = (TextInputLayout) findViewById(R.id.textInputLayoutEmail);
  88.         textInputLayoutPassword = (TextInputLayout) findViewById(R.id.textInputLayoutPassword);
  89.         textInputLayoutUserName = (TextInputLayout) findViewById(R.id.textInputLayoutUserName);
  90.         buttonRegister = (Button) findViewById(R.id.buttonRegister);
  91.  
  92.     }
  93.  
  94.     //This method is used to validate input given by user
  95.     public boolean validate() {
  96.         boolean valid = false;
  97.  
  98.         //Get values from EditText fields
  99.         String UserName = editTextUserName.getText().toString();
  100.         String Email = editTextEmail.getText().toString();
  101.         String Password = editTextPassword.getText().toString();
  102.  
  103.         //Handling validation for UserName field
  104.         if (UserName.isEmpty()) {
  105.             valid = false;
  106.             textInputLayoutUserName.setError("Please enter valid username!");
  107.         } else {
  108.             if (UserName.length() > 5) {
  109.                 valid = true;
  110.                 textInputLayoutUserName.setError(null);
  111.             } else {
  112.                 valid = false;
  113.                 textInputLayoutUserName.setError("Username is to short!");
  114.             }
  115.         }
  116.  
  117.         //Handling validation for Email field
  118.         if (!android.util.Patterns.EMAIL_ADDRESS.matcher(Email).matches()) {
  119.             valid = false;
  120.             textInputLayoutEmail.setError("Please enter valid email!");
  121.         } else {
  122.             valid = true;
  123.             textInputLayoutEmail.setError(null);
  124.         }
  125.  
  126.         //Handling validation for Password field
  127.         if (Password.isEmpty()) {
  128.             valid = false;
  129.             textInputLayoutPassword.setError("Please enter valid password!");
  130.         } else {
  131.             if (Password.length() > 5) {
  132.                 valid = true;
  133.                 textInputLayoutPassword.setError(null);
  134.             } else {
  135.                 valid = false;
  136.                 textInputLayoutPassword.setError("Password is to short!");
  137.             }
  138.         }
  139.         return valid;
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement