daily pastebin goal
58%
SHARE
TWEET

Untitled

a guest Jan 19th, 2017 130 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package dev.mintz.loginapp;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.content.Intent;
  5. import android.support.annotation.NonNull;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.text.TextUtils;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.EditText;
  12. import android.widget.TextView;
  13.  
  14. import android.widget.Toast;
  15.  
  16. import com.google.android.gms.tasks.OnCompleteListener;
  17. import com.google.android.gms.tasks.Task;
  18. import com.google.firebase.auth.AuthResult;
  19. import com.google.firebase.auth.FirebaseAuth;
  20.  
  21. import com.google.firebase.database.DatabaseReference;
  22. import com.google.firebase.database.FirebaseDatabase;
  23.  
  24. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  25.  
  26.     public static String username, password;
  27.     //defining view objects
  28.     private EditText editTextEmail;
  29.     private EditText editTextPassword;
  30.     private EditText editTextUsername;
  31.     private Button buttonSignup;
  32.  
  33.     private TextView textViewSignin;
  34.  
  35.     private ProgressDialog progressDialog;
  36.  
  37.  
  38.     //defining firebaseauth object
  39.     private FirebaseAuth firebaseAuth;
  40.  
  41.     private DatabaseReference databaseReference;
  42.  
  43.     @Override
  44.     protected void onCreate(Bundle savedInstanceState) {
  45.         super.onCreate(savedInstanceState);
  46.         setContentView(R.layout.activity_main);
  47.  
  48.         //initializing firebase auth object
  49.         firebaseAuth = FirebaseAuth.getInstance();
  50.  
  51.         //if getCurrentUser does not returns null
  52.         if(firebaseAuth.getCurrentUser() != null){
  53.             //that means user is already logged in
  54.             //so close this activity
  55.             finish();
  56.  
  57.             //and open profile activity
  58.             startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
  59.         }
  60.  
  61.         //getting the database reference
  62.         databaseReference = FirebaseDatabase.getInstance().getReference();
  63.         //initializing views
  64.         editTextEmail = (EditText) findViewById(R.id.editTextEmail);
  65.         editTextUsername = (EditText) findViewById(R.id.editTextUsername);
  66.         editTextPassword = (EditText) findViewById(R.id.editTextPassword);
  67.         textViewSignin = (TextView) findViewById(R.id.textViewSignin);
  68.  
  69.         buttonSignup = (Button) findViewById(R.id.buttonSignup);
  70.  
  71.         progressDialog = new ProgressDialog(this);
  72.  
  73.         //attaching listener to button
  74.         buttonSignup.setOnClickListener(this);
  75.         textViewSignin.setOnClickListener(this);
  76.     }
  77.  
  78.     private void registerUser(){
  79.  
  80.         //getting email and password from edit texts
  81.         String email = editTextEmail.getText().toString().trim();
  82.         String username = editTextUsername.getText().toString().trim();
  83.         String password  = editTextPassword.getText().toString().trim();
  84.  
  85.         //checking if email and passwords are empty
  86.         if(TextUtils.isEmpty(email)){
  87.             Toast.makeText(this,"Please enter email",Toast.LENGTH_LONG).show();
  88.             return;
  89.         }
  90.  
  91.         if(TextUtils.isEmpty(password)){
  92.             Toast.makeText(this,"Please enter password",Toast.LENGTH_LONG).show();
  93.             return;
  94.         }
  95.  
  96.         //if the email and password are not empty
  97.         //displaying a progress dialog
  98.  
  99.         progressDialog.setMessage("Registering Please Wait...");
  100.         progressDialog.show();
  101.  
  102.         //creating a new user
  103.         firebaseAuth.createUserWithEmailAndPassword(email, password)
  104.                 .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  105.                     @Override
  106.                     public void onComplete(@NonNull Task<AuthResult> task) {
  107.                         //checking if success
  108.                         if(task.isSuccessful()){
  109.                             saveUserInformation();
  110.                             Toast.makeText(MainActivity.this,"Successfully registered",Toast.LENGTH_LONG).show();
  111.                             finish();
  112.                             startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
  113.                         }else{
  114.                             //display some message here
  115.                             Toast.makeText(MainActivity.this,"Registration Error",Toast.LENGTH_LONG).show();
  116.                         }
  117.                         progressDialog.dismiss();
  118.                     }
  119.                 });
  120.  
  121.     }
  122.  
  123.     private void saveUserInformation() {
  124.         //Getting values from database
  125.  
  126.         //creating a userinformation object
  127.         UserInformation userInformation = new UserInformation(username,password);
  128.         databaseReference.child("users").setValue(userInformation);
  129.  
  130.  
  131.     }
  132.  
  133.     @Override
  134.     public void onClick(View view) {
  135.  
  136.         if(view == buttonSignup){
  137.             registerUser();
  138.         }
  139.  
  140.         if(view == textViewSignin){
  141.             //open login activity when user taps on the already registered textview
  142.             startActivity(new Intent(this, LoginActivity.class));
  143.         }
  144.  
  145.     }
  146. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top