Advertisement
OoryaK

My task - Version 1.1 / Oorya

Mar 16th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.71 KB | None | 0 0
  1. //==========================================MainActivity
  2. package com.example.com.myshared;
  3.  
  4. import android.app.Activity;
  5. import android.content.Intent;
  6. import android.content.SharedPreferences;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.widget.EditText;
  10. import android.widget.Toast;
  11.  
  12. public class MainActivity extends Activity
  13. {
  14.     EditText txtUser;
  15.     EditText txtPass;
  16.  
  17.     @Override
  18.     protected void onCreate(Bundle savedInstanceState) {
  19.         super.onCreate(savedInstanceState);
  20.         setContentView(R.layout.activity_main);
  21.         txtUser=(EditText)findViewById(R.id.user);
  22.         txtPass=(EditText)findViewById(R.id.pass);
  23.         if (checkUser())
  24.         {
  25.             //if key found , start the second activity.
  26.             SharedPreferences pref=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
  27.             Intent iEntry = new Intent(this,Entry.class);
  28.             iEntry.putExtra("putUser", pref.getString("userLogged", "error"));
  29.             this.startActivity(iEntry);
  30.             finish();
  31.         }
  32.     }
  33.  
  34.  
  35.     private boolean checkUser()
  36.     {
  37.         //declaration of shared preferences
  38.         SharedPreferences pref=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
  39.         //getting value of key "user"
  40.         String userName = pref.getString("userLogged","error");
  41.         //return false if key "user" not exists
  42.         return (!userName.equals("error"));
  43.     }
  44.  
  45.     public void btnLogin(View v)
  46.     {
  47.         SharedPreferences pref = getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
  48.  
  49.         String userName=txtUser.getText().toString().toLowerCase();
  50.         String password= txtPass.getText().toString();
  51.         String passDB=pref.getString(userName,"ErrorP");
  52.  
  53.         if(userName.equals("") || password.equals("")) {
  54.             Toast.makeText(this, "Please type username and password", Toast.LENGTH_LONG).show();
  55.             return;
  56.         }
  57.             else if(!pref.contains(userName)) {
  58.                 Toast.makeText(this, "Please register", Toast.LENGTH_LONG).show();
  59.                 return;
  60.             }
  61.                 else if(password.equals(passDB)) {
  62.                     Toast.makeText(this, "Good to see you", Toast.LENGTH_SHORT).show();
  63.                 }
  64.                     else
  65.                     {
  66.                         Toast.makeText(this, "Wrong user name or password", Toast.LENGTH_LONG).show();
  67.                         return;
  68.                     }
  69.  
  70.         //declaration of editor to edit shared preferences
  71.         SharedPreferences.Editor editor=pref.edit();
  72.         //create key "user" with value of userName, if key exists, it will be overwritten
  73.         editor.putString("userLogged",userName);
  74.         //commit the changes
  75.         editor.commit();
  76.  
  77.         Intent iEntry = new Intent(this,Entry.class);
  78.         iEntry.putExtra("putUser", pref.getString("userLogged","error"));
  79.         this.startActivity(iEntry);
  80.         finish();
  81.     }
  82.  
  83.     public void btnReg(View v)
  84.     {
  85.         Intent regIntent = new Intent(this, Register.class);
  86.         this.startActivity(regIntent);
  87.     }
  88. }
  89.  
  90. //======================================REGISTER CLASS
  91. package com.example.com.myshared;
  92.  
  93. import android.app.Activity;
  94. import android.content.Intent;
  95. import android.content.SharedPreferences;
  96. import android.os.Bundle;
  97. import android.view.View;
  98. import android.widget.EditText;
  99. import android.widget.Toast;
  100.  
  101.  
  102. public class Register extends Activity
  103. {
  104.     EditText txtUser, txtPass1, txtPass2;
  105.  
  106.     @Override
  107.     protected void onCreate(Bundle savedInstanceState) {
  108.         super.onCreate(savedInstanceState);
  109.         setContentView(R.layout.register);
  110.         setPointer();
  111.     }
  112.  
  113.     public void setPointer()
  114.     {
  115.         txtUser=(EditText)findViewById(R.id.user);
  116.         txtPass1=(EditText)findViewById(R.id.pass1);
  117.         txtPass2=(EditText)findViewById(R.id.pass2);
  118.     }
  119.  
  120.  
  121.     public void btnRegister(View v)
  122.     {
  123.         SharedPreferences pref=getApplicationContext().getSharedPreferences("myPref", MODE_PRIVATE);
  124.         //declaration of editor to edit shared preferences
  125.         SharedPreferences.Editor editor=pref.edit();
  126.         //insert into string value from EditText
  127.         String userName=txtUser.getText().toString().toLowerCase();
  128.         String password1=txtPass1.getText().toString();
  129.         String password2=txtPass2.getText().toString();
  130.  
  131.         if(userName.equals("")||password1.equals("")){
  132.             Toast.makeText(this, "Please type username and password", Toast.LENGTH_LONG).show();
  133.             return;
  134.         }
  135.             else if(pref.contains(userName)) {
  136.                 Toast.makeText(this, "The username exist, please type another username", Toast.LENGTH_LONG).show();
  137.             return;
  138.             }
  139.                 else if(!password1.equals(password2)){
  140.                     Toast.makeText(this, "The password must be equals", Toast.LENGTH_LONG).show();
  141.                     return;
  142.                 }
  143.         //create key "user name" with value of your password
  144.         editor.putString(userName, password1);
  145.  
  146.         //commit the changes
  147.         editor.commit();
  148.  
  149.         Intent iLogin = new Intent(this, MainActivity.class);
  150.         this.startActivity(iLogin);
  151.         finish();
  152.     }
  153.  
  154.     public void btnCancel(View v)
  155.     {
  156.         finish();
  157.     }
  158. }
  159.  
  160. //===========================================ENTRY CLASS
  161. package com.example.com.myshared;
  162.  
  163. import android.app.Activity;
  164. import android.content.Context;
  165. import android.content.Intent;
  166. import android.content.SharedPreferences;
  167. import android.os.Bundle;
  168. import android.view.View;
  169. import android.widget.ListView;
  170. import android.widget.TextView;
  171. import java.util.Map;
  172.  
  173.  
  174. public class Entry extends Activity {
  175.  
  176.     TextView txt;
  177.     ListView myList;
  178.     Context context = this;
  179.  
  180.     @Override
  181.     protected void onCreate(Bundle savedInstanceState) {
  182.         super.onCreate(savedInstanceState);
  183.         setContentView(R.layout.entry);
  184.         txt=(TextView)findViewById(R.id.text);
  185.         myList=(ListView)findViewById(R.id.myListV);
  186.         String myUser = getIntent().getStringExtra("putUser");
  187.         txt.setText("Hello " + myUser);
  188.         SharedPreferences pref=getApplicationContext().getSharedPreferences(myUser, MODE_PRIVATE);
  189.  
  190.         String[] taskArr = new String[pref.getAll().size()];
  191.         int cnt=0;
  192.         Map<String, ?> allTask = pref.getAll();
  193.  
  194.         for (Map.Entry<String, ?> singleTask : allTask.entrySet())
  195.         {
  196.             taskArr[cnt] = singleTask.getKey();
  197.             cnt++;
  198.         }
  199.  
  200.         TaskAdapter myTask = new TaskAdapter(context,taskArr,myUser);
  201.         myList.setAdapter(myTask);
  202.     }
  203.  
  204.     public void btnLogout(View v)
  205.     {
  206.         //declaration of shared preferences
  207.         SharedPreferences pref=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
  208.         //declaration of editor
  209.         SharedPreferences.Editor editor=pref.edit();
  210.         //remove key user and pass
  211.         editor.remove("userLogged");
  212.         //commit changes
  213.         editor.commit();
  214.         //start another window - main window
  215.         Intent iMain = new Intent(this,MainActivity.class);
  216.         this.startActivity(iMain);
  217.         finish();
  218.     }
  219.  
  220.     public void btnAddTask(View view)
  221.     {
  222.         Intent iAddTask = new Intent(this, AddTask.class);
  223.         iAddTask.putExtra("putUserTask", getIntent().getStringExtra("putUser"));
  224.         this.startActivity(iAddTask);
  225.         finish();
  226.     }
  227. }
  228.  
  229. //=========================================ADD TASK CLASS
  230. package com.example.com.myshared;
  231.  
  232. import android.app.Activity;
  233. import android.content.Intent;
  234. import android.content.SharedPreferences;
  235. import android.os.Bundle;
  236. import android.view.View;
  237. import android.widget.EditText;
  238.  
  239. public class AddTask extends Activity {
  240.  
  241.     EditText txtTask;
  242.  
  243.  
  244.     @Override
  245.     protected void onCreate(Bundle savedInstanceState) {
  246.         super.onCreate(savedInstanceState);
  247.         setContentView(R.layout.add_task);
  248.         txtTask = (EditText)findViewById(R.id.task_type);
  249.     }
  250.  
  251.     public void btnConfirm(View view)
  252.     {
  253.         String myUser = getIntent().getStringExtra("putUserTask");
  254.         SharedPreferences pref=getApplicationContext().getSharedPreferences(myUser, MODE_PRIVATE);
  255.         SharedPreferences.Editor editor = pref.edit();
  256.  
  257.         String task = txtTask.getText().toString();
  258.         editor.putString(task, "0");
  259.         editor.commit();
  260.  
  261.         Intent iEntry = new Intent(this, Entry.class);
  262.         iEntry.putExtra("putUser", myUser);
  263.         this.startActivity(iEntry);
  264.         finish();
  265.     }
  266.  
  267.  
  268.     public void btnCancelTask(View view)
  269.     {
  270.         SharedPreferences pref=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
  271.         Intent iEntry = new Intent(this, Entry.class);
  272.         iEntry.putExtra("putUser", pref.getString("userLogged", "error"));
  273.         this.startActivity(iEntry);
  274.         finish();
  275.     }
  276. }
  277.  
  278. //============================================TASK ADAPTER CLASS
  279. package com.example.com.myshared;
  280.  
  281. import android.content.Context;
  282. import android.content.SharedPreferences;
  283. import android.graphics.Color;
  284. import android.view.Gravity;
  285. import android.view.View;
  286. import android.view.ViewGroup;
  287. import android.widget.BaseAdapter;
  288. import android.widget.LinearLayout;
  289. import android.widget.TextView;
  290. import android.widget.Toast;
  291.  
  292.  
  293. public class TaskAdapter extends BaseAdapter
  294. {
  295.     Context context;
  296.     String myTask[];
  297.     String myUser;
  298.  
  299.     public TaskAdapter(Context context,String myTask[], String myUser)
  300.     {
  301.         this.context=context;
  302.         this.myTask=myTask;
  303.         this.myUser=myUser;
  304.     }
  305.     @Override
  306.     public int getCount() {
  307.         return myTask.length;
  308.     }
  309.  
  310.     @Override
  311.     public Object getItem(int position) {
  312.         return myTask[position];
  313.     }
  314.  
  315.     @Override
  316.     public long getItemId(int position) {
  317.         return position;
  318.     }
  319.  
  320.     @Override
  321.     public View getView(final int position, View convertView, ViewGroup parent)
  322.     {
  323.         final SharedPreferences pref=context.getApplicationContext().getSharedPreferences(myUser, context.MODE_PRIVATE);
  324.         final SharedPreferences.Editor editor = pref.edit();
  325.  
  326.         LinearLayout myLayout = new LinearLayout(context);
  327.         myLayout.setOrientation(LinearLayout.VERTICAL);
  328.  
  329.         final TextView task = new TextView(context);
  330.         task.setText(myTask[position]);
  331.         task.setTextSize(32);
  332.         task.setGravity(Gravity.CENTER_HORIZONTAL);
  333.         task.setWidth(myLayout.getWidth() / 2);
  334.  
  335.         if(pref.getString(myTask[position], "error").equals("1"))
  336.             task.setBackgroundColor(Color.GREEN);
  337.         else
  338.             task.setBackgroundColor(Color.RED);
  339.  
  340.         task.setOnClickListener(new View.OnClickListener() {
  341.             @Override
  342.             public void onClick(View v) {
  343.  
  344.                 if(pref.getString(myTask[position], "error").equals("0")) {
  345.                     editor.putString(myTask[position], "1");
  346.                     editor.commit();
  347.                     task.setBackgroundColor(Color.GREEN);
  348.                     Toast.makeText(context, "The mission was successful ", Toast.LENGTH_SHORT).show();
  349.                 }
  350.                 else
  351.                 {
  352.                     editor.putString(myTask[position], "0");
  353.                     editor.commit();
  354.                     task.setBackgroundColor(Color.RED);
  355.                     Toast.makeText(context, "The mission not successful ", Toast.LENGTH_SHORT).show();
  356.                 }
  357.             }
  358.         });
  359.         myLayout.addView(task);
  360.         return myLayout;
  361.     }
  362. }
  363. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement