Guest User

Untitled

a guest
Dec 8th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1. package com.example.ilham.dompettebal;
  2.  
  3. import android.content.Intent;
  4. import android.service.autofill.UserData;
  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.ProgressBar;
  13. import android.widget.TextView;
  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. import com.google.firebase.database.DatabaseReference;
  21. import com.google.firebase.database.FirebaseDatabase;
  22.  
  23. import org.w3c.dom.Text;
  24.  
  25. import java.util.HashMap;
  26.  
  27. public class register extends AppCompatActivity {
  28. private EditText Inputemail,Inputusername,Inputpassword;
  29. private TextView signin;
  30. private Button buttonRegister;
  31. private FirebaseAuth auth;
  32. private DatabaseReference users;
  33. String userId;
  34. // private ProgressBar progressBar;
  35.  
  36.  
  37. @Override
  38. protected void onCreate(Bundle savedInstanceState) {
  39. super.onCreate(savedInstanceState);
  40. setContentView(R.layout.activity_register);
  41.  
  42. auth = FirebaseAuth.getInstance();
  43. userId = auth.getCurrentUser().getUid();
  44. users = FirebaseDatabase.getInstance().getReference().child("users").child(userId);
  45. buttonRegister = (Button) findViewById(R.id.buttonRegister);
  46. Inputemail = (EditText) findViewById(R.id.email);
  47. Inputpassword = (EditText) findViewById(R.id.password);
  48. Inputusername = (EditText) findViewById(R.id.username);
  49. signin = (TextView) findViewById(R.id.signin) ;
  50.  
  51.  
  52. signin.setOnClickListener(new View.OnClickListener() {
  53. @Override
  54. public void onClick(View v) {
  55. startActivity(new Intent(register.this, login.class));
  56. }
  57. });
  58.  
  59.  
  60.  
  61.  
  62. buttonRegister.setOnClickListener(new View.OnClickListener() {
  63. @Override
  64. public void onClick(View v) {
  65.  
  66. String email = Inputemail.getText().toString().trim();
  67. String password = Inputpassword.getText().toString().trim();
  68. String username = Inputusername.getText().toString().trim();
  69.  
  70. if (TextUtils.isEmpty(email)) {
  71. Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
  72. return;
  73. }
  74.  
  75. if (TextUtils.isEmpty(password)) {
  76. Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
  77. return;
  78. }
  79.  
  80. if (password.length() < 6) {
  81. Toast.makeText(getApplicationContext(), "Password too short, enter minimum 6 characters!", Toast.LENGTH_SHORT).show();
  82. return;
  83. }
  84.  
  85. // progressBar.setVisibility(View.VISIBLE);
  86. //create user
  87. auth.createUserWithEmailAndPassword(email, password)
  88. .addOnCompleteListener(register.this, new OnCompleteListener<AuthResult>() {
  89. @Override
  90. public void onComplete(@NonNull Task<AuthResult> task) {
  91. Toast.makeText(register.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
  92. // progressBar.setVisibility(View.GONE);
  93. // If sign in fails, display a message to the user. If sign in succeeds
  94. // the auth state listener will be notified and logic to handle the
  95. // signed in user can be handled in the listener.
  96. if (!task.isSuccessful()) {
  97. Toast.makeText(register.this, "Authentication failed." + task.getException(),
  98. Toast.LENGTH_SHORT).show();
  99. } else {
  100. SavedUser();
  101. startActivity(new Intent(register.this, MainActivity.class));
  102. finish();
  103. }
  104. }
  105. });
  106.  
  107. }
  108. });
  109. }
  110.  
  111. private void SavedUser()
  112. {
  113.  
  114.  
  115. Inputemail = (EditText) findViewById(R.id.email);
  116. Inputpassword = (EditText) findViewById(R.id.password);
  117. Inputusername = (EditText) findViewById(R.id.username);
  118. String email = Inputemail.getText().toString().trim();
  119. String password = Inputpassword.getText().toString().trim();
  120. String username = Inputusername.getText().toString().trim();
  121. HashMap userData = new HashMap();
  122. userData.put("username",username);
  123. userData.put("email",email);
  124. users.updateChildren(userData).addOnCompleteListener(new OnCompleteListener() {
  125. @Override
  126. public void onComplete(@NonNull Task task) {
  127. if(task.isSuccessful())
  128. {
  129. Toast.makeText(register.this, "User Created" + task.getException(),
  130. Toast.LENGTH_SHORT).show();
  131. }
  132. }
  133. });
  134.  
  135. }
  136.  
  137. }
  138.  
  139. apply plugin: 'com.android.application'
  140. apply plugin: 'com.google.gms.google-services'
  141.  
  142. android {
  143. compileSdkVersion 28
  144. defaultConfig {
  145. applicationId "com.example.ilham.dompettebal"
  146. minSdkVersion 15
  147. targetSdkVersion 28
  148. versionCode 1
  149. versionName "1.0"
  150. testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
  151. }
  152. buildTypes {
  153. release {
  154. minifyEnabled false
  155. proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  156. }
  157. }
  158. }
  159.  
  160. dependencies {
  161. implementation fileTree(dir: 'libs', include: ['*.jar'])
  162. //noinspection GradleCompatible
  163. implementation 'com.android.support:appcompat-v7:28.0.0'
  164. implementation 'com.android.support.constraint:constraint-layout:1.1.3'
  165. testImplementation 'junit:junit:4.12'
  166. androidTestImplementation 'com.android.support.test:runner:1.0.2'
  167. androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
  168.  
  169.  
  170. implementation 'com.google.firebase:firebase-core:16.0.1'
  171. implementation 'com.google.firebase:firebase-storage:16.0.1'
  172. implementation 'com.google.firebase:firebase-auth:16.0.1'
  173. implementation 'com.google.firebase:firebase-database:16.0.1'
  174. }
Add Comment
Please, Sign In to add comment