Advertisement
ipungpurwono

Untitled

Jan 28th, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.26 KB | None | 0 0
  1. package com.banyucenter.atlasapp;
  2.  
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.support.design.widget.Snackbar;
  6. import android.support.design.widget.TextInputLayout;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.text.Html;
  9. import android.text.Spanned;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.EditText;
  13. import android.widget.TextView;
  14.  
  15. public class LoginActivity extends AppCompatActivity {
  16.  
  17.     //Declaration EditTexts
  18.     EditText editTextEmail;
  19.     EditText editTextPassword;
  20.  
  21.     //Declaration TextInputLayout
  22.     TextInputLayout textInputLayoutEmail;
  23.     TextInputLayout textInputLayoutPassword;
  24.  
  25.     //Declaration Button
  26.     Button buttonLogin;
  27.  
  28.     //Declaration SqliteHelper
  29.     SqliteHelper sqliteHelper;
  30.  
  31.     @Override
  32.     protected void onCreate(Bundle savedInstanceState) {
  33.         super.onCreate(savedInstanceState);
  34.         setContentView(R.layout.activity_login);
  35.         sqliteHelper = new SqliteHelper(this);
  36.         initCreateAccountTextView();
  37.         initViews();
  38.  
  39.         //set click event of login button
  40.         buttonLogin.setOnClickListener(new View.OnClickListener() {
  41.             @Override
  42.             public void onClick(View view) {
  43.  
  44.                 //Check user input is correct or not
  45.                 if (validate()) {
  46.  
  47.                     //Get values from EditText fields
  48.                     String Email = editTextEmail.getText().toString();
  49.                     String Password = editTextPassword.getText().toString();
  50.  
  51.                     //Authenticate user
  52.                     User currentUser = sqliteHelper.Authenticate(new User(null, null, Email, Password));
  53.  
  54.                     //Check Authentication is successful or not
  55.                     if (currentUser != null) {
  56.                         Snackbar.make(buttonLogin, "Successfully Logged in!", Snackbar.LENGTH_LONG).show();
  57.  
  58.                         //User Logged in Successfully Launch You home screen activity
  59.                         Intent intent=new Intent(LoginActivity.this,MainActivity.class);
  60.                         startActivity(intent);
  61.                         finish();
  62.  
  63.                     } else {
  64.  
  65.                         //User Logged in Failed
  66.                         Snackbar.make(buttonLogin, "Failed to log in , please try again", Snackbar.LENGTH_LONG).show();
  67.  
  68.                     }
  69.                 }
  70.             }
  71.         });
  72.     }
  73.  
  74.     //this method used to set Create account TextView text and click event( maltipal colors
  75.     // for TextView yet not supported in Xml so i have done it programmatically)
  76.     private void initCreateAccountTextView() {
  77.         TextView textViewCreateAccount = (TextView) findViewById(R.id.textViewCreateAccount);
  78.         textViewCreateAccount.setText(fromHtml("<font color='#ffffff'>I don't have account yet. </font><font color='#0c0099'>create one</font>"));
  79.         textViewCreateAccount.setOnClickListener(new View.OnClickListener() {
  80.             @Override
  81.             public void onClick(View view) {
  82.                 Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
  83.                 startActivity(intent);
  84.             }
  85.         });
  86.     }
  87.  
  88.     //this method is used to connect XML views to its Objects
  89.     private void initViews() {
  90.         editTextEmail = (EditText) findViewById(R.id.editTextEmail);
  91.         editTextPassword = (EditText) findViewById(R.id.editTextPassword);
  92.         textInputLayoutEmail = (TextInputLayout) findViewById(R.id.textInputLayoutEmail);
  93.         textInputLayoutPassword = (TextInputLayout) findViewById(R.id.textInputLayoutPassword);
  94.         buttonLogin = (Button) findViewById(R.id.buttonLogin);
  95.  
  96.     }
  97.  
  98.     //This method is for handling fromHtml method deprecation
  99.     @SuppressWarnings("deprecation")
  100.     public static Spanned fromHtml(String html) {
  101.         Spanned result;
  102.         if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
  103.             result = Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY);
  104.         } else {
  105.             result = Html.fromHtml(html);
  106.         }
  107.         return result;
  108.     }
  109.  
  110.     //This method is used to validate input given by user
  111.     public boolean validate() {
  112.         boolean valid = false;
  113.  
  114.         //Get values from EditText fields
  115.         String Email = editTextEmail.getText().toString();
  116.         String Password = editTextPassword.getText().toString();
  117.  
  118.         //Handling validation for Email field
  119.         if (!android.util.Patterns.EMAIL_ADDRESS.matcher(Email).matches()) {
  120.             valid = false;
  121.             textInputLayoutEmail.setError("Please enter valid email!");
  122.         } else {
  123.             valid = true;
  124.             textInputLayoutEmail.setError(null);
  125.         }
  126.  
  127.         //Handling validation for Password field
  128.         if (Password.isEmpty()) {
  129.             valid = false;
  130.             textInputLayoutPassword.setError("Please enter valid password!");
  131.         } else {
  132.             if (Password.length() > 5) {
  133.                 valid = true;
  134.                 textInputLayoutPassword.setError(null);
  135.             } else {
  136.                 valid = false;
  137.                 textInputLayoutPassword.setError("Password is to short!");
  138.             }
  139.         }
  140.         return valid;
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement