MainActivity.java
3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package com.example.user.firebaseauthdemo;
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
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;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button buttonRegister;
private EditText editTextEmail;
private EditText editTextPassword;
private TextView textViewSignIn;
private ProgressDialog progressDialog;
private FirebaseAuth firebaseAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressDialog = new ProgressDialog(MainActivity.this);
firebaseAuth = FirebaseAuth.getInstance();
if(firebaseAuth.getCurrentUser() != null){
//profile activity here
finish();
startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
}
buttonRegister = (Button) findViewById(R.id.buttonRegister);
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
editTextPassword = (EditText) findViewById(R.id.editTextPassword);
textViewSignIn = (TextView) findViewById(R.id.textViewSignIn);
buttonRegister.setOnClickListener(this);
textViewSignIn.setOnClickListener(this);
}
private void registerUser(){
String email = editTextEmail.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();
if(TextUtils.isEmpty(email)){
//email is empty
Toast.makeText(this, "Please enter email", Toast.LENGTH_SHORT).show();
//stopping the function execution further
return;
}
if(TextUtils.isEmpty(password)) {
//password is empty
Toast.makeText(this, "Please enter password", Toast.LENGTH_SHORT).show();
//stopping the function execution further
return;
}
//if validation are ok
//we will first show a progressbar
progressDialog.setTitle("Loading...");
progressDialog.setMessage("Registering User...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.show();
//creating a new user
firebaseAuth.createUserWithEmailAndPassword(email,password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
finish();
startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
}else{
Toast.makeText(MainActivity.this, "Could not register.. Try again", Toast.LENGTH_SHORT).show();
}
progressDialog.dismiss();
}
});
}
@Override
public void onClick(View view) {
if(view == buttonRegister){
registerUser();
}
if(view == textViewSignIn){
//will open login activity here
startActivity(new Intent(this, LoginActivity.class));
}
}
}