Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.ronel.finalapp;
- import android.content.DialogInterface;
- import android.os.Build;
- import android.support.annotation.NonNull;
- import android.support.v7.app.AlertDialog;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.text.TextUtils;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.RadioButton;
- import android.widget.Toast;
- import com.google.android.gms.tasks.OnCompleteListener;
- import com.google.android.gms.tasks.Task;
- import com.google.firebase.auth.AuthResult;
- import com.google.firebase.auth.FirebaseAuth;
- import com.google.firebase.auth.FirebaseUser;
- import com.google.firebase.database.DatabaseReference;
- import com.google.firebase.database.FirebaseDatabase;
- public class Register extends AppCompatActivity {
- RadioButton rb1,rb2;
- EditText etName,etMail,etPass,etPhone;
- Button btn;
- String name,mail,pass,phone;
- Boolean sex;
- private FirebaseAuth mAuth;
- DatabaseReference myRef;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_register);
- rb1 = findViewById(R.id.rb1);
- rb2 = findViewById(R.id.rb2);
- btn = findViewById(R.id.btn);
- etName = findViewById(R.id.etName);
- etMail = findViewById(R.id.etMail);
- etPass = findViewById(R.id.etPass);
- etPhone = findViewById(R.id.etPhone);
- mAuth = FirebaseAuth.getInstance();
- }
- public void function(View view) {
- //Works good - no bugs
- name = etName.getText().toString();
- mail = etMail.getText().toString();
- pass = etPass.getText().toString();
- phone = etPhone.getText().toString();
- if (TextUtils.isEmpty(name)) {
- Toast.makeText(this, "Enter name", Toast.LENGTH_SHORT).show();
- return;
- }
- if (name.length() < 2) {
- Toast.makeText(this, "Name to short", Toast.LENGTH_SHORT).show();
- return;
- }
- if (TextUtils.isEmpty(mail)) {
- Toast.makeText(this, "Enter mail", Toast.LENGTH_SHORT).show();
- return;
- }
- if (TextUtils.isEmpty(pass)) {
- Toast.makeText(this, "Enter password", Toast.LENGTH_SHORT).show();
- return;
- }
- if (TextUtils.isEmpty(phone)) {
- Toast.makeText(this, "Enter phone", Toast.LENGTH_SHORT).show();
- return;
- }
- if (phone.length() != 10) {
- Toast.makeText(this, "Phone number needs to be 10 digits", Toast.LENGTH_SHORT).show();
- return;
- }
- if ((!rb1.isChecked()) && (!rb2.isChecked())) {
- Toast.makeText(this, "Choose gender", Toast.LENGTH_SHORT).show();
- return;
- }
- if (rb1.isChecked()) {
- sex = true;
- }
- else {
- sex = false;
- }
- Log.d(name,"name is:");
- Log.d(mail,"mail is:");
- Log.d(pass,"password is:");
- Log.d(phone,"phone is:");
- Log.d(String.valueOf(sex),"gender is:");
- //End of first section
- //Alert dialog
- AlertDialog.Builder builder;
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
- builder = new AlertDialog.Builder(Register.this, android.R.style.Theme_Material_Dialog_Alert);
- } else {
- builder = new AlertDialog.Builder(Register.this);
- }
- builder.setTitle("Send data")
- .setMessage("Are you sure you want to send data?" + "Email: " + mail + " Password: " + pass)
- .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- // continue with delete
- }
- })
- .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- // do nothing
- }
- })
- .setIcon(android.R.drawable.ic_dialog_alert)
- .show();
- //Send to DataBase
- //Works fine - no bugs
- mAuth.createUserWithEmailAndPassword(mail, pass)
- .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
- @Override
- public void onComplete(@NonNull Task<AuthResult> task) {
- if (task.isSuccessful()) {
- // Sign in success
- Log.d("success", "createUserWithEmail:success");
- FirebaseUser user = mAuth.getInstance().getCurrentUser();
- Toast.makeText(Register.this, "Registered successfully", Toast.LENGTH_SHORT).show();
- String uid = user.getUid();
- Log.d("uid",uid);
- User user1=new User(name,uid,sex,phone,0);
- Log.d("user1.tostring()",user1.toString());
- //send info
- myRef =FirebaseDatabase.getInstance().getReference("users");
- myRef.child(uid).setValue(user1);
- } else {
- // If sign in fails, display a message to the user.
- Log.w("fail", "createUserWithEmail:failure", task.getException());
- Toast.makeText(Register.this, "Authentication failed", Toast.LENGTH_SHORT).show();
- }
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement