Advertisement
Guest User

Untitled

a guest
Mar 15th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.83 KB | None | 0 0
  1. ==============================================================
  2. ===============MAIN ACTIVITY CLASS============================
  3. ==============================================================
  4. package com.example.app0811.login;
  5.  
  6. import android.app.Activity;
  7. import android.content.Intent;
  8. import android.content.SharedPreferences;
  9. import android.os.Bundle;
  10. import android.view.Menu;
  11. import android.view.MenuItem;
  12. import android.view.View;
  13. import android.widget.EditText;
  14. import android.widget.Toast;
  15.  
  16. public class MainActivity extends Activity {
  17.  
  18. EditText txtUser;
  19. EditText txtPass;
  20. String userNameLogged;
  21.  
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.activity_main);
  26. txtUser = (EditText)findViewById(R.id.username);
  27. txtPass = (EditText)findViewById(R.id.pass);
  28. if (userLogged())
  29. {
  30. Intent iLogged = new Intent(this,Hello.class);
  31. iLogged.putExtra("userName",userNameLogged);
  32. this.startActivity(iLogged);
  33. }
  34. }
  35. private boolean userLogged()
  36. {
  37. SharedPreferences myPref = getApplicationContext().getSharedPreferences("myPref", MODE_PRIVATE);
  38. userNameLogged = myPref.getString("logged", "error");
  39. if (userNameLogged.equals("error"))
  40. {
  41. //we not logged in
  42. return false;
  43. }
  44. else
  45. {
  46. //we are logged in
  47. return true;
  48. }
  49. }
  50.  
  51. public void onClick (View v)
  52. {
  53. if (chkUser())
  54. {
  55. Intent helloIntent = new Intent(this , Hello.class);
  56. this.startActivity(helloIntent);
  57. }
  58. else
  59. {
  60. Toast.makeText(this,"Wrong user name or password!!",Toast.LENGTH_LONG).show();
  61. }
  62.  
  63. }
  64.  
  65. private boolean chkUser()
  66. {
  67. SharedPreferences myPref = getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
  68. String userName = myPref.getString("userName", "error");
  69. String userPass = myPref.getString("password","error");
  70. if (userName.equals(txtUser.getText().toString()) && userPass.equals(txtPass.getText().toString()))
  71. {
  72. return true;
  73. }
  74. else
  75. {
  76. return false;
  77. }
  78. }
  79.  
  80. public void btnReg (View v)
  81. {
  82. Intent intentReg = new Intent(this, Register.class);
  83. this.startActivity(intentReg);
  84. }
  85.  
  86. }
  87.  
  88.  
  89. ==============================================================
  90. ===============HELLO CLASS============================
  91. ==============================================================
  92.  
  93. package com.example.app0811.login;
  94.  
  95. import android.app.Activity;
  96. import android.content.Intent;
  97. import android.content.SharedPreferences;
  98. import android.os.Bundle;
  99. import android.view.View;
  100. import android.widget.EditText;
  101. import android.widget.TextView;
  102.  
  103. /**
  104. * Created by app0811 on 06/03/2016.
  105. */
  106. public class Hello extends Activity
  107. {
  108. SharedPreferences myPrefs; //calling SharedPrefrences Class
  109. SharedPreferences.Editor editor;//calling the editor for SharedPrefrences
  110.  
  111. TextView myTxt;
  112. protected void onCreate(Bundle savedInstanceState)
  113. {
  114. super.onCreate(savedInstanceState);
  115. setContentView(R.layout.hello);
  116. setPoints();
  117. myTxt.setText("Hello "+ getUser());
  118. }
  119.  
  120. private void setPoints()
  121. {
  122. myTxt = (TextView)findViewById(R.id.hellotxt);
  123. myPrefs = getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
  124. editor = myPrefs.edit();
  125. }
  126.  
  127. private String getUser()
  128. {
  129. String user = myPrefs.getString("userName","error");
  130. return user;
  131. }
  132.  
  133. public void btnExit(View v)
  134. {
  135. editor.remove("logged");
  136. editor.commit();
  137.  
  138. Intent exit = new Intent(this,MainActivity.class);
  139. this.startActivity(exit);
  140. }
  141.  
  142.  
  143. }
  144.  
  145. ==============================================================
  146. ===============REGISTER CLASS=================================
  147. ==============================================================
  148.  
  149. package com.example.app0811.login;
  150.  
  151. import android.app.Activity;
  152. import android.content.Intent;
  153. import android.content.SharedPreferences;
  154. import android.os.Bundle;
  155. import android.view.View;
  156. import android.widget.EditText;
  157. import android.widget.Toast;
  158.  
  159. /**
  160. * Created by app0811 on 06/03/2016.
  161. */
  162. public class Register extends Activity
  163.  
  164. {
  165. private SharedPreferences myPrefs;//calling SharedPrefrences Class
  166. private SharedPreferences.Editor editor;//calling the editor for SharedPrefrences
  167.  
  168. private EditText crUser;//create user
  169. private EditText crPass;//password
  170. private EditText confPass;//confirm password
  171.  
  172.  
  173. protected void onCreate(Bundle savedInstanceState)
  174. {
  175. super.onCreate(savedInstanceState);
  176. setContentView(R.layout.reg);
  177.  
  178. setPoint();
  179. }
  180.  
  181. private void setPoint()
  182. {
  183. crUser = (EditText)findViewById(R.id.crUser);
  184. crPass = (EditText)findViewById(R.id.crPass);
  185. confPass = (EditText)findViewById(R.id.confPass);
  186. //creating the sharedprefrences xml file
  187. myPrefs = getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
  188. editor = myPrefs.edit();
  189. }
  190.  
  191. public void creatUser(View v)
  192. {
  193. String userName = crUser.getText().toString();
  194. String userPass = crPass.getText().toString();
  195. String confirmPass = confPass.getText().toString();
  196. String chkUser = "";
  197.  
  198. if (userName.trim().length()<=0 || userPass.trim().length()<=0 || confirmPass.trim().length()<=0)
  199. {
  200. Toast.makeText(this,"Please fill all the fields above",Toast.LENGTH_LONG).show();
  201. }
  202. else if (!userPass.equals(confirmPass))
  203. {
  204. Toast.makeText(this, "The password not confirm" ,Toast.LENGTH_LONG).show();
  205. }
  206. else if(userName.equals(myPrefs.getString("username",chkUser)))
  207. {
  208. Toast.makeText(this, "This username is taken" , Toast.LENGTH_LONG).show();
  209. }
  210. else
  211. {
  212. editor.putString("userName" ,userName );
  213. editor.putString("password", userPass);
  214. editor.commit();
  215. Toast.makeText(this , "The username creating" ,Toast.LENGTH_LONG).show();
  216. Intent intentMain = new Intent(this,MainActivity.class);
  217. this.startActivity(intentMain);
  218. }
  219.  
  220. }
  221.  
  222. public void btncancel (View v)
  223. {
  224. Intent mainIntent = new Intent(this , MainActivity.class);
  225. this.startActivity(mainIntent);
  226. }
  227. }
  228.  
  229. ==================activity main xml=================
  230.  
  231. <LinearLayout
  232. android:layout_width="match_parent"
  233. android:layout_height="match_parent"
  234. android:orientation="vertical"
  235. xmlns:android="http://schemas.android.com/apk/res/android" >
  236.  
  237. <EditText
  238. android:layout_width="match_parent"
  239. android:layout_height="wrap_content"
  240. android:hint="Username"
  241. android:id="@+id/username"/>
  242. <EditText
  243. android:layout_width="match_parent"
  244. android:layout_height="wrap_content"
  245. android:hint="Password"
  246. android:id="@+id/pass"/>
  247.  
  248. <Button
  249. android:layout_width="match_parent"
  250. android:layout_height="wrap_content"
  251. android:text="Login"
  252. android:id="@+id/btnLogin"
  253. android:onClick="onClick"/>
  254.  
  255. <Button
  256. android:layout_width="match_parent"
  257. android:layout_height="wrap_content"
  258. android:text="Register"
  259. android:id="@+id/btnRegister"
  260. android:onClick="btnReg"
  261. />
  262.  
  263.  
  264. </LinearLayout>
  265.  
  266.  
  267.  
  268. =======================hello xml===================
  269.  
  270. <?xml version="1.0" encoding="utf-8"?>
  271. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  272. android:orientation="vertical" android:layout_width="match_parent"
  273. android:layout_height="match_parent">
  274.  
  275. <TextView
  276. android:layout_width="match_parent"
  277. android:layout_height="wrap_content"
  278. android:text="Hello"
  279. android:textSize="54sp"
  280. android:id="@+id/hellotxt"/>
  281. <Button
  282. android:layout_width="match_parent"
  283. android:layout_height="wrap_content"
  284. android:text="Logout"
  285. android:id="@+id/btnlogout"
  286. android:onClick="btnExit"/>
  287. </LinearLayout>
  288.  
  289. =================================reg xml======================
  290. <?xml version="1.0" encoding="utf-8"?>
  291. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  292. android:orientation="vertical" android:layout_width="match_parent"
  293. android:layout_height="match_parent">
  294.  
  295. <EditText
  296. android:layout_width="match_parent"
  297. android:layout_height="wrap_content"
  298. android:hint="Enter Username"
  299. android:id="@+id/crUser"
  300. />
  301. <EditText
  302. android:layout_width="match_parent"
  303. android:layout_height="wrap_content"
  304. android:hint="Password"
  305. android:id="@+id/crPass"/>
  306. <EditText
  307. android:layout_width="match_parent"
  308. android:layout_height="wrap_content"
  309. android:hint="Password2"
  310. android:id="@+id/confPass"/>
  311. <Button
  312. android:layout_width="match_parent"
  313. android:layout_height="wrap_content"
  314. android:text="Register"
  315. android:onClick="creatUser"
  316. />
  317. <Button
  318. android:layout_width="match_parent"
  319. android:layout_height="wrap_content"
  320. android:text="Cancel"
  321. android:onClick="btncancel"/>
  322.  
  323. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement