Advertisement
Guest User

Untitled

a guest
Jan 10th, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.52 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2.  
  3. MaterialEditText edtNewUser, edtNewPassword, edtNewEmail; //sign up
  4. MaterialEditText edtUser, edtPassword; //for sign in
  5.  
  6. Button btnSignUp, btnSignIn;
  7.  
  8.  
  9. FirebaseDatabase database;
  10. DatabaseReference users;
  11.  
  12.  
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17.  
  18. //Firebase
  19. database= FirebaseDatabase.getInstance();
  20. if(){
  21. users = database.getReference("Students");
  22. }
  23. else {
  24. users = database.getReference("Teachers");
  25. }
  26.  
  27.  
  28. edtUser = (MaterialEditText)findViewById(R.id.edtUser);
  29. edtPassword = (MaterialEditText)findViewById(R.id.edtPassword);
  30.  
  31. btnSignIn = (Button)findViewById(R.id.btn_sign_in);
  32. btnSignUp = (Button)findViewById(R.id.btn_sign_up);
  33.  
  34. btnSignUp.setOnClickListener(new View.OnClickListener() {
  35. @Override
  36. public void onClick(View view) {
  37. showSignUpDialog();
  38. }
  39. });
  40.  
  41. btnSignIn.setOnClickListener(new View.OnClickListener() {
  42. @Override
  43. public void onClick(View view) {
  44. signIn(edtUser.getText().toString(), edtPassword.getText().toString());
  45. }
  46. });
  47.  
  48. }
  49.  
  50. private void signIn(final String user,final String pwd) {
  51. users.addListenerForSingleValueEvent(new ValueEventListener() {
  52. @Override
  53. public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  54. if(dataSnapshot.child(user).exists())
  55. {
  56. if(!user.isEmpty())
  57. {
  58. User login = dataSnapshot.child(user).getValue(User.class);
  59. if(login.getPassword().equals(pwd))
  60. {
  61. Toast.makeText(MainActivity.this, "Login ok!", Toast.LENGTH_SHORT).show();
  62. }
  63. else
  64. {
  65. Toast.makeText(MainActivity.this, "Wrong password", Toast.LENGTH_SHORT).show();
  66. }
  67. }
  68. else
  69. {
  70. Toast.makeText(MainActivity.this, "Please enter your user name", Toast.LENGTH_SHORT).show();
  71. }
  72. }
  73. else
  74. {
  75. Toast.makeText(MainActivity.this, "User is not existing!", Toast.LENGTH_SHORT).show();
  76. }
  77. }
  78.  
  79. @Override
  80. public void onCancelled(@NonNull DatabaseError databaseError) {
  81.  
  82. }
  83. });
  84. }
  85.  
  86. private void showSignUpDialog() {
  87. AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
  88. alertDialog.setTitle("Sign up");
  89. alertDialog.setMessage("Please fill full information");
  90.  
  91. LayoutInflater inflater = this.getLayoutInflater();
  92. View sign_up_layout = inflater.inflate(R.layout.sign_up_layout, null);
  93.  
  94. edtNewUser = (MaterialEditText)sign_up_layout.findViewById(R.id.edtNewUserName);
  95. edtNewPassword = (MaterialEditText)sign_up_layout.findViewById(R.id.edtNewPassword);
  96. edtNewEmail = (MaterialEditText)sign_up_layout.findViewById(R.id.edtNewEmail);
  97.  
  98. alertDialog.setView(sign_up_layout);
  99.  
  100.  
  101. alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
  102. @Override
  103. public void onClick(DialogInterface dialogInterface, int i) {
  104. dialogInterface.dismiss();
  105. }
  106. });
  107.  
  108. alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
  109. @Override
  110. public void onClick(DialogInterface dialogInterface, int i) {
  111. final User user = new User(edtNewUser.getText().toString(), edtNewPassword.getText().toString(), edtNewEmail.getText().toString());
  112.  
  113. users.addListenerForSingleValueEvent(new ValueEventListener() {
  114. @Override
  115. public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  116. if(dataSnapshot.child(user.getUserName()).exists()){
  117. Toast.makeText(MainActivity.this, "User already exists!", Toast.LENGTH_SHORT).show();
  118. }
  119. else {
  120. users.child(user.getUserName()).setValue(user);
  121. Toast.makeText(MainActivity.this, "User registration success!", Toast.LENGTH_SHORT).show();
  122. }
  123. }
  124.  
  125. @Override
  126. public void onCancelled(@NonNull DatabaseError databaseError) {
  127.  
  128. }
  129. });
  130. dialogInterface.dismiss();
  131. }
  132. });
  133. alertDialog.show();
  134. }
  135. }
  136.  
  137.  
  138. ****************************************
  139.  
  140. package com.cristi.logintest.Model;
  141.  
  142. public class User {
  143.  
  144. private String userName;
  145. private String password;
  146. private String email;
  147.  
  148. public User(){
  149. }
  150.  
  151. public User(String userName, String password, String email) {
  152. this.userName = userName;
  153. this.password = password;
  154. this.email = email;
  155. }
  156.  
  157. public String getUserName() {
  158. return userName;
  159. }
  160.  
  161. public void setUserName(String userName) {
  162. this.userName = userName;
  163. }
  164.  
  165. public String getPassword() {
  166. return password;
  167. }
  168.  
  169. public void setPassword(String password) {
  170. this.password = password;
  171. }
  172.  
  173. public String getEmail() {
  174. return email;
  175. }
  176.  
  177. public void setEmail(String email) {
  178. this.email = email;
  179. }
  180. }
  181.  
  182.  
  183. *************************************
  184.  
  185. <?xml version="1.0" encoding="utf-8"?>
  186. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  187. xmlns:app="http://schemas.android.com/apk/res-auto"
  188. xmlns:tools="http://schemas.android.com/tools"
  189. android:layout_width="match_parent"
  190. android:layout_height="match_parent"
  191. android:background="@color/colorPrimary"
  192. tools:context=".MainActivity">
  193.  
  194.  
  195. <RelativeLayout
  196.  
  197. android:layout_width="match_parent"
  198. android:layout_height="wrap_content"
  199. app:cardElevation="4dp"
  200. android:layout_margin="8dp"
  201. app:layout_constraintBottom_toBottomOf="parent"
  202. app:layout_constraintLeft_toLeftOf="parent"
  203. app:layout_constraintRight_toRightOf="parent"
  204. app:layout_constraintTop_toTopOf="parent" >
  205.  
  206. <android.support.v7.widget.CardView
  207. android:id="@+id/info_login"
  208. xmlns:android="http://schemas.android.com/apk/res/android"
  209. xmlns:app="http://schemas.android.com/apk/res-auto"
  210. android:layout_width="match_parent"
  211. android:layout_height="wrap_content"
  212. android:layout_margin="8dp"
  213. app:cardElevation="4dp">
  214. <LinearLayout
  215. android:padding="16dp"
  216. android:orientation="vertical"
  217. android:layout_width="match_parent"
  218. android:layout_height="wrap_content">
  219.  
  220. <com.rengwuxian.materialedittext.MaterialEditText
  221. android:id="@+id/edtUser"
  222. android:hint="User name"
  223. android:textColorHint="@color/colorPrimary"
  224. android:textColor="@color/colorPrimary"
  225. android:textSize="24sp"
  226. android:layout_width="match_parent"
  227. android:layout_height="wrap_content"
  228. app:met_baseColor="@color/colorPrimary"
  229. app:met_floatingLabel="highlight"
  230. app:met_primaryColor="@color/colorPrimary"
  231. app:met_singleLineEllipsis="true"
  232. />
  233.  
  234. <com.rengwuxian.materialedittext.MaterialEditText
  235. android:id="@+id/edtPassword"
  236. android:hint="Password"
  237. android:textColorHint="@color/colorPrimary"
  238. android:textColor="@color/colorPrimary"
  239. android:textSize="24sp"
  240. android:inputType="textPassword"
  241. android:layout_width="match_parent"
  242. android:layout_height="wrap_content"
  243. app:met_baseColor="@color/colorPrimary"
  244. app:met_floatingLabel="highlight"
  245. app:met_primaryColor="@color/colorPrimary"
  246. app:met_singleLineEllipsis="true"
  247. />
  248.  
  249.  
  250.  
  251.  
  252. </LinearLayout>
  253. </android.support.v7.widget.CardView>
  254. <LinearLayout
  255. android:layout_below="@id/info_login"
  256. android:orientation="horizontal"
  257. android:weightSum="2"
  258. android:layout_margin="8dp"
  259. android:layout_width="match_parent"
  260. android:layout_height="wrap_content">
  261.  
  262. <Button
  263. android:id="@+id/btn_sign_up"
  264. android:text="Sign up"
  265. style="@style/Widget.AppCompat.Button.Colored"
  266. android:layout_weight="1"
  267. android:layout_width="0dp"
  268. android:layout_height="wrap_content" />
  269.  
  270. <Button
  271. android:id="@+id/btn_sign_in"
  272. android:text="Sign in"
  273. style="@style/Widget.AppCompat.Button.Colored"
  274. android:layout_weight="1"
  275. android:layout_width="0dp"
  276. android:layout_height="wrap_content" />
  277.  
  278.  
  279. </LinearLayout>
  280.  
  281. </RelativeLayout>
  282.  
  283.  
  284.  
  285. </android.support.constraint.ConstraintLayout>
  286.  
  287.  
  288. **************************************
  289.  
  290. <?xml version="1.0" encoding="utf-8"?>
  291. <android.support.v7.widget.CardView
  292. xmlns:android="http://schemas.android.com/apk/res/android"
  293. xmlns:app="http://schemas.android.com/apk/res-auto"
  294. android:layout_width="match_parent"
  295. android:layout_height="wrap_content"
  296. android:layout_margin="8dp"
  297. app:cardElevation="4dp">
  298.  
  299. <LinearLayout
  300. android:orientation="vertical"
  301. android:layout_width="match_parent"
  302. android:layout_height="wrap_content">
  303.  
  304. <com.rengwuxian.materialedittext.MaterialEditText
  305. android:id="@+id/edtNewUserName"
  306. android:hint="User name"
  307. android:textColorHint="@color/colorPrimary"
  308. android:textColor="@color/colorPrimary"
  309. android:textSize="24sp"
  310. android:layout_width="match_parent"
  311. android:layout_height="wrap_content"
  312. app:met_baseColor="@color/colorPrimary"
  313. app:met_floatingLabel="highlight"
  314. app:met_primaryColor="@color/colorPrimary"
  315. app:met_singleLineEllipsis="true"
  316. />
  317.  
  318. <com.rengwuxian.materialedittext.MaterialEditText
  319. android:id="@+id/edtNewPassword"
  320. android:hint="Password"
  321. android:textColorHint="@color/colorPrimary"
  322. android:textColor="@color/colorPrimary"
  323. android:textSize="24sp"
  324. android:inputType="textPassword"
  325. android:layout_width="match_parent"
  326. android:layout_height="wrap_content"
  327. app:met_baseColor="@color/colorPrimary"
  328. app:met_floatingLabel="highlight"
  329. app:met_primaryColor="@color/colorPrimary"
  330. app:met_singleLineEllipsis="true"
  331. />
  332.  
  333. <com.rengwuxian.materialedittext.MaterialEditText
  334. android:id="@+id/edtNewEmail"
  335. android:hint="Email"
  336. android:textColorHint="@color/colorPrimary"
  337. android:textColor="@color/colorPrimary"
  338. android:textSize="24sp"
  339. android:inputType="textEmailAddress"
  340. android:layout_width="match_parent"
  341. android:layout_height="wrap_content"
  342. app:met_baseColor="@color/colorPrimary"
  343. app:met_floatingLabel="highlight"
  344. app:met_primaryColor="@color/colorPrimary"
  345. app:met_singleLineEllipsis="true"
  346. />
  347.  
  348.  
  349. </LinearLayout>
  350.  
  351. </android.support.v7.widget.CardView>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement