Guest User

Untitled

a guest
Jan 17th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.54 KB | None | 0 0
  1. setContentView(R.layout.activity_main);
  2.  
  3. Button btnSignIn,btnRegister;
  4. RelativeLayout rootLayout;
  5.  
  6.  
  7. FirebaseAuth auth;
  8. FirebaseDatabase db;
  9. DatabaseReference users;
  10.  
  11. @Override
  12. protected void attachBaseContext(Context newBase) {
  13. super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
  14. }
  15.  
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
  20. .setDefaultFontPath("fonts/Arkhip_font.ttf")
  21. .setFontAttrId(R.attr.fontPath)
  22. .build());
  23. setContentView(R.layout.activity_main);
  24.  
  25. //initialization of firebase
  26.  
  27. auth = FirebaseAuth.getInstance();
  28. db=FirebaseDatabase.getInstance();
  29. users=db.getReference("Users");
  30.  
  31. btnRegister =(Button)findViewById(R.id.btnRegister);
  32. btnSignIn=(Button)findViewById(R.id.btnSignIn);
  33. rootLayout= (RelativeLayout) findViewById(R.id.rootLayoutMax);
  34.  
  35. //Event
  36. btnRegister.setOnClickListener(new View.OnClickListener() {
  37. @Override
  38. public void onClick(View view) {
  39. showRegisterDialog();
  40. }
  41. });
  42.  
  43. btnSignIn.setOnClickListener(new View.OnClickListener() {
  44. @Override
  45. public void onClick(View view) {
  46. showLoginDialog();
  47. }
  48. });
  49.  
  50. }
  51.  
  52. private void showLoginDialog() {
  53. final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
  54. dialog.setTitle("SIGN IN ");
  55. dialog.setMessage("Please use email to sign in");
  56.  
  57. LayoutInflater inflater = LayoutInflater.from(this);
  58. View login_layout = inflater.inflate(R.layout.layout_login,null);
  59.  
  60. final MaterialEditText edtEmail = login_layout.findViewById(R.id.edtEmail);
  61. final MaterialEditText edtPassword = login_layout.findViewById(R.id.edtPassword);
  62.  
  63. dialog.setView(login_layout);
  64.  
  65. //Setting Button
  66.  
  67. dialog.setPositiveButton("SIGN IN", new DialogInterface.OnClickListener() {
  68. @Override
  69. public void onClick(DialogInterface dialogInterface, int i) {
  70.  
  71. dialogInterface.dismiss();
  72.  
  73. //Check validation
  74. if (TextUtils.isEmpty(edtEmail.getText().toString())) {
  75. Snackbar.make(rootLayout, "Please enter email address", Snackbar.LENGTH_LONG).show();
  76. return;
  77.  
  78. }
  79.  
  80.  
  81. if (TextUtils.isEmpty(edtPassword.getText().toString())) {
  82. Snackbar.make(rootLayout, "Please enter password", Snackbar.LENGTH_LONG).show();
  83. return;
  84.  
  85. }
  86. if (edtPassword.getText().toString().length() < 6) {
  87. Snackbar.make(rootLayout, "Password too short", Snackbar.LENGTH_LONG).show();
  88. return;
  89.  
  90. }
  91.  
  92. //Login
  93. auth.signInWithEmailAndPassword(edtEmail.getText().toString(),edtPassword.getText().toString())
  94. .addOnSuccessListener(new OnSuccessListener<AuthResult>() {
  95. @Override
  96. public void onSuccess(AuthResult authResult) {
  97. startActivity(new Intent(MainActivity.this,Welcome.class));
  98. finish();
  99.  
  100. }
  101. }).addOnFailureListener(new OnFailureListener() {
  102. @Override
  103. public void onFailure(@NonNull Exception e) {
  104. Snackbar.make(rootLayout,"Failed"+e.getMessage(),Snackbar.LENGTH_SHORT)
  105. .show();
  106.  
  107. }
  108. });
  109. }
  110. });
  111. dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
  112. @Override
  113. public void onClick(DialogInterface dialogInterface, int i) {
  114. dialogInterface.dismiss();
  115. }
  116. });
  117.  
  118.  
  119. dialog.show();
  120. }
  121.  
  122. private void showRegisterDialog() {
  123. final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
  124. dialog.setTitle("REGISTER ");
  125. dialog.setMessage("Please use email to register");
  126.  
  127. LayoutInflater inflater = LayoutInflater.from(this);
  128. View register_layout = inflater.inflate(R.layout.layout_register,null);
  129.  
  130. final MaterialEditText edtEmail = register_layout.findViewById(R.id.edtEmail);
  131. final MaterialEditText edtPassword = register_layout.findViewById(R.id.edtPassword);
  132. final MaterialEditText edtName = register_layout.findViewById(R.id.edtName);
  133. final MaterialEditText edtPhone = register_layout.findViewById(R.id.edtPhone);
  134.  
  135. dialog.setView(register_layout);
  136.  
  137. //Setting Button
  138.  
  139. dialog.setPositiveButton("REGISTER", new DialogInterface.OnClickListener() {
  140. @Override
  141. public void onClick(DialogInterface dialogInterface, int i) {
  142.  
  143. dialogInterface.dismiss();
  144.  
  145. //Check validation
  146. if(TextUtils.isEmpty(edtEmail.getText().toString())){
  147. Snackbar.make(rootLayout,"Please enter email address",Snackbar.LENGTH_LONG).show();
  148. return;
  149.  
  150. }
  151.  
  152. if(TextUtils.isEmpty(edtPhone.getText().toString())){
  153. Snackbar.make(rootLayout,"Please enter phone number",Snackbar.LENGTH_LONG).show();
  154. return;
  155.  
  156. }
  157. if(TextUtils.isEmpty(edtPassword.getText().toString())){
  158. Snackbar.make(rootLayout,"Please enter password",Snackbar.LENGTH_LONG).show();
  159. return;
  160.  
  161. }
  162. if(edtPassword.getText().toString().length() < 6){
  163. Snackbar.make(rootLayout,"Password too short",Snackbar.LENGTH_LONG).show();
  164. return;
  165.  
  166. }
  167.  
  168. //Register new user
  169. auth.createUserWithEmailAndPassword(edtEmail.getText().toString(),edtPassword.getText().toString())
  170. .addOnSuccessListener(new OnSuccessListener<AuthResult>() {
  171. @Override
  172. public void onSuccess(AuthResult authResult) {
  173. //That will save user to db
  174. User user = new User();
  175. user.setEmail(edtEmail.getText().toString());
  176. user.setPassword(edtPassword.getText().toString());
  177. user.setName(edtName.getText().toString());
  178. user.setPhone(edtPhone.getText().toString());
  179.  
  180.  
  181. //It will use email to key
  182. users.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
  183. .setValue(user)
  184. .addOnSuccessListener(new OnSuccessListener<Void>() {
  185. @Override
  186. public void onSuccess(Void aVoid) {
  187. Snackbar.make(rootLayout,"Register success fully",Snackbar.LENGTH_LONG).show();
  188.  
  189. }
  190. })
  191.  
  192. .addOnFailureListener(new OnFailureListener() {
  193. @Override
  194. public void onFailure(@NonNull Exception e) {
  195. Snackbar.make(rootLayout,"Failed"+e.getMessage(),Snackbar.LENGTH_LONG).show();
  196.  
  197. }
  198. });
  199.  
  200.  
  201.  
  202. }
  203. })
  204.  
  205. .addOnFailureListener(new OnFailureListener() {
  206. @Override
  207. public void onFailure(@NonNull Exception e) {
  208.  
  209. Snackbar.make(rootLayout,"Failed"+e.getMessage(),Snackbar.
  210. LENGTH_LONG).show();
  211.  
  212. }
  213. });
  214.  
  215.  
  216. }
  217. });
  218.  
  219. dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
  220. @Override
  221. public void onClick(DialogInterface dialogInterface, int i) {
  222. dialogInterface.dismiss();
  223. }
  224. });
  225.  
  226. dialog.show();
  227.  
  228. }
  229.  
  230. <?xml version="1.0" encoding="utf-8"?>
  231.  
  232.  
  233. <ripple
  234. xmlns:android="http://schemas.android.com/apk/res/android"
  235. xmlns:tools="http://schemas.android.com/tools"
  236. android:color="@color/rippleEffectColor"
  237. tools:targetApi="lollipop"
  238.  
  239. >
  240.  
  241. <item>
  242.  
  243. <shape android:shape="rectangle">
  244. <solid android:color="@android:color/white"></solid>
  245. <stroke android:color="@android:color/white" android:width="2dp">
  246. </stroke>
  247. <corners android:radius="2dp"/>
  248.  
  249. </shape>
  250.  
  251. </item>
  252.  
  253.  
  254.  
  255. </ripple>
Add Comment
Please, Sign In to add comment