Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dev.mintz.loginapp;
- 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.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;
- import com.google.firebase.database.DatabaseReference;
- import com.google.firebase.database.FirebaseDatabase;
- public class MainActivity extends AppCompatActivity implements View.OnClickListener {
- public static String username, password;
- //defining view objects
- private EditText editTextEmail;
- private EditText editTextPassword;
- private EditText editTextUsername;
- private Button buttonSignup;
- private TextView textViewSignin;
- private ProgressDialog progressDialog;
- //defining firebaseauth object
- private FirebaseAuth firebaseAuth;
- private DatabaseReference databaseReference;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //initializing firebase auth object
- firebaseAuth = FirebaseAuth.getInstance();
- //if getCurrentUser does not returns null
- if(firebaseAuth.getCurrentUser() != null){
- //that means user is already logged in
- //so close this activity
- finish();
- //and open profile activity
- startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
- }
- //getting the database reference
- databaseReference = FirebaseDatabase.getInstance().getReference();
- //initializing views
- editTextEmail = (EditText) findViewById(R.id.editTextEmail);
- editTextUsername = (EditText) findViewById(R.id.editTextUsername);
- editTextPassword = (EditText) findViewById(R.id.editTextPassword);
- textViewSignin = (TextView) findViewById(R.id.textViewSignin);
- buttonSignup = (Button) findViewById(R.id.buttonSignup);
- progressDialog = new ProgressDialog(this);
- //attaching listener to button
- buttonSignup.setOnClickListener(this);
- textViewSignin.setOnClickListener(this);
- }
- private void registerUser(){
- //getting email and password from edit texts
- String email = editTextEmail.getText().toString().trim();
- String username = editTextUsername.getText().toString().trim();
- String password = editTextPassword.getText().toString().trim();
- //checking if email and passwords are empty
- if(TextUtils.isEmpty(email)){
- Toast.makeText(this,"Please enter email",Toast.LENGTH_LONG).show();
- return;
- }
- if(TextUtils.isEmpty(password)){
- Toast.makeText(this,"Please enter password",Toast.LENGTH_LONG).show();
- return;
- }
- //if the email and password are not empty
- //displaying a progress dialog
- progressDialog.setMessage("Registering Please Wait...");
- progressDialog.show();
- //creating a new user
- firebaseAuth.createUserWithEmailAndPassword(email, password)
- .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
- @Override
- public void onComplete(@NonNull Task<AuthResult> task) {
- //checking if success
- if(task.isSuccessful()){
- saveUserInformation();
- Toast.makeText(MainActivity.this,"Successfully registered",Toast.LENGTH_LONG).show();
- finish();
- startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
- }else{
- //display some message here
- Toast.makeText(MainActivity.this,"Registration Error",Toast.LENGTH_LONG).show();
- }
- progressDialog.dismiss();
- }
- });
- }
- private void saveUserInformation() {
- //Getting values from database
- //creating a userinformation object
- UserInformation userInformation = new UserInformation(username,password);
- databaseReference.child("users").setValue(userInformation);
- }
- @Override
- public void onClick(View view) {
- if(view == buttonSignup){
- registerUser();
- }
- if(view == textViewSignin){
- //open login activity when user taps on the already registered textview
- startActivity(new Intent(this, LoginActivity.class));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement