Advertisement
Guest User

Untitled

a guest
Apr 12th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1. public class BlogSingleActivity extends AppCompatActivity {
  2.  
  3. private Button mSubmitBook;;
  4. private DatabaseReference mDatabaseBookUser;
  5. private FirebaseAuth firebaseAuth;
  6.  
  7.  
  8. List<Blog> Info;
  9.  
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_blog_single);
  14. firebaseAuth= FirebaseAuth.getInstance();
  15.  
  16. mSubmitBook = (Button) findViewById(R.id.SubmitBook);
  17. mDatabaseBookUser = FirebaseDatabase.getInstance().getReference("Users");
  18.  
  19. Info = new ArrayList<>();
  20.  
  21. mSubmitBook.setOnClickListener(new View.OnClickListener() {
  22. @Override
  23. public void onClick(View view) {
  24. if(view== mSubmitBook){
  25. addName();
  26. }
  27. }
  28. });
  29. }
  30.  
  31. public class MainActivity extends AppCompatActivity implements View.OnClickListener{
  32.  
  33. private Button buttonRegister;
  34. private EditText mMatricField;
  35. private EditText mNameField;
  36. private EditText editTextEmail;
  37. private EditText editTextPassword;
  38. private TextView textViewSignin;
  39.  
  40. private ProgressDialog progressDialog;
  41.  
  42. private FirebaseAuth firebaseAuth;
  43. private DatabaseReference mDatabase;
  44.  
  45. @Override
  46. protected void onCreate(Bundle savedInstanceState) {
  47. super.onCreate(savedInstanceState);
  48. setContentView(R.layout.activity_main);
  49.  
  50. firebaseAuth= FirebaseAuth.getInstance();
  51.  
  52.  
  53. if(firebaseAuth.getCurrentUser() !=null){
  54. //profile activity here
  55. finish();
  56. startActivity(new Intent(getApplicationContext(),ProfileActivity.class));
  57. }
  58. mDatabase= FirebaseDatabase.getInstance().getReference().child("Users");
  59.  
  60. progressDialog=new ProgressDialog(this);
  61.  
  62. buttonRegister=(Button) findViewById(R.id.buttonRegister);
  63.  
  64. mMatricField= (EditText) findViewById(R.id.matricField);
  65. mNameField=(EditText) findViewById(R.id.nameField);
  66. editTextEmail=(EditText) findViewById(R.id.editTextEmail);
  67. editTextPassword=(EditText) findViewById(R.id.editTextPassword);
  68.  
  69. textViewSignin=(TextView) findViewById(R.id.textViewSignin);
  70.  
  71. buttonRegister.setOnClickListener(this);
  72. textViewSignin.setOnClickListener(this);
  73. }
  74.  
  75. private void registerUser(){
  76. final String matric=mMatricField.getText().toString().trim();
  77. final String name= mNameField.getText().toString().trim();
  78. String email=editTextEmail.getText().toString().trim();
  79. String password=editTextPassword.getText().toString().trim();
  80.  
  81. if(TextUtils.isEmpty(matric)){
  82. //password is empty
  83. Toast.makeText(this, "Please enter matric number", Toast.LENGTH_SHORT).show();
  84. //stopping the function execution further
  85. return;
  86.  
  87. }
  88. if(TextUtils.isEmpty(name)){
  89. //password is empty
  90. Toast.makeText(this, "Please enter your name", Toast.LENGTH_SHORT).show();
  91. //stopping the function execution further
  92. return;
  93.  
  94. }
  95.  
  96. if(TextUtils.isEmpty(email)){
  97. //email is empty
  98. Toast.makeText(this,"Please enter email",Toast.LENGTH_SHORT).show();
  99. //stopping the function execution further
  100. return;
  101. }
  102. if(TextUtils.isEmpty(password)){
  103. //password is empty
  104. Toast.makeText(this, "Please enter password", Toast.LENGTH_SHORT).show();
  105. //stopping the function execution further
  106. return;
  107.  
  108. }
  109. //if validation are ok
  110. //we will first show a progressbar
  111.  
  112. progressDialog.setMessage("Registering User...");
  113. progressDialog.show();
  114.  
  115. firebaseAuth.createUserWithEmailAndPassword(email,password)
  116. .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  117. @Override
  118. public void onComplete(@NonNull Task<AuthResult> task) {
  119. if(task.isSuccessful()){
  120. String user_id=firebaseAuth.getCurrentUser().getUid();
  121. DatabaseReference current_user_db= mDatabase.child(user_id);
  122. current_user_db.child("name").setValue(name);
  123. current_user_db.child("matric").setValue(matric);
  124. current_user_db.child("image").setValue("default");
  125. //user is successfully registered and logged in
  126. //we will start the profile activity here
  127. //display a toast only
  128. finish();
  129. startActivity(new Intent(getApplicationContext(),ProfileActivity.class));
  130. Toast.makeText(MainActivity.this, "Registered Successfully", Toast.LENGTH_SHORT).show();
  131. }else{
  132. Toast.makeText(MainActivity.this, "Could not register... Please try again", Toast.LENGTH_SHORT).show();
  133.  
  134. }
  135. }
  136. });
  137. }
  138.  
  139. @Override
  140. public void onClick(View view) {
  141. if(view== buttonRegister){
  142. registerUser();
  143. }
  144. if(view== textViewSignin){
  145. //will open login activity here
  146. startActivity(new Intent(this, LoginActivity.class));
  147. }
  148.  
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement