zainijmusic

SIgn up

May 6th, 2018
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.64 KB | None | 0 0
  1. package com.zainijofficial.babapoultry;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.content.Intent;
  5. import android.support.annotation.NonNull;
  6. import android.support.design.widget.TextInputLayout;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.os.Bundle;
  9. import android.text.TextUtils;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.TextView;
  13. import android.widget.Toast;
  14.  
  15. import com.google.android.gms.tasks.OnCompleteListener;
  16. import com.google.android.gms.tasks.OnSuccessListener;
  17. import com.google.android.gms.tasks.Task;
  18. import com.google.firebase.auth.AuthResult;
  19. import com.google.firebase.auth.FirebaseAuth;
  20. import com.google.firebase.auth.FirebaseUser;
  21. import com.google.firebase.database.DatabaseReference;
  22. import com.google.firebase.database.FirebaseDatabase;
  23.  
  24. public class SignUpActivity extends AppCompatActivity {
  25.  
  26.  
  27.     private TextInputLayout createFullname;
  28.     private TextInputLayout createEmail;
  29.     private TextInputLayout createPassword;
  30.     private Button SignUpButtonID;
  31.     private TextView backSignIn;
  32.     private ProgressDialog mprogressDialog;
  33.  
  34.     private FirebaseAuth mAuth;
  35.     private FirebaseAuth.AuthStateListener mAuthListener;
  36.     private FirebaseUser mUser;
  37.     private FirebaseDatabase mDatabase;
  38.     private DatabaseReference myRef;
  39.  
  40.  
  41.  
  42.     @Override
  43.     protected void onCreate(Bundle savedInstanceState) {
  44.         super.onCreate(savedInstanceState);
  45.         setContentView(R.layout.activity_sign_up);
  46.  
  47.  
  48.         createFullname=findViewById(R.id.Text_input_fullname);
  49.         createEmail=findViewById(R.id.Text_input_CreateEmail);
  50.         createPassword=findViewById(R.id.Text_input_Createpassword);
  51.         SignUpButtonID=findViewById(R.id.signupIDButton);
  52.         backSignIn=findViewById(R.id.backSignIn);
  53.  
  54.         mAuth=FirebaseAuth.getInstance();
  55.         mUser=mAuth.getCurrentUser();
  56.  
  57.         mDatabase=FirebaseDatabase.getInstance();
  58.         myRef=mDatabase.getReference().child("MyUser");
  59.         mprogressDialog = new ProgressDialog(this);
  60.  
  61.  
  62.       //------------for back to sign in page ------------
  63.  
  64.         backSignIn.setOnClickListener(new View.OnClickListener() {
  65.             @Override
  66.             public void onClick(View v) {
  67.                 Intent intent = new Intent(SignUpActivity.this,SignInActivity.class);
  68.                 startActivity(intent);
  69.                 finish();
  70.             }
  71.         });
  72.       //-------------------------end--------------------------
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.         //----------------Create account-----------------------
  82.  
  83.  
  84.         SignUpButtonID.setOnClickListener(new View.OnClickListener() {
  85.             @Override
  86.             public void onClick(View v) {
  87.  
  88.  
  89.                 if     (    !TextUtils.isEmpty(createFullname.getEditText().getText().toString().trim())
  90.                         &&  !TextUtils.isEmpty(createEmail.getEditText().getText().toString().trim())
  91.                         &&  !TextUtils.isEmpty(createPassword.getEditText().getText().toString().trim())
  92.                         )
  93.                 {
  94.                     mprogressDialog.setMessage("Account creating");
  95.                     mprogressDialog.show();
  96.                     AccountCreate();
  97. //                    Intent intent = new Intent(SignUpActivity.this,ContentActivity.class);
  98. //                    startActivity(intent);
  99. //                    finish();
  100.           }
  101.                 else {
  102.  
  103.                     ValidateFullname();
  104.                     ValidatePassword();
  105.                     ValidateEmail();
  106.                    }
  107.             }
  108.         });
  109.  
  110.  
  111.  
  112.     } //---end of on create ----------------
  113.  
  114.  
  115.  
  116.  
  117.  
  118.     //-------------- Check Feild validation -------------------
  119.  
  120.     private boolean ValidateFullname(){
  121.         String Fullnamestr =createFullname.getEditText().getText().toString().trim();
  122.         if (Fullnamestr.isEmpty()){
  123.  
  124.             createFullname.setError("Field Can't be Empty");
  125.             return false;
  126.         }
  127.  
  128.         else {
  129.             createFullname.setError(null);
  130.             return true;
  131.         }
  132.     }
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.     private boolean ValidateEmail(){
  141.         final String emailstr=createEmail.getEditText().getText().toString().trim();
  142.         if (emailstr.isEmpty()){
  143.  
  144.             createEmail.setError("Field Can't be Empty");
  145.             return false;
  146.         }
  147.         else {
  148.             createEmail.setError(null);
  149.             return true;
  150.         }
  151.     }
  152.  
  153.  
  154.  
  155.  
  156.  
  157.     private boolean ValidatePassword(){
  158.         final String poswdstr=createPassword.getEditText().getText().toString().trim();
  159.         if (poswdstr.isEmpty()){
  160.  
  161.             createPassword.setError("Field Can't be Empty");
  162.             return false;
  163.         }
  164.         else {
  165.             createPassword.setError(null);
  166.             return true;
  167.         }
  168.     }
  169.  
  170.  
  171. //--------------------------end validation--------------------------
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.     private void AccountCreate() {
  181.  
  182.         final String emailid=createEmail.getEditText().getText().toString().trim();
  183.         final String pwdid=createPassword.getEditText().getText().toString().trim();
  184.         final String Fullname = createFullname.getEditText().getText().toString().trim();
  185.  
  186.         mAuth.createUserWithEmailAndPassword(emailid,pwdid)
  187.                 .addOnSuccessListener(new OnSuccessListener<AuthResult>() {
  188.                     @Override
  189.                     public void onSuccess(AuthResult authResult) {
  190.                         if (authResult !=null){
  191.                             mprogressDialog.setMessage("Account creating");
  192.                             mprogressDialog.show();
  193.  
  194.  
  195.                             String userID = mAuth.getCurrentUser().getUid();
  196.                             DatabaseReference currentUserDB = myRef.child(userID);
  197.                             currentUserDB.child("Full name").setValue(Fullname);
  198.                             currentUserDB.child("Email id").setValue(emailid);
  199.                             currentUserDB.child("Password").setValue(pwdid);
  200.  
  201.  
  202.                             Toast.makeText(SignUpActivity.this, "Successfull! Login", Toast.LENGTH_SHORT).show();
  203.  
  204.                             Intent intent = new Intent(SignUpActivity.this, MainContentActivity.class);
  205.                             startActivity(intent);
  206.                             finish();
  207.                         }
  208.                         else {
  209.                             Toast.makeText(SignUpActivity.this, "Try again!!  Acount can't be created", Toast.LENGTH_SHORT).show();
  210.                             mprogressDialog.dismiss();
  211.                         }
  212.                     }
  213.                 });
  214.  
  215.  
  216.  
  217.     }
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226. }
Add Comment
Please, Sign In to add comment