Advertisement
Guest User

login HomeWork

a guest
Mar 11th, 2016
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.51 KB | None | 0 0
  1. =====================================xml activity_man==========================================================
  2. <RelativeLayout
  3. android:layout_height="match_parent"
  4. android:layout_width="match_parent"
  5. android:id="@+id/mainTag"
  6. xmlns:android="http://schemas.android.com/apk/res/android">
  7. <EditText
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:textSize="30sp"
  11. android:hint="@string/hintUser"
  12. android:gravity="center"
  13. android:id="@+id/inUser"
  14. android:layout_marginTop="15sp"
  15. />
  16. <EditText
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:textSize="30sp"
  20. android:inputType="textPassword"
  21. android:hint="@string/hintPass"
  22. android:gravity="center"
  23. android:id="@+id/inPass"
  24. android:layout_below="@+id/inUser"
  25. android:layout_marginTop="5sp"
  26. />
  27. <Button
  28. android:layout_width="match_parent"
  29. android:layout_height="wrap_content"
  30. android:textSize="30sp"
  31. android:text="@string/btnLog"
  32. android:id="@+id/btnLog"
  33. android:layout_below="@+id/inPass"
  34. android:layout_marginTop="40sp"
  35. android:padding="20sp"
  36. android:onClick="btnLogIn"
  37. />
  38. <Button
  39. android:layout_width="match_parent"
  40. android:layout_height="wrap_content"
  41. android:text="@string/btnRegi"
  42. android:textSize="25sp"
  43. android:id="@+id/btnReg"
  44. android:layout_below="@+id/btnLog"
  45. android:layout_marginTop="10sp"
  46. android:padding="10sp"
  47. android:onClick="btnRegister"
  48. />
  49. </RelativeLayout>
  50. =============================================================================================================================
  51. =========================================Java MainActivity===================================================================
  52. package com.example.igor.mylogin;
  53.  
  54. import android.app.Activity;
  55. import android.content.Intent;
  56. import android.content.SharedPreferences;
  57. import android.os.Bundle;
  58. import android.view.View;
  59. import android.widget.EditText;
  60. import android.widget.Toast;
  61.  
  62. public class MainActivity extends Activity
  63. {
  64. public final static String PREFERENCE = "account";//name of the sharedprefrences file!
  65.  
  66. private SharedPreferences myPrefs; //calling SharedPrefrences Class
  67. private SharedPreferences.Editor editor; //calling the editor for SharedPrefrences
  68.  
  69. private EditText userTxt; //username input
  70. private EditText passTxt; //password input
  71.  
  72. private static String chkOnline; //will be used to check if the user is online!
  73.  
  74. @Override
  75. protected void onCreate(Bundle savedInstanceState)
  76. {
  77. super.onCreate(savedInstanceState);
  78. setContentView(R.layout.activity_main);
  79. setPoints();
  80.  
  81. /*check if user exists or not!
  82. if user exists go to login page automaticly
  83. */
  84.  
  85. if(chkUser())
  86. {
  87. //if keyfound start second activity
  88. Intent logIntent = new Intent(this, Logged.class);
  89. logIntent.putExtra("user", chkOnline);
  90. this.startActivity(logIntent);
  91. }
  92.  
  93. }
  94.  
  95. //ASSISTANCE METHODS
  96.  
  97. //method that casts all our local variables with the ids of xml
  98. private void setPoints()
  99. {
  100. userTxt = (EditText)findViewById(R.id.inUser);
  101. passTxt = (EditText)findViewById(R.id.inPass);
  102. myPrefs = getApplicationContext().getSharedPreferences(PREFERENCE,MODE_PRIVATE);
  103. editor = myPrefs.edit();
  104. }
  105.  
  106. private boolean chkUser()
  107. {
  108. chkOnline = myPrefs.getString("logged", "error");
  109. if(chkOnline.equals("error"))
  110. {
  111. return false;
  112. }
  113. else
  114. {
  115. return true;
  116. }
  117. }
  118.  
  119. //inputs the username into logged
  120. //creates a value of username in the key logged making it appear full and not defualt
  121. private void apprOnline(String user)
  122. {
  123. editor.putString("logged",user);
  124. editor.commit();
  125. }
  126.  
  127. //method that checks if the user filled all the fields, if the username is taken
  128. //if tha password matches and creates a user if all statements above are done right
  129. private void chkLog()
  130. {
  131. String user = userTxt.getText().toString();
  132. String pass = passTxt.getText().toString();
  133.  
  134. if(user.trim().length() > 0 && pass.trim().length()>0)
  135. {
  136. String userName = null;
  137. String userPass = null;
  138.  
  139. //if myPrefs contains username and the password input them into
  140. //userName and userPass Variables
  141.  
  142. if(myPrefs.contains("user") && myPrefs.contains("password") )
  143. {
  144. userName = myPrefs.getString("user","");
  145. userPass = myPrefs.getString("password","");
  146. }
  147.  
  148.  
  149. //check if pass word and username match!
  150.  
  151. if(user.equals(userName) && pass.equals(userPass))
  152. {
  153. //pop a toast welcome
  154. Toast.makeText(this,"Logged in succesfully",Toast.LENGTH_SHORT).show();
  155. //if the statement is true put a String value into logged
  156. apprOnline(user);
  157. // move to logged page
  158. Intent logIn = new Intent(this, Logged.class);
  159. this.startActivity(logIn);
  160. }
  161. else
  162. {
  163. //if pass and user do not match pop a toast
  164. Toast.makeText(this,"Wrong Username or Password",Toast.LENGTH_LONG).show();
  165. }
  166. }
  167. else
  168. {
  169. //user didnt add username or password
  170. Toast.makeText(this,"Please enter Username and Password",Toast.LENGTH_LONG).show();
  171. }
  172. }
  173.  
  174. //BUTTONS
  175.  
  176. //button that activates the the chkLog method and checks if all the fields and data are valid!
  177. public void btnLogIn(View v)
  178. {
  179. chkLog();
  180. }
  181.  
  182. //button that takes the user to the registery page
  183. public void btnRegister(View v)
  184. {
  185. Intent rgstIntent = new Intent(this,Register.class);
  186. this.startActivity(rgstIntent);
  187. }
  188.  
  189. }
  190.  
  191. =======================================================================================================================================
  192. ==============================================xml activity_register====================================================================
  193. <RelativeLayout
  194. android:layout_height="match_parent"
  195. android:layout_width="match_parent"
  196. xmlns:android="http://schemas.android.com/apk/res/android">
  197. <TextView
  198. android:layout_width="match_parent"
  199. android:layout_height="wrap_content"
  200. android:text="Registration"
  201. android:gravity="center"
  202. android:textSize="20sp"
  203. android:id="@+id/regLine"
  204. android:layout_marginTop="20sp"
  205. />
  206. <EditText
  207. android:layout_width="match_parent"
  208. android:layout_height="wrap_content"
  209. android:hint="@string/hintUser"
  210. android:gravity="center"
  211. android:textSize="30sp"
  212. android:id="@+id/setUser"
  213. android:layout_below="@+id/regLine"
  214. android:layout_marginTop="15sp"
  215. />
  216.  
  217. <EditText
  218. android:layout_width="match_parent"
  219. android:layout_height="wrap_content"
  220. android:hint="@string/hintPass"
  221. android:inputType="textPassword"
  222. android:gravity="center"
  223. android:textSize="30sp"
  224. android:id="@+id/setPass"
  225. android:layout_below="@+id/setUser"
  226. android:layout_marginTop="30sp" />
  227.  
  228. <EditText
  229. android:layout_width="match_parent"
  230. android:layout_height="wrap_content"
  231. android:hint="@string/confPass"
  232. android:inputType="textPassword"
  233. android:gravity="center"
  234. android:textSize="30sp"
  235. android:id="@+id/confPass"
  236. android:layout_below="@+id/setPass"
  237. android:layout_marginTop="30sp" />
  238.  
  239. <Button
  240. android:layout_width="match_parent"
  241. android:layout_height="wrap_content"
  242. android:text="@string/btnRegi"
  243. android:textSize="30sp"
  244. android:id="@+id/tryReg"
  245. android:layout_below="@+id/confPass"
  246. android:layout_marginTop="10sp"
  247. android:onClick="btnRegister"
  248. />
  249. <Button
  250. android:layout_width="match_parent"
  251. android:layout_height="wrap_content"
  252. android:text="@string/canc"
  253. android:textSize="20sp"
  254. android:id="@+id/regCanc"
  255. android:layout_below="@+id/tryReg"
  256. android:layout_marginTop="20sp"
  257. android:onClick="btnCancle"
  258. />
  259.  
  260.  
  261. </RelativeLayout>
  262. ===================================================================================================================================
  263. ==========================================Java Register============================================================================
  264. package com.example.igor.mylogin;
  265.  
  266. import android.app.Activity;
  267. import android.content.Intent;
  268. import android.content.SharedPreferences;
  269. import android.os.Bundle;
  270. import android.view.View;
  271. import android.widget.EditText;
  272. import android.widget.Toast;
  273.  
  274. /**
  275. * Created by Igor on 3/7/2016.
  276. */
  277. public class Register extends Activity
  278. {
  279. private SharedPreferences myPrefs;//calling SharedPrefrences Class
  280. private SharedPreferences.Editor editor;//calling the editor for SharedPrefrences
  281.  
  282.  
  283. private EditText rgsUser; //the name a user wish to register with
  284. private EditText rgsPass; //the password user wishes to use
  285. private EditText chkPass; //check if user entered the password correctly
  286.  
  287.  
  288. @Override
  289. protected void onCreate(Bundle savedInstanceState)
  290. {
  291. super.onCreate(savedInstanceState);
  292. setContentView(R.layout.activity_register);
  293. setPoints();
  294. }
  295.  
  296. //sets all the points connects our local variables with xml id's
  297. private void setPoints()
  298. {
  299. rgsUser = (EditText)findViewById(R.id.setUser);
  300. rgsPass = (EditText)findViewById(R.id.setPass);
  301. chkPass = (EditText)findViewById(R.id.confPass);
  302. myPrefs = getApplicationContext().getSharedPreferences(MainActivity.PREFERENCE, MODE_PRIVATE);
  303. editor = myPrefs.edit();
  304. }
  305.  
  306. //method that checks all users data inpouted in the fields and registration of account if
  307. //data is valid
  308. private void chkReg()
  309. {
  310. String userName = rgsUser.getText().toString();
  311. String passWord = rgsPass.getText().toString();
  312. String checkPass = chkPass.getText().toString();
  313. String chkUser = null;// will be used to check if the name is taken
  314.  
  315.  
  316.  
  317.  
  318. if(userName.trim().length()<=0 || passWord.trim().length()<=0 || checkPass.trim().length()<=0)
  319. {
  320. //if one of the fields is empty pop toast message
  321. Toast.makeText(this,"Error, please fill all the fields",Toast.LENGTH_LONG).show();
  322. }
  323. else if(!checkPass.equals(passWord))
  324. {
  325. //if confirm password doesnt equal to the password pop toast message
  326. Toast.makeText(this,"Error, please make sure the passwords match!",Toast.LENGTH_LONG).show();
  327. }
  328. else if(userName.equals(myPrefs.getString("user",chkUser)))
  329. {
  330. Toast.makeText(this,"Error, This username is already taken",Toast.LENGTH_LONG).show();
  331. }
  332. else
  333. {
  334. //pop message that data has been saved successfully!
  335. Toast.makeText(this,"Success! Account Registered!",Toast.LENGTH_LONG).show();
  336.  
  337. //if everything works save the data using the following keys
  338. editor.putString("user", userName);
  339. editor.putString("password", passWord);
  340.  
  341. //commit the file
  342. editor.commit();
  343.  
  344. //move to the main page (login scrren)
  345. Intent toLog = new Intent(this, MainActivity.class);
  346. this.startActivity(toLog);
  347. }
  348. }
  349.  
  350. //calls the chkReg method checking if all the fields and data are valid!
  351. public void btnRegister(View v)
  352. {
  353. chkReg();
  354. }
  355.  
  356. //activates the canlce button
  357. public void btnCancle(View v)
  358. {
  359. Intent cnlReg = new Intent(this,MainActivity.class);
  360. this.startActivity(cnlReg);
  361. }
  362.  
  363.  
  364. }
  365. =============================================================================================================================
  366. =============================================xml activity_logged=============================================================
  367. <RelativeLayout
  368. android:layout_height="wrap_content"
  369. android:layout_width="match_parent"
  370. android:id="@+id/logPage"
  371. xmlns:android="http://schemas.android.com/apk/res/android">
  372. <TextView
  373. android:layout_width="match_parent"
  374. android:layout_height="wrap_content"
  375. android:id="@+id/logMsg"
  376. android:layout_marginTop="25sp"
  377. android:textSize="50sp"
  378. />
  379. <Button
  380. android:layout_width="match_parent"
  381. android:layout_height="wrap_content"
  382. android:id="@+id/btnOut"
  383. android:layout_below="@+id/logMsg"
  384. android:text="@string/btnLogOut"
  385. android:textSize="30sp"
  386. android:gravity="center"
  387. android:layout_marginTop="20sp"
  388. android:onClick="exitBtn"
  389. />
  390.  
  391. <TextView
  392. android:layout_width="wrap_content"
  393. android:layout_height="wrap_content"
  394. android:textAppearance="?android:attr/textAppearanceSmall"
  395. android:text="Created by Igor"
  396. android:id="@+id/whoLog"/>
  397. </RelativeLayout>
  398. ==============================================================================================================================
  399. ============================================Java Logged=======================================================================
  400. package com.example.igor.mylogin;
  401.  
  402. import android.app.Activity;
  403. import android.content.Intent;
  404. import android.content.SharedPreferences;
  405. import android.os.Bundle;
  406. import android.view.View;
  407. import android.widget.TextView;
  408.  
  409. /**
  410. * Created by Igor on 3/7/2016.
  411. */
  412. public class Logged extends Activity
  413. {
  414.  
  415. SharedPreferences myPrefs; //calling SharedPrefrences Class
  416. SharedPreferences.Editor editor;//calling the editor for SharedPrefrences
  417.  
  418. TextView userLogged; //what kind of user is logged in
  419.  
  420. @Override
  421. protected void onCreate(Bundle savedInstanceState)
  422. {
  423. super.onCreate(savedInstanceState);
  424. setContentView(R.layout.activity_logged);
  425. setPoints();
  426. userLogged.setText("Greetings " + getUserName());
  427. }
  428.  
  429. //ASSISTANCE METHODS
  430.  
  431. //sets all the points
  432. private void setPoints()
  433. {
  434. userLogged = (TextView)findViewById(R.id.logMsg);
  435. myPrefs = getApplicationContext().getSharedPreferences(MainActivity.PREFERENCE,MODE_PRIVATE);
  436. editor = myPrefs.edit();
  437. }
  438.  
  439. //returns a String value of key user
  440. private String getUserName()
  441. {
  442. String userName = myPrefs.getString("user","offline");
  443. return userName;
  444. }
  445.  
  446. public void exitBtn(View v)
  447. {
  448. editor.remove("logged");
  449. editor.commit();
  450.  
  451. Intent exitPage = new Intent(this, MainActivity.class);
  452. this.startActivity(exitPage);
  453. }
  454.  
  455.  
  456. }
  457. ============================================================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement