Guest User

My shared / oorya

a guest
Mar 8th, 2016
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.96 KB | None | 0 0
  1. package com.example.com.myshared;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.content.SharedPreferences;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.EditText;
  9. import android.widget.Toast;
  10.  
  11. public class MainActivity extends Activity
  12. {
  13.     EditText txtUser;
  14.     EditText txtPass;
  15.  
  16.     @Override
  17.     protected void onCreate(Bundle savedInstanceState) {
  18.         super.onCreate(savedInstanceState);
  19.         setContentView(R.layout.activity_main);
  20.         txtUser=(EditText)findViewById(R.id.user);
  21.         txtPass=(EditText)findViewById(R.id.pass);
  22.         if (checkUser())
  23.         {
  24.             //if key found , start the second activity.
  25.             Intent iEntry = new Intent(this,Entry.class);
  26.             this.startActivity(iEntry);
  27.         }
  28.     }
  29.  
  30.  
  31.     private boolean checkUser()
  32.     {
  33.         //declaration of shared preferences
  34.         SharedPreferences pref=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
  35.         //getting value of key "user"
  36.         String userName = pref.getString("userLogged","error");
  37.         //return false if key "user" not exists
  38.         return (!userName.equals("error"));
  39.     }
  40.  
  41.     public void btnLogin(View v)
  42.     {
  43.         SharedPreferences pref=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
  44.  
  45.         String userName=txtUser.getText().toString();
  46.         String password=txtPass.getText().toString();
  47.         String userDB=pref.getString("userReg", "ErrorU");
  48.         String passDB=pref.getString("passReg","ErrorP");
  49.  
  50.  
  51.         if(userName.equals("") && password.equals("")) {
  52.             Toast.makeText(this, "Please type username and password", Toast.LENGTH_LONG).show();
  53.             return;
  54.         }
  55.             else if(userDB.equals("ErrorU") && passDB.equals("ErrorP")) {
  56.                 Toast.makeText(this, "Please register", Toast.LENGTH_LONG).show();
  57.                 return;
  58.             }
  59.                 else if(userName.equals(userDB) && password.equals(passDB)) {
  60.                     Toast.makeText(this, "Good to see you", Toast.LENGTH_SHORT).show();
  61.                 }
  62.                     else
  63.                     {
  64.                         Toast.makeText(this, "Wrong user name or password", Toast.LENGTH_LONG).show();
  65.                         return;
  66.                     }
  67.  
  68.         //declartion of editor to edit shared preferances
  69.         SharedPreferences.Editor editor=pref.edit();
  70.         //insert into string value from EditText
  71.         //create key "user" with value of userName, if key exists, it will be overwritten
  72.         editor.putString("userLogged",userName);
  73.         editor.putString("passLogged",password);
  74.         //commit the changes
  75.         editor.commit();
  76.  
  77.         Intent iEntry = new Intent(this,Entry.class);
  78.         this.startActivity(iEntry);
  79.     }
  80.  
  81.     public void btnReg(View v)
  82.     {
  83.         Intent regIntent = new Intent(this, Register.class);
  84.         this.startActivity(regIntent);
  85.     }
  86. }
  87.  
  88. //====================================REGISTER CLASS
  89. package com.example.com.myshared;
  90.  
  91. import android.app.Activity;
  92. import android.content.Intent;
  93. import android.content.SharedPreferences;
  94. import android.os.Bundle;
  95. import android.view.View;
  96. import android.widget.EditText;
  97. import android.widget.Toast;
  98.  
  99.  
  100. public class Register extends Activity
  101. {
  102.     EditText txtUser, txtPass1, txtpass2;
  103.  
  104.     @Override
  105.     protected void onCreate(Bundle savedInstanceState) {
  106.         super.onCreate(savedInstanceState);
  107.         setContentView(R.layout.register);
  108.         setPointer();
  109.     }
  110.  
  111.     public void setPointer()
  112.     {
  113.         txtUser=(EditText)findViewById(R.id.user);
  114.         txtPass1=(EditText)findViewById(R.id.pass1);
  115.         txtpass2=(EditText)findViewById(R.id.pass2);
  116.     }
  117.  
  118.  
  119.     public void btnRegister(View v)
  120.     {
  121.         SharedPreferences pref=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
  122.         //declartion of editor to edit shared preferances
  123.         SharedPreferences.Editor editor=pref.edit();
  124.         //insert into string value from EditText
  125.         String userName=txtUser.getText().toString();
  126.         String password1=txtPass1.getText().toString();
  127.         String password2=txtpass2.getText().toString();
  128.         String userDB=pref.getString("userReg", "ErrorU");
  129.  
  130.         if(userName.equals("")||password1.equals("")){
  131.             Toast.makeText(this, "Please type username and password", Toast.LENGTH_LONG).show();
  132.             return;
  133.         }
  134.             else if(userName.equals(userDB)) {
  135.                 Toast.makeText(this, "The username exist, please type another username", Toast.LENGTH_LONG).show();
  136.             return;
  137.             }
  138.                 else if(!password1.equals(password2)){
  139.                     Toast.makeText(this, "The password must be equls", Toast.LENGTH_LONG).show();
  140.                     return;
  141.                 }
  142.         //create key "user" with value of userName, if key exists, it will be overwritten
  143.         editor.putString("userReg", userName);
  144.         editor.putString("passReg", password1);
  145.  
  146.         //commit the changes
  147.         editor.commit();
  148.  
  149.         Intent iLogin = new Intent(this, MainActivity.class);
  150.         this.startActivity(iLogin);
  151.     }
  152.  
  153.     public void btnCancel(View v)
  154.     {
  155.         Intent iLogin = new Intent(this, MainActivity.class);
  156.         this.startActivity(iLogin);
  157.     }
  158. }
  159.  
  160. //===============================ENTRY CLASS
  161. package com.example.com.myshared;
  162.  
  163. import android.app.Activity;
  164. import android.content.Intent;
  165. import android.content.SharedPreferences;
  166. import android.os.Bundle;
  167. import android.view.View;
  168. import android.widget.TextView;
  169.  
  170. public class Entry extends Activity {
  171.  
  172.     TextView txt;
  173.  
  174.     @Override
  175.     protected void onCreate(Bundle savedInstanceState) {
  176.         super.onCreate(savedInstanceState);
  177.         setContentView(R.layout.entry);
  178.         txt=(TextView)findViewById(R.id.text);
  179.         txt.setText("Hello " + getUserName());
  180.     }
  181.  
  182.  
  183.     private String getUserName()
  184.     {
  185.         //declaration of shared preferences
  186.         SharedPreferences pref=getApplicationContext().getSharedPreferences("myPref", MODE_PRIVATE);
  187.         //get string from key user, insert Error, if key not found
  188.         String userName = pref.getString("userLogged","error");
  189.         return userName;
  190.     }
  191.  
  192.     public void btnLogout(View v)
  193.     {
  194.         //declaration of shared preferences
  195.         SharedPreferences pref=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
  196.         //declaration of editor
  197.         SharedPreferences.Editor editor=pref.edit();
  198.         //remove key user and pass
  199.         editor.remove("userLogged");
  200.         editor.remove("passLogged");
  201.         editor.remove("userReg");
  202.         editor.remove("passReg");
  203.         //commit changes
  204.         editor.commit();
  205.         //start another window - main window
  206.         Intent iMain = new Intent(this,MainActivity.class);
  207.         this.startActivity(iMain);
  208.     }
  209. }
Add Comment
Please, Sign In to add comment