Advertisement
Ronel101

Register

Jul 24th, 2018
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.30 KB | None | 0 0
  1. package com.example.ronel.finalapp;
  2.  
  3. import android.content.DialogInterface;
  4. import android.os.Build;
  5. import android.support.annotation.NonNull;
  6. import android.support.v7.app.AlertDialog;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.os.Bundle;
  9. import android.text.TextUtils;
  10. import android.util.Log;
  11. import android.view.View;
  12. import android.widget.Button;
  13. import android.widget.EditText;
  14. import android.widget.RadioButton;
  15. import android.widget.Toast;
  16.  
  17. import com.google.android.gms.tasks.OnCompleteListener;
  18. import com.google.android.gms.tasks.Task;
  19. import com.google.firebase.auth.AuthResult;
  20. import com.google.firebase.auth.FirebaseAuth;
  21. import com.google.firebase.auth.FirebaseUser;
  22. import com.google.firebase.database.DatabaseReference;
  23. import com.google.firebase.database.FirebaseDatabase;
  24.  
  25. public class Register extends AppCompatActivity {
  26.  
  27.     RadioButton rb1,rb2;
  28.     EditText etName,etMail,etPass,etPhone;
  29.     Button btn;
  30.     String name,mail,pass,phone;
  31.     Boolean sex;
  32.     private FirebaseAuth mAuth;
  33.     DatabaseReference myRef;
  34.  
  35.     @Override
  36.     protected void onCreate(Bundle savedInstanceState) {
  37.         super.onCreate(savedInstanceState);
  38.         setContentView(R.layout.activity_register);
  39.         rb1 = findViewById(R.id.rb1);
  40.         rb2 = findViewById(R.id.rb2);
  41.  
  42.         btn = findViewById(R.id.btn);
  43.  
  44.         etName = findViewById(R.id.etName);
  45.         etMail = findViewById(R.id.etMail);
  46.         etPass = findViewById(R.id.etPass);
  47.         etPhone = findViewById(R.id.etPhone);
  48.  
  49.         mAuth = FirebaseAuth.getInstance();
  50.     }
  51.  
  52.     //Get the information from user
  53.     public void function(View view) {
  54.         name = etName.getText().toString();
  55.         mail = etMail.getText().toString();
  56.         pass = etPass.getText().toString();
  57.         phone = etPhone.getText().toString();
  58.  
  59.         //Check if information is valid
  60.         if (TextUtils.isEmpty(name)) {
  61.             Toast.makeText(this, "Enter name", Toast.LENGTH_SHORT).show();
  62.             return;
  63.         }
  64.         if (name.length() < 2) {
  65.             Toast.makeText(this, "Name to short", Toast.LENGTH_SHORT).show();
  66.             return;
  67.         }
  68.         if (TextUtils.isEmpty(mail)) {
  69.             Toast.makeText(this, "Enter email", Toast.LENGTH_SHORT).show();
  70.             return;
  71.         }
  72.         if (mail.equals("admin@admin.com")) {
  73.             Toast.makeText(this, "Change email", Toast.LENGTH_SHORT).show();
  74.             return;
  75.         }
  76.         if (TextUtils.isEmpty(pass)) {
  77.             Toast.makeText(this, "Enter password", Toast.LENGTH_SHORT).show();
  78.             return;
  79.         }
  80.         if (TextUtils.isEmpty(phone)) {
  81.             Toast.makeText(this, "Enter phone", Toast.LENGTH_SHORT).show();
  82.             return;
  83.         }
  84.         if (phone.length() != 10) {
  85.             Toast.makeText(this, "Phone number needs to be 10 digits", Toast.LENGTH_SHORT).show();
  86.             return;
  87.         }
  88.         if ((!rb1.isChecked()) && (!rb2.isChecked())) {
  89.             Toast.makeText(this, "Choose gender", Toast.LENGTH_SHORT).show();
  90.             return;
  91.         }
  92.         if (rb1.isChecked()) {
  93.             sex = true;
  94.         } else {
  95.             sex = false;
  96.         }
  97.         Log.d(name, "name is:");
  98.         Log.d(mail, "mail is:");
  99.         Log.d(pass, "password is:");
  100.         Log.d(phone, "phone is:");
  101.         Log.d(String.valueOf(sex), "gender is:");
  102.         //End of first section
  103.  
  104.         //Get conformation from user to send data
  105.         AlertDialog.Builder builder;
  106.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  107.             builder = new AlertDialog.Builder(Register.this, android.R.style.Theme_Material_Dialog_Alert);
  108.         } else {
  109.             builder = new AlertDialog.Builder(Register.this);
  110.         }
  111.         builder.setTitle("Send data")
  112.                 .setMessage("Are you sure you want to send data?" + "Email: " + mail + " Password: " + pass)
  113.                 .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
  114.                     public void onClick(DialogInterface dialog, int which) {
  115.                         // continue with sending to DataBase
  116.                         send_to_data_base();
  117.                     }
  118.                 })
  119.                 .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
  120.                     public void onClick(DialogInterface dialog, int which) {
  121.                         // do nothing
  122.                     }
  123.                 })
  124.                 .setIcon(android.R.drawable.ic_dialog_alert)
  125.                 .show();
  126.     }
  127.         //Send data to data-base
  128.         private void send_to_data_base() {
  129.         //Create user in authentication
  130.             mAuth.createUserWithEmailAndPassword(mail, pass)
  131.                     .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  132.                         @Override
  133.                         public void onComplete(@NonNull Task<AuthResult> task) {
  134.                             if (task.isSuccessful()) {
  135.                                 // Sign in success
  136.                                 Log.d("success", "createUserWithEmail:success");
  137.                                 FirebaseUser user = mAuth.getInstance().getCurrentUser();
  138.                                 Toast.makeText(Register.this, "Registered successfully", Toast.LENGTH_SHORT).show();
  139.                                 String uid = user.getUid();
  140.                                 Log.d("uid", uid);
  141.  
  142.                                 //Create user in data base
  143.                                 User user1 = new User(name, uid, sex, phone, 0);
  144.                                 Log.d("user1.tostring()", user1.toString());
  145.                                 //send info
  146.                                 myRef = FirebaseDatabase.getInstance().getReference("users");
  147.                                 myRef.child(uid).setValue(user1);
  148.                             } else {
  149.                                 // If sign in fails, display a message to the user.
  150.                                 Log.w("fail", "createUserWithEmail:failure", task.getException());
  151.                                 Toast.makeText(Register.this, "Authentication failed", Toast.LENGTH_SHORT).show();
  152.                             }
  153.                         }
  154.                     });
  155.         }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement