Advertisement
Guest User

creating dialogs

a guest
Feb 22nd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.38 KB | None | 0 0
  1. private void showLogInDialog() {
  2. final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
  3. dialog.setTitle("Log In ");
  4. dialog.setMessage("Please use email to log in");
  5.  
  6. LayoutInflater inflater = LayoutInflater.from(this);
  7. View login_layout = inflater.inflate(R.layout.layout_login,null);
  8.  
  9. final MaterialEditText edtEmail = login_layout.findViewById(R.id.edtEmail);
  10. final MaterialEditText edtPassword = login_layout.findViewById(R.id.edtPassword);
  11. dialog.setView(login_layout);
  12.  
  13.  
  14. //Set button
  15. dialog.setPositiveButton("Log In", new DialogInterface.OnClickListener() {
  16. @Override
  17. public void onClick(DialogInterface dialogInterface, int i) {
  18. dialogInterface.dismiss();
  19.  
  20. //check validation
  21. if (TextUtils.isEmpty(edtEmail.getText().toString())){
  22. Snackbar.make(rootLayout,"Please enter email",Snackbar.LENGTH_SHORT).show();
  23. return;
  24. }
  25. if (TextUtils.isEmpty(edtPassword.getText().toString())){
  26. Snackbar.make(rootLayout,"Please enter Password",Snackbar.LENGTH_SHORT).show();
  27. return;
  28. }
  29.  
  30. if (edtPassword.getText().toString().length() < 6){
  31. Snackbar.make(rootLayout,"Password is too short",Snackbar.LENGTH_SHORT).show();
  32. return;
  33. }
  34. //Login
  35. auth.signInWithEmailAndPassword(edtEmail.getText().toString(),edtPassword.getText().toString())
  36. .addOnSuccessListener(new OnSuccessListener<AuthResult>() {
  37. @Override
  38. public void onSuccess(AuthResult authResult) {
  39. startActivity(new Intent(MainActivity.this, Welcome.class));
  40. finish();
  41. }
  42. }).addOnFailureListener(new OnFailureListener() {
  43. @Override
  44. public void onFailure(@NonNull Exception e) {
  45. Snackbar.make(rootLayout,"Failed "+e.getMessage(), Snackbar.LENGTH_SHORT).show();
  46. }
  47. });
  48.  
  49. }
  50. });
  51. dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
  52. @Override
  53. public void onClick(DialogInterface dialogInterface, int i) {
  54. dialogInterface.dismiss();
  55. }
  56. });
  57. dialog.show();
  58. }
  59. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  60.  
  61.  
  62.  
  63.  
  64. private void showRgisterDialog() {
  65. final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
  66. dialog.setTitle("REGISTER ");
  67. dialog.setMessage("Please use email to register");
  68. LayoutInflater inflater = LayoutInflater.from(this);
  69. View register_layout = inflater.inflate(R.layout.layout_register,null);
  70.  
  71. final MaterialEditText edtFullName = register_layout.findViewById(R.id.edtFullName);
  72. final MaterialEditText edtEmail = register_layout.findViewById(R.id.edtEmail);
  73. final MaterialEditText edtAge = register_layout.findViewById(R.id.edtAge);
  74. final MaterialEditText edtPassword = register_layout.findViewById(R.id.edtPassword);
  75. final MaterialEditText edtPhone = register_layout.findViewById(R.id.edtPhone);
  76. final MaterialEditText edtCity = register_layout.findViewById(R.id.edtCity);
  77. final MaterialEditText edtStreet = register_layout.findViewById(R.id.edtStreet);
  78. final MaterialEditText edtHouseNum = register_layout.findViewById(R.id.edtHouseNum);
  79. final Switch newDriver = register_layout.findViewById(R.id.newDriver);
  80. newDriver.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  81. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  82. // do something, the isChecked will be
  83. // true if the switch is in the On position
  84. isNewDriver = "true";
  85. }
  86. });
  87.  
  88.  
  89. dialog.setView(register_layout);
  90.  
  91. //Set button
  92. dialog.setPositiveButton("REGISTER", new DialogInterface.OnClickListener() {
  93. @Override
  94. public void onClick(DialogInterface dialogInterface, int i) {
  95. dialogInterface.dismiss();
  96.  
  97. //Check validation
  98. if (TextUtils.isEmpty(edtFullName.getText().toString())){
  99. Snackbar.make(rootLayout,"Please enter first name",Snackbar.LENGTH_SHORT).show();
  100. return;
  101. }
  102.  
  103. if (TextUtils.isEmpty(edtEmail.getText().toString())){
  104. Snackbar.make(rootLayout,"Please enter email",Snackbar.LENGTH_SHORT).show();
  105. return;
  106. }
  107.  
  108.  
  109. if (TextUtils.isEmpty(edtPassword.getText().toString())){
  110. Snackbar.make(rootLayout,"Please enter Password",Snackbar.LENGTH_SHORT).show();
  111. return;
  112. }
  113.  
  114. if (edtPassword.getText().toString().length() < 6){
  115. Snackbar.make(rootLayout,"Password is too short",Snackbar.LENGTH_SHORT).show();
  116. return;
  117. }
  118.  
  119.  
  120. if (TextUtils.isEmpty(edtPhone.getText().toString())){
  121. Snackbar.make(rootLayout,"Please enter phone number",Snackbar.LENGTH_SHORT).show();
  122. return;
  123. }
  124.  
  125. //Register new user
  126. auth.createUserWithEmailAndPassword(edtEmail.getText().toString(), edtPassword.getText().toString())
  127. .addOnSuccessListener(new OnSuccessListener<AuthResult>() {
  128.  
  129. @Override
  130.  
  131. public void onSuccess(AuthResult authResult){
  132. //Save user to db
  133. User user = new User(edtFullName.getText().toString(),edtEmail.getText().toString(),edtPassword.getText().toString(),edtPhone.getText().toString(),edtAge.getText().toString(),edtCity.getText().toString(),edtStreet.getText().toString(),edtHouseNum.getText().toString(), image, isNewDriver);
  134. //Use email to key
  135. users.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
  136. .setValue(user)
  137. .addOnSuccessListener(new OnSuccessListener<Void>() {
  138. @Override
  139. public void onSuccess(Void aVoid) {
  140. Snackbar.make(rootLayout,"Registeration made successfully",Snackbar.LENGTH_SHORT).show();
  141. }
  142. })
  143. .addOnFailureListener(new OnFailureListener() {
  144. @Override
  145. public void onFailure(@NonNull Exception e) {
  146. Snackbar.make(rootLayout,"Failed"+e.getMessage(),Snackbar.LENGTH_SHORT).show();
  147. }
  148. });
  149.  
  150. }
  151. }).addOnFailureListener(new OnFailureListener() {
  152. @Override
  153. public void onFailure(@NonNull Exception e) {
  154. Snackbar.make(rootLayout,"Failed"+e.getMessage(),Snackbar.LENGTH_SHORT).show();
  155. }
  156. });
  157.  
  158.  
  159. }
  160. });
  161.  
  162. dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
  163. @Override
  164. public void onClick(DialogInterface dialogInterface, int i) {
  165. dialogInterface.dismiss();
  166. }
  167. });
  168.  
  169. dialog.show();
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement