SHARE
TWEET

yanivJava4

a guest Mar 20th, 2016 24 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ====================================Main Activity
  2. package com.example.y.login;
  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.Menu;
  9. import android.view.MenuItem;
  10. import android.view.View;
  11. import android.widget.EditText;
  12. import android.widget.Toast;
  13.  
  14. public class MainActivity extends Activity
  15. {
  16.     EditText txtUser;
  17.     EditText txtPass;
  18.     String userNameLogged;
  19.  
  20.     @Override
  21.     protected void onCreate(Bundle savedInstanceState)
  22.     {
  23.         super.onCreate(savedInstanceState);
  24.         setContentView(R.layout.activity_main);
  25.         txtUser = (EditText) findViewById(R.id.user);
  26.         txtPass = (EditText) findViewById(R.id.pass);
  27.  
  28.     }
  29.  
  30.  
  31.     public void btnLogin(View v)
  32.     {
  33.         if (checkUser())
  34.         {
  35.             Intent helloIntent = new Intent(this, Hello.class);
  36.             helloIntent.putExtra("userNameLogged", txtUser.getText().toString());
  37.             this.startActivity(helloIntent);
  38.         } else
  39.             Toast.makeText(this, "Wrong user name or password!", Toast.LENGTH_LONG).show();
  40.     }
  41.     private boolean checkUser()
  42.     {
  43.  
  44.         SharedPreferences myPref=getApplication().getSharedPreferences("myPref",MODE_PRIVATE);
  45.         String myPass=myPref.getString(txtUser.getText().toString(), "error");
  46.  
  47.         if (myPass.equals("error"))
  48.         {
  49.             return false;
  50.         }
  51.  
  52.         return (txtPass.getText().toString().equals(myPass));
  53.  
  54.     }
  55.     public void btnReg(View v)
  56.     {
  57.         Intent registerIntent=new Intent(this, Register.class);
  58.         this.startActivity(registerIntent);
  59.     }
  60. }
  61. ============================Register
  62. package com.example.y.login;
  63.  
  64. import android.app.Activity;
  65. import android.content.Intent;
  66. import android.content.SharedPreferences;
  67. import android.os.Bundle;
  68. import android.view.View;
  69. import android.widget.EditText;
  70. import android.widget.Toast;
  71.  
  72. public class Register extends Activity
  73. {
  74.     EditText txtUser;
  75.     EditText txtPass1;
  76.     EditText txtPass2;
  77.  
  78.     @Override
  79.     protected void onCreate(Bundle savedInstanceState)
  80.     {
  81.         super.onCreate(savedInstanceState);
  82.         setContentView(R.layout.regis);
  83.         setPointer();
  84.     }
  85.     public void setPointer()
  86.     {
  87.         txtUser=(EditText)findViewById(R.id.user);
  88.         txtPass1=(EditText)findViewById(R.id.passReg1);
  89.         txtPass2=(EditText)findViewById(R.id.passReg2);
  90.     }
  91.     public void btnRegister (View v)
  92.     {
  93.         SharedPreferences myPref=getApplicationContext().getSharedPreferences("myPref", MODE_PRIVATE);
  94.         SharedPreferences.Editor editor=myPref.edit();
  95.         String userName=txtUser.getText().toString();
  96.         String password1=txtPass1.getText().toString();
  97.         String password2=txtPass2.getText().toString();
  98.  
  99.         if (!password1.equals(password2))
  100.         {
  101.             Toast.makeText(this,"The password must be equls",Toast.LENGTH_LONG).show();
  102.             return;
  103.         }
  104.  
  105.         String userDB=myPref.getString(userName, "error");
  106.         if (userName.equals(userDB))
  107.         {
  108.             Toast.makeText(this,"The user name exist, please type another user name", Toast.LENGTH_LONG).show();
  109.             return;
  110.         }
  111.  
  112.         editor.putString(userName,password1);
  113.  
  114.         editor.commit();
  115.  
  116.         finish();
  117.     }
  118.     public void btnCancle (View v)
  119.     {
  120.         Intent myLog=new Intent(this,MainActivity.class);
  121.         this.startActivity(myLog);
  122.     }
  123. }
  124. ===============================Hello
  125. package com.example.y.login;
  126.  
  127.  
  128. import android.app.Activity;
  129. import android.content.Context;
  130. import android.content.Intent;
  131. import android.content.SharedPreferences;
  132. import android.os.Bundle;
  133. import android.view.View;
  134. import android.widget.TextView;
  135. import android.widget.Toast;
  136.  
  137. public class Hello extends Activity
  138. {
  139.     TextView myTxt;
  140.     ListView myListView;
  141.     Context context=this;
  142.  
  143.     @Override
  144.     protected void onCreate(Bundle savedInstanceState)
  145.     {
  146.         super.onCreate(savedInstanceState);
  147.         setContentView(R.layout.hello);
  148.         myListView=(ListView)findViewById(R.id.myListView);
  149.         myTxt=(TextView)findViewById(R.id.helloUser);
  150.         myTxt.setText("Hello " + getIntent().getStringExtra("userNameLogged"));
  151.         Toast.makeText(this,getIntent().getStringExtra("userNameLogged"),Toast.LENGTH_LONG).show();
  152.  
  153.        // myTxt.setText("Hello "+getUserName());
  154.     }
  155.  
  156.     /*private String getUserName()
  157.     {
  158.         SharedPreferences myPref=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
  159.         userName=myPref.getString("logged","error");
  160.         return userName;
  161.     }*/
  162.     public void btnAddTask (View v)
  163.     {
  164.  
  165.     }
  166.     public void btnLogout (View v)
  167.     {
  168.         SharedPreferences myPref=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
  169.         SharedPreferences.Editor editor=myPref.edit();
  170.         editor.remove("logged");
  171.         editor.commit();
  172.         Intent mainIntent=new Intent(this,MainActivity.class);
  173.         this.startActivity(mainIntent);
  174.     }
  175. }
  176. =====================List View
  177. package com.example.y.login;
  178.  
  179. import android.content.Context;
  180. import android.content.SharedPreferences;
  181. import android.graphics.Color;
  182. import android.view.View;
  183. import android.view.ViewGroup;
  184. import android.widget.BaseAdapter;
  185. import android.widget.LinearLayout;
  186. import android.widget.TextView;
  187. import android.widget.Toast;
  188.  
  189. import java.util.List;
  190.  
  191. public class ListView extends BaseAdapter
  192. {
  193.     Context context;
  194.     String taskArray[];
  195.     String user;
  196.  
  197.     public ListView(Context context, String taskArray[],String user)
  198.     {
  199.         this.context=context;
  200.         this.taskArray=taskArray;
  201.         this.user=user;
  202.     }
  203.  
  204.     @Override
  205.     public int getCount()
  206.     {
  207.         return taskArray.length;
  208.     }
  209.  
  210.     @Override
  211.     public Object getItem(int position)
  212.     {
  213.         return position;
  214.     }
  215.  
  216.     @Override
  217.     public long getItemId(int position)
  218.     {
  219.         return position;
  220.     }
  221.  
  222.     @Override
  223.     public View getView(final int position, View convertView, ViewGroup parent)
  224.     {
  225.         final SharedPreferences myPref=context.getApplicationContext().getSharedPreferences(user, context.MODE_PRIVATE);
  226.         final SharedPreferences.Editor editor=myPref.edit();
  227.         LinearLayout mylay=new LinearLayout(context);
  228.         mylay.setOrientation(LinearLayout.VERTICAL);
  229.         final TextView myTask=new TextView(context);
  230.         myTask.setText(taskArray[position]);
  231.         myTask.setTextSize(24);
  232.         myTask.setGravity(mylay.getWidth()/2);
  233.         if (myPref.getString(taskArray[position],"error").equals("1"))
  234.             myTask.setBackgroundColor(Color.RED);
  235.         else
  236.             myTask.setBackgroundColor(Color.GREEN);
  237.         myTask.setOnClickListener(new View.OnClickListener() {
  238.             @Override
  239.             public void onClick(View v)
  240.             {
  241.                 if(myPref.getString(taskArray[position],"error").equals("0"))
  242.                 {
  243.                     editor.putString(taskArray[position],"1");
  244.                     editor.commit();
  245.                     myTask.setBackgroundColor(Color.GREEN);
  246.                     Toast.makeText(context,"Good job",Toast.LENGTH_LONG).show();
  247.                 }
  248.             }
  249.         });
  250.         mylay.addView(myTask);
  251.         return mylay;
  252.     }
  253. }
  254. ========================
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top