Guest User

Untitled

a guest
Mar 12th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity implements View.OnClickListener{
  2.  
  3. private Button buttonRegister;
  4. private EditText editTextEmail;
  5. private EditText editTextPassword;
  6. private TextView textViewSignin;
  7.  
  8. private ProgressDialog progressDialog;
  9.  
  10. private FirebaseAuth firebaseAuth;
  11.  
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16.  
  17. firebaseAuth = FirebaseAuth.getInstance();
  18. progressDialog = new ProgressDialog(this);
  19.  
  20. if(firebaseAuth.getCurrentUser() != null){
  21. //Profile activity
  22. finish();
  23. startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
  24. }
  25.  
  26. buttonRegister = (Button) findViewById(R.id.buttonRegister);
  27. editTextEmail = (EditText) findViewById(R.id.editTextEmail);
  28. editTextPassword = (EditText) findViewById(R.id.editTextPassword);
  29. textViewSignin = (TextView) findViewById((R.id.textViewSignin));
  30.  
  31. buttonRegister.setOnClickListener(this);
  32. textViewSignin.setOnClickListener(this);
  33. }
  34.  
  35. private void registerUser(){
  36. String email = editTextEmail.getText().toString().trim();
  37. String password = editTextPassword.getText().toString().trim();
  38.  
  39. if(TextUtils.isEmpty(email)){
  40. //email is empty
  41. Toast.makeText(this, "Please enter email", Toast.LENGTH_LONG).show();
  42. //stopping the function execution further
  43. return;
  44. }
  45.  
  46. if(TextUtils.isEmpty(password)){
  47. //email is empty
  48. Toast.makeText(this, "Please enter password", Toast.LENGTH_LONG).show();
  49. //stopping the function execution further
  50. return;
  51. }
  52.  
  53. //if validations are Okay
  54. //Register User
  55. progressDialog.setMessage("Registering User...");
  56. progressDialog.show();
  57.  
  58. firebaseAuth.createUserWithEmailAndPassword(email, password)
  59. .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  60. @Override
  61. public void onComplete(@NonNull Task<AuthResult> task) {
  62. if(task.isSuccessful()){
  63. //user is successfully registered
  64. // Toast.makeText(MainActivity.this, "Registered Successfully", Toast.LENGTH_LONG).show();
  65. finish();
  66. startActivity((new Intent(getApplicationContext(), ProfileActivity.class)));
  67. }
  68. else{
  69. Toast.makeText(MainActivity.this, "Could not register, Please try again!", Toast.LENGTH_LONG).show();
  70. }
  71. progressDialog.dismiss();
  72. }
  73. });
  74. }
  75.  
  76. @Override
  77. public void onClick(View view) {
  78. if(view == buttonRegister) {
  79. registerUser();
  80. }
  81.  
  82. if (view == textViewSignin){
  83. //open login activity here
  84. startActivity(new Intent(this,LoginActivity.class));
  85. }
  86. }
  87. }
  88.  
  89. public class ProfileActivity extends AppCompatActivity implements View.OnClickListener {
  90.  
  91.  
  92. private FirebaseAuth firebaseAuth;
  93. private DatabaseReference databaseReference;
  94.  
  95. private TextView textViewUserEmail;
  96. private Button buttonLogout, saveUserInformationBtn;
  97. EditText userName, userAddress;
  98. @Override
  99. protected void onCreate(Bundle savedInstanceState) {
  100. super.onCreate(savedInstanceState);
  101. setContentView(R.layout.activity_profile);
  102.  
  103. firebaseAuth = FirebaseAuth.getInstance();
  104.  
  105. if (firebaseAuth.getCurrentUser() == null) {
  106. finish();
  107. startActivity(new Intent(this, LoginActivity.class));
  108. }
  109.  
  110. databaseReference = FirebaseDatabase.getInstance().getReference();
  111.  
  112. FirebaseUser user = firebaseAuth.getCurrentUser();
  113.  
  114. textViewUserEmail = (TextView) findViewById(R.id.textViewUserEmail);
  115. saveUserInformationBtn = (Button) findViewById(R.id.userInfoSave);
  116. userName = (EditText) findViewById(R.id.userName);
  117. userAddress = (EditText) findViewById(R.id.userAddress);
  118.  
  119.  
  120. textViewUserEmail.setText("Welcome " + user.getEmail());
  121.  
  122. buttonLogout = (Button) findViewById(R.id.buttonLogout);
  123.  
  124. buttonLogout.setOnClickListener(this);
  125. saveUserInformationBtn.setOnClickListener(this);
  126. }
  127.  
  128. private void saveUserInformation(){
  129. String name = userName.getText().toString().trim();
  130. String address = userAddress.getText().toString().trim();
  131.  
  132. Userinformation userinformation = new Userinformation(name, address);
  133.  
  134. FirebaseUser user = firebaseAuth.getCurrentUser();
  135.  
  136. databaseReference.child(user.getUid()).setValue(userinformation);
  137.  
  138. Toast.makeText(this, "Information Saved...", Toast.LENGTH_LONG). show();
  139. }
  140.  
  141. @Override
  142. public void onClick(View view) {
  143. if(view == buttonLogout){
  144. firebaseAuth.signOut();
  145. finish();
  146. startActivity(new Intent(this,LoginActivity.class));
  147. }
  148.  
  149. if (view == saveUserInformationBtn){
  150. saveUserInformation();
  151. }
  152. }
  153. }
Add Comment
Please, Sign In to add comment