Advertisement
Guest User

HomeWork MyToDoList

a guest
Mar 19th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.47 KB | None | 0 0
  1. HomeWork MyToDoList by Igor
  2.  
  3. xml activity_main======================================================================================================================
  4. <RelativeLayout
  5. android:layout_height="match_parent"
  6. android:layout_width="match_parent"
  7. android:id="@+id/mainTag"
  8. xmlns:android="http://schemas.android.com/apk/res/android">
  9. <EditText
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:textSize="30sp"
  13. android:hint="Username"
  14. android:gravity="center"
  15. android:id="@+id/inUser"
  16. android:layout_marginTop="15sp"
  17. />
  18. <EditText
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content"
  21. android:textSize="30sp"
  22. android:inputType="textPassword"
  23. android:hint="Password"
  24. android:gravity="center"
  25. android:id="@+id/inPass"
  26. android:layout_below="@+id/inUser"
  27. android:layout_marginTop="5sp"
  28. />
  29. <Button
  30. android:layout_width="match_parent"
  31. android:layout_height="wrap_content"
  32. android:textSize="30sp"
  33. android:text="Login"
  34. android:id="@+id/btnLog"
  35. android:layout_below="@+id/inPass"
  36. android:layout_marginTop="40sp"
  37. android:padding="20sp"
  38. android:onClick="btnLog"
  39. />
  40. <Button
  41. android:layout_width="match_parent"
  42. android:layout_height="wrap_content"
  43. android:text="Register"
  44. android:textSize="25sp"
  45. android:id="@+id/btnReg"
  46. android:layout_below="@+id/btnLog"
  47. android:layout_marginTop="10sp"
  48. android:padding="10sp"
  49. android:onClick="btnReg"
  50. />
  51. </RelativeLayout>
  52. ====================================================================================================================================
  53. Java MainActivity===================================================================================================================
  54. package com.example.igor.mytodolist;
  55.  
  56. import android.app.Activity;
  57. import android.content.Intent;
  58. import android.content.SharedPreferences;
  59. import android.os.Bundle;
  60. import android.view.View;
  61. import android.widget.EditText;
  62. import android.widget.Toast;
  63.  
  64. public class MainActivity extends Activity
  65. {
  66. //creating local EditText variables to connect with XML tag ids
  67.  
  68. // username password
  69. private EditText userTxt, userPass;
  70.  
  71. //Creating a SharedPRefrences
  72. private SharedPreferences myPref;
  73. private SharedPreferences.Editor editor;
  74.  
  75. //will be used to check if the username is online
  76. private String userConnected;
  77.  
  78. @Override
  79. protected void onCreate(Bundle savedInstanceState)
  80. {
  81. super.onCreate(savedInstanceState);
  82. setContentView(R.layout.activity_main);
  83. setPoints();
  84. //checks if user has allready logged in the past
  85. if(chkOnline())
  86. {
  87. //if he did and the method is true go direclty to Logged page
  88. Intent logged = new Intent(this,Logged.class);
  89. logged.putExtra("userOn",userConnected); // if logged transfer this data onward
  90. this.startActivity(logged);
  91. }
  92. }
  93.  
  94. private void setPoints()
  95. {
  96. //connects the local vars with the id'd tags
  97. userTxt = (EditText)findViewById(R.id.inUser);
  98. userPass = (EditText)findViewById(R.id.inPass);
  99.  
  100. //Setting up the SharedPrefrences //working with the clients(users) file
  101. myPref = getApplicationContext().getSharedPreferences(Register.myClients,MODE_PRIVATE);
  102. editor = myPref.edit();
  103. }
  104.  
  105. //method that is caleld by the button to check if all info is valid
  106. private boolean chkUser()
  107. {
  108. String userInput = userTxt.getText().toString();
  109. String passInput = userPass.getText().toString();
  110. //checks if all fields are correct
  111. if(userInput.trim().equals("") || passInput.trim().equals(""))
  112. {
  113. Toast.makeText(this,"Error, Please fill all the fields", Toast.LENGTH_SHORT).show();
  114. return false;
  115. }
  116.  
  117. //checks if username and password are correct!
  118. if(!myPref.contains(userInput) || !passInput.equals(myPref.getString(userInput,passInput)))
  119. {
  120. Toast.makeText(this,"Error, Wrong username or password!", Toast.LENGTH_SHORT).show();
  121. return false;
  122. }
  123.  
  124. // if none of the if statements accure save the Data in userOn and return true
  125. editor.putString("userOn",userInput).commit();
  126. return true;
  127. }
  128.  
  129. //method that checks if the username has logged in the past
  130. private boolean chkOnline()
  131. {
  132. userConnected = myPref.getString("userOn","offline");
  133.  
  134. return !userConnected.equals("offline");
  135. }
  136.  
  137. //button that calls upon method that checks users name and password and decides if to continue
  138. public void btnLog(View v)
  139. {
  140. if(chkUser())
  141. {
  142. Toast.makeText(this,"Logged in Succesfully",Toast.LENGTH_SHORT).show();
  143. Intent logIn = new Intent(this,Logged.class);
  144. this.startActivity(logIn);
  145. }
  146. }
  147.  
  148. //button that sends user to the registration
  149. public void btnReg(View v)
  150. {
  151. Intent goReg = new Intent(this, Register.class);
  152. this.startActivity(goReg);
  153. }
  154. }
  155. ====================================================================================================================================
  156. XML activity_register===============================================================================================================
  157. <RelativeLayout
  158. android:layout_height="match_parent"
  159. android:layout_width="match_parent"
  160. xmlns:android="http://schemas.android.com/apk/res/android">
  161. <TextView
  162. android:layout_width="match_parent"
  163. android:layout_height="wrap_content"
  164. android:text="Registration"
  165. android:gravity="center"
  166. android:textSize="20sp"
  167. android:id="@+id/regLine"
  168. android:layout_marginTop="20sp"
  169. />
  170. <EditText
  171. android:layout_width="match_parent"
  172. android:layout_height="wrap_content"
  173. android:hint="UserName"
  174. android:gravity="center"
  175. android:textSize="30sp"
  176. android:id="@+id/setUser"
  177. android:layout_below="@+id/regLine"
  178. android:layout_marginTop="15sp"
  179. />
  180.  
  181. <EditText
  182. android:layout_width="match_parent"
  183. android:layout_height="wrap_content"
  184. android:hint="Password"
  185. android:inputType="textPassword"
  186. android:gravity="center"
  187. android:textSize="30sp"
  188. android:id="@+id/setPass"
  189. android:layout_below="@+id/setUser"
  190. android:layout_marginTop="30sp" />
  191.  
  192. <EditText
  193. android:layout_width="match_parent"
  194. android:layout_height="wrap_content"
  195. android:hint="Re-Password"
  196. android:inputType="textPassword"
  197. android:gravity="center"
  198. android:textSize="30sp"
  199. android:id="@+id/confPass"
  200. android:layout_below="@+id/setPass"
  201. android:layout_marginTop="30sp" />
  202.  
  203. <Button
  204. android:layout_width="match_parent"
  205. android:layout_height="wrap_content"
  206. android:text="Register"
  207. android:textSize="30sp"
  208. android:id="@+id/tryReg"
  209. android:layout_below="@+id/confPass"
  210. android:layout_marginTop="10sp"
  211. android:onClick="btnReg"
  212. />
  213. <Button
  214. android:layout_width="match_parent"
  215. android:layout_height="wrap_content"
  216. android:text="Cancle"
  217. android:textSize="20sp"
  218. android:id="@+id/regCanc"
  219. android:layout_below="@+id/tryReg"
  220. android:layout_marginTop="20sp"
  221. android:onClick="btnCncl"
  222. />
  223. </RelativeLayout>
  224. ====================================================================================================================================
  225. Java Register=======================================================================================================================
  226. package com.example.igor.mytodolist;
  227.  
  228. import android.app.Activity;
  229. import android.content.Intent;
  230. import android.content.SharedPreferences;
  231. import android.os.Bundle;
  232. import android.view.View;
  233. import android.widget.EditText;
  234. import android.widget.Toast;
  235.  
  236. /**
  237. * Created by Igor on 3/19/2016.
  238. */
  239. public class Register extends Activity
  240. {
  241. //Creating final variable to set SharedPRefrences file name (XML filename)
  242. public static final String myClients = "account";
  243.  
  244. private SharedPreferences myPref;
  245. private SharedPreferences.Editor editor;
  246.  
  247. //creating 3 EditText variables to connect them with the XML file
  248.  
  249. // /*username password confrim password*/
  250. private EditText inptUser, inpPass, confPass;
  251.  
  252. @Override
  253. protected void onCreate(Bundle savedInstanceState)
  254. {
  255. super.onCreate(savedInstanceState);
  256. setContentView(R.layout.activity_register);
  257. setPoints();
  258. }
  259.  
  260. //setting up all the variables in a method that will be excuted in /*onCreate*/ method
  261. private void setPoints()
  262. {
  263. //connecting local variables with XML id'd tags
  264. inptUser =(EditText)findViewById(R.id.setUser);
  265. inpPass =(EditText)findViewById(R.id.setPass);
  266. confPass =(EditText)findViewById(R.id.confPass);
  267.  
  268. //setting up our SharedPrefrences file
  269. myPref = getApplicationContext().getSharedPreferences(myClients,MODE_PRIVATE);
  270. editor = myPref.edit();
  271. }
  272.  
  273. private boolean chkReg()
  274. {
  275. String userName = inptUser.getText().toString();
  276. String passWord = inpPass.getText().toString();
  277. String confirm = confPass.getText().toString();
  278.  
  279. //checks if all fields are filled
  280. if( userName.trim().equals("") || passWord.trim().equals("")
  281. || confirm.toLowerCase().equals("") )
  282. {
  283. Toast.makeText(this,"Error, Please make sure all fields are full!",Toast.LENGTH_SHORT).show();
  284. return false;
  285. }
  286.  
  287. //checks if passwords match
  288. if(!confirm.equals(passWord))
  289. {
  290. Toast.makeText(this,"Error, Please make sure passwords match!",Toast.LENGTH_SHORT).show();
  291. return false;
  292. }
  293.  
  294. //checks if the username is availble
  295. if(myPref.contains(userName))
  296. {
  297. Toast.makeText(this,"Error, this username has allready been taken!",Toast.LENGTH_SHORT).show();
  298. return false;
  299. }
  300.  
  301. //if all these statements do not accure just input the data given and return true
  302.  
  303. editor.putString(userName, passWord).commit();
  304.  
  305. Toast.makeText(this,"Success, Account has been registered!",Toast.LENGTH_SHORT).show();
  306. return true;
  307. }
  308.  
  309. //Register button that chekcs if account is valid
  310. public void btnReg(View v)
  311. {
  312. if(chkReg())
  313. {
  314. Intent bckLog = new Intent(this,MainActivity.class);
  315. this.startActivity(bckLog);
  316. finish();
  317. }
  318. }
  319.  
  320. //Cancle Registration and return to login
  321. public void btnCncl(View v)
  322. {
  323. Intent rtnLog = new Intent(this,MainActivity.class);
  324. this.startActivity(rtnLog);
  325. finish();
  326. }
  327. }
  328. ====================================================================================================================================
  329. XML activity_logged=================================================================================================================
  330. <RelativeLayout
  331. android:layout_height="wrap_content"
  332. android:layout_width="match_parent"
  333. android:id="@+id/logPage"
  334. xmlns:android="http://schemas.android.com/apk/res/android">
  335. <TextView
  336. android:layout_width="match_parent"
  337. android:layout_height="wrap_content"
  338. android:id="@+id/logMsg"
  339. android:layout_marginTop="25sp"
  340. android:textSize="50sp"
  341. />
  342.  
  343. <ListView
  344. android:id="@+id/idList"
  345. android:layout_width="match_parent"
  346. android:layout_height="wrap_content"
  347. android:layout_below="@+id/logMsg"
  348. />
  349. <Button
  350. android:layout_width="match_parent"
  351. android:layout_height="wrap_content"
  352. android:id="@+id/btnTask"
  353. android:layout_below="@+id/idList"
  354. android:text="Add Task"
  355. android:textSize="30sp"
  356. android:gravity="center"
  357. android:layout_marginTop="20sp"
  358. android:onClick="btnTask"
  359. />
  360. <Button
  361. android:layout_width="match_parent"
  362. android:layout_height="wrap_content"
  363. android:id="@+id/btnOut"
  364. android:layout_below="@+id/btnTask"
  365. android:text="log out"
  366. android:textSize="30sp"
  367. android:gravity="center"
  368. android:layout_marginTop="20sp"
  369. android:onClick="btnLogOut"
  370. />
  371.  
  372. <TextView
  373. android:layout_width="wrap_content"
  374. android:layout_height="wrap_content"
  375. android:textAppearance="?android:attr/textAppearanceSmall"
  376. android:text="Created by Igor"
  377. android:id="@+id/whoLog"/>
  378. </RelativeLayout>
  379. ====================================================================================================================================
  380. JAVA Logged=========================================================================================================================
  381. package com.example.igor.mytodolist;
  382.  
  383. import android.app.Activity;
  384. import android.content.Intent;
  385. import android.content.SharedPreferences;
  386. import android.os.Bundle;
  387. import android.view.View;
  388. import android.widget.ListView;
  389. import android.widget.TextView;
  390.  
  391. import java.util.ArrayList;
  392. import java.util.Map;
  393.  
  394. /**
  395. * Created by Igor on 3/19/2016.
  396. */
  397. public class Logged extends Activity
  398. {
  399. TextView usrName;//will be used to display user that has logged
  400. ListView myList; //will be used to connect all the tasks on a listview
  401.  
  402. //SharedPrefrences
  403. SharedPreferences myPref;
  404. SharedPreferences myData; // will be used to select each users database of tasks!
  405. SharedPreferences.Editor editor;
  406.  
  407. //setting up an arraylist to hold all of the tasks
  408. ArrayList<String> myTasks = new ArrayList<String>();
  409.  
  410. @Override
  411. protected void onCreate(Bundle savedInstanceState)
  412. {
  413. super.onCreate(savedInstanceState);
  414. setContentView(R.layout.activity_logged);
  415. setPoints();
  416. //say hello user
  417. usrName.setText("Hello "+ getUsername());
  418. }
  419.  
  420. //returns the name of the user that is currently online
  421. private String getUsername()
  422. {
  423. String userName = myPref.getString("userOn","offline");
  424. return userName;//returns current username online
  425. }
  426.  
  427. //settingup basic points in the onCreate
  428. private void setPoints()
  429. {
  430. //connects the id with variable
  431. usrName = (TextView)findViewById(R.id.logMsg);
  432. myList = (ListView)findViewById(R.id.idList);
  433.  
  434. //setting up the prefrences
  435. myPref = getApplicationContext().getSharedPreferences(Register.myClients,MODE_PRIVATE);
  436. editor = myPref.edit();
  437.  
  438. //setting up specific user database of tasks
  439. myData = getApplicationContext().getSharedPreferences(getUsername(),MODE_PRIVATE);
  440. }
  441.  
  442. protected void onResume()
  443. {
  444. super.onResume();
  445. Map<String,?> usersTasks= myData.getAll();
  446. for(Map.Entry<String,?> tasks:usersTasks.entrySet())
  447. {
  448. myTasks.add(tasks.getKey().toString());
  449. }
  450.  
  451. MyAdapter adpt = new MyAdapter(this,myTasks);
  452. myList.setAdapter(adpt);
  453. }
  454.  
  455.  
  456. //add new task button
  457. public void btnTask(View v)
  458. {
  459. Intent newTask = new Intent(this,NewTask.class);
  460. this.startActivity(newTask);
  461. finish();
  462. }
  463.  
  464. //logout button
  465. public void btnLogOut(View v)
  466. {
  467. Intent logOut = new Intent(this,MainActivity.class);
  468. editor.remove("userOn").commit();
  469. this.startActivity(logOut);
  470. finish();
  471. }
  472. }
  473. ====================================================================================================================================
  474. XML activity_addtask================================================================================================================
  475. <RelativeLayout
  476. android:id="@+id/conter"
  477. android:layout_width="match_parent"
  478. android:layout_height="match_parent"
  479. xmlns:android="http://schemas.android.com/apk/res/android">
  480. <TextView
  481. android:id="@+id/wlcTask"
  482. android:layout_width="match_parent"
  483. android:layout_height="wrap_content"
  484. android:text="Add Your Task below"
  485. android:textSize="30sp"
  486. android:gravity="center"
  487. />
  488. <EditText
  489. android:id="@+id/txtTask"
  490. android:layout_width="match_parent"
  491. android:layout_height="wrap_content"
  492. android:hint="New task"
  493. android:gravity="center"
  494. android:textSize="25sp"
  495. android:layout_below="@+id/wlcTask"
  496. android:layout_marginTop="40sp"
  497. />
  498. <Button
  499. android:id="@+id/btnAdd"
  500. android:layout_width="match_parent"
  501. android:layout_height="wrap_content"
  502. android:layout_below="@+id/txtTask"
  503. android:layout_marginTop="20sp"
  504. android:text="Add Task"
  505. android:textSize="50sp"
  506. android:onClick="btnTask"
  507. />
  508. <Button
  509. android:id="@+id/btnRtn"
  510. android:layout_width="match_parent"
  511. android:layout_height="wrap_content"
  512. android:layout_below="@+id/btnAdd"
  513. android:layout_marginTop="20sp"
  514. android:text="Canlce"
  515. android:textSize="30sp"
  516. android:onClick="btnCancle"
  517. />
  518. </RelativeLayout>
  519. ====================================================================================================================================
  520. JAVA NewTask========================================================================================================================
  521. package com.example.igor.mytodolist;
  522.  
  523.  
  524. import android.app.Activity;
  525. import android.content.Intent;
  526. import android.content.SharedPreferences;
  527. import android.os.Bundle;
  528. import android.view.View;
  529. import android.widget.EditText;
  530.  
  531. /**
  532. * Created by Igor on 3/19/2016.
  533. */
  534. public class NewTask extends Activity
  535. {
  536. //user task input
  537. private EditText usrTask;
  538.  
  539. private SharedPreferences myPref;
  540.  
  541. @Override
  542. protected void onCreate(Bundle savedInstanceState)
  543. {
  544. super.onCreate(savedInstanceState);
  545. setContentView(R.layout.activity_addtask);
  546. usrTask = (EditText)findViewById(R.id.txtTask);
  547. myPref = getApplicationContext().getSharedPreferences(Register.myClients, MODE_PRIVATE);
  548. }
  549.  
  550. public void btnTask(View v)
  551. {
  552. //create a String variable to hold userOn info
  553. String dataBase = myPref.getString("userOn","offline");
  554. //create new XML file for for current user
  555. SharedPreferences userData = getApplicationContext().getSharedPreferences(dataBase, MODE_PRIVATE);
  556. SharedPreferences.Editor editor = userData.edit();
  557.  
  558. //add task as key with value of 0 /*by defualt not done!*/
  559. editor.putInt(usrTask.getText().toString(),0).commit();
  560.  
  561. //go back to the Logged page
  562. Intent added = new Intent(this,Logged.class);
  563. this.startActivity(added);//go to logged
  564. finish();
  565. }
  566.  
  567. public void btnCancle(View v)
  568. {
  569. Intent cncl = new Intent(this,Logged.class);
  570. this.startActivity(cncl);//cancle adding task
  571. finish();
  572. }
  573.  
  574.  
  575.  
  576. }
  577. ====================================================================================================================================
  578. JAVA MyAdapter======================================================================================================================
  579. package com.example.igor.mytodolist;
  580.  
  581. import android.content.Context;
  582. import android.content.SharedPreferences;
  583. import android.graphics.Color;
  584. import android.view.Gravity;
  585. import android.view.View;
  586. import android.view.ViewGroup;
  587. import android.widget.BaseAdapter;
  588. import android.widget.CheckBox;
  589. import android.widget.LinearLayout;
  590. import android.widget.TextView;
  591. import android.widget.Toast;
  592. import java.util.ArrayList;
  593.  
  594. /**
  595. * Created by Igor on 3/19/2016.
  596. */
  597. public class MyAdapter extends BaseAdapter
  598. {
  599. Context context;
  600.  
  601. SharedPreferences currUser;
  602. SharedPreferences.Editor editor;
  603.  
  604. //setting up arraylist to /*filter*/ the data threw adapter
  605. ArrayList<String> tasks = new ArrayList<String>();
  606.  
  607. public MyAdapter(Context context, ArrayList<String> tasks)
  608. {
  609. this.context=context;
  610. this.tasks = tasks;
  611. setPrefs();
  612. }
  613.  
  614. //Method that Returns the current user that is using
  615. private void setPrefs()
  616. {
  617. SharedPreferences myPref= context.getApplicationContext().getSharedPreferences(Register.myClients, Context.MODE_PRIVATE);
  618. String user = myPref.getString("userOn", "offline");
  619. currUser = context.getApplicationContext().getSharedPreferences(user,Context.MODE_PRIVATE);
  620. editor = currUser.edit();
  621. }
  622.  
  623. @Override
  624. public int getCount() {
  625. return tasks.size();
  626. }
  627.  
  628. @Override
  629. public Object getItem(int position) {
  630. return tasks.get(position);
  631. }
  632.  
  633. @Override
  634. public long getItemId(int position)
  635. {
  636. return position;
  637. }
  638.  
  639. @Override
  640. public View getView(final int position, View convertView, ViewGroup parent)
  641. {
  642. //my linearlayout that will hold all the list in!
  643. LinearLayout myLay = new LinearLayout(context);
  644. myLay.setOrientation(LinearLayout.VERTICAL);
  645.  
  646. //my text view that will appear
  647. final CheckBox checkBox=new CheckBox(context);
  648. final TextView textView = new TextView(context);
  649. textView.setText(tasks.get(position));//show the task
  650. textView.setTextSize(30);
  651. textView.setGravity(Gravity.CENTER_HORIZONTAL);
  652. textView.setWidth(myLay.getWidth() / 2);
  653.  
  654. //setting up colors of the lists
  655. if(currUser.getInt(tasks.get(position),0) == 0)
  656. {
  657. checkBox.setChecked(false);
  658. textView.setTextColor(Color.RED);
  659. }
  660. else
  661. {
  662. checkBox.setChecked(true);
  663. textView.setTextColor(Color.BLUE);
  664. }
  665.  
  666. //onclick on the task
  667. textView.setOnClickListener(new View.OnClickListener(){
  668.  
  669. @Override
  670. public void onClick(View v)
  671. {
  672. if(currUser.getInt(tasks.get(position),0)==0)
  673. {
  674. editor.putInt(tasks.get(position), 1);
  675. editor.commit();
  676. textView.setTextColor(Color.BLUE);
  677. Toast.makeText(context,"Done",Toast.LENGTH_SHORT).show();
  678. }
  679. else
  680. {
  681. editor.putInt(tasks.get(position), 0);
  682. editor.commit();
  683. textView.setTextColor(Color.RED);
  684. Toast.makeText(context,"Undone",Toast.LENGTH_SHORT).show();
  685. }
  686. }
  687. });
  688.  
  689. myLay.addView(textView);
  690. return myLay;
  691. }
  692. }
  693. =====================================================================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement