Advertisement
OoryaK

My shared / Version 1.1

Mar 10th, 2016
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.55 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.         }
  31.     }
  32.  
  33.  
  34.     private boolean checkUser()
  35.     {
  36.         //declaration of shared preferences
  37.         SharedPreferences pref=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
  38.         //getting value of key "user"
  39.         String userName = pref.getString("userLogged","error");
  40.         //return false if key "user" not exists
  41.         return (!userName.equals("error"));
  42.     }
  43.  
  44.     public void btnLogin(View v)
  45.     {
  46.         SharedPreferences pref = getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
  47.  
  48.         String userName=txtUser.getText().toString().toLowerCase();
  49.         String password= txtPass.getText().toString();
  50.         String passDB=pref.getString(userName,"ErrorP");
  51.  
  52.         if(userName.equals("") || password.equals("")) {
  53.             Toast.makeText(this, "Please type username and password", Toast.LENGTH_LONG).show();
  54.             return;
  55.         }
  56.             else if(!pref.contains(userName)) {
  57.                 Toast.makeText(this, "Please register", Toast.LENGTH_LONG).show();
  58.                 return;
  59.             }
  60.                 else if(password.equals(passDB)) {
  61.                     Toast.makeText(this, "Good to see you", Toast.LENGTH_SHORT).show();
  62.                 }
  63.                     else
  64.                     {
  65.                         Toast.makeText(this, "Wrong user name or password", Toast.LENGTH_LONG).show();
  66.                         return;
  67.                     }
  68.  
  69.         //declaration of editor to edit shared preferences
  70.         SharedPreferences.Editor editor=pref.edit();
  71.         //create key "user" with value of userName, if key exists, it will be overwritten
  72.         editor.putString("userLogged",userName);
  73.         //commit the changes
  74.         editor.commit();
  75.  
  76.         Intent iEntry = new Intent(this,Entry.class);
  77.         iEntry.putExtra("putUser", pref.getString("userLogged","error"));
  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.         //declaration of editor to edit shared preferences
  123.         SharedPreferences.Editor editor=pref.edit();
  124.         //insert into string value from EditText
  125.         String userName=txtUser.getText().toString().toLowerCase();
  126.         String password1=txtPass1.getText().toString();
  127.         String password2=txtPass2.getText().toString();
  128.  
  129.         if(userName.equals("")||password1.equals("")){
  130.             Toast.makeText(this, "Please type username and password", Toast.LENGTH_LONG).show();
  131.             return;
  132.         }
  133.             else if(pref.contains(userName)) {
  134.                 Toast.makeText(this, "The username exist, please type another username", Toast.LENGTH_LONG).show();
  135.             return;
  136.             }
  137.                 else if(!password1.equals(password2)){
  138.                     Toast.makeText(this, "The password must be equals", Toast.LENGTH_LONG).show();
  139.                     return;
  140.                 }
  141.         //create key "user name" with value of your password
  142.         editor.putString(userName, password1);
  143.  
  144.         //commit the changes
  145.         editor.commit();
  146.  
  147.         Intent iLogin = new Intent(this, MainActivity.class);
  148.         this.startActivity(iLogin);
  149.     }
  150.  
  151.     public void btnCancel(View v)
  152.     {
  153.         Intent iLogin = new Intent(this, MainActivity.class);
  154.         this.startActivity(iLogin);
  155.     }
  156. }
  157.  
  158. //===========================================ENTRY CLASS
  159. package com.example.com.myshared;
  160.  
  161. import android.app.Activity;
  162. import android.content.Intent;
  163. import android.content.SharedPreferences;
  164. import android.os.Bundle;
  165. import android.view.View;
  166. import android.widget.TextView;
  167.  
  168. public class Entry extends Activity {
  169.  
  170.     TextView txt;
  171.  
  172.     @Override
  173.     protected void onCreate(Bundle savedInstanceState) {
  174.         super.onCreate(savedInstanceState);
  175.         setContentView(R.layout.entry);
  176.         txt=(TextView)findViewById(R.id.text);
  177.         txt.setText("Hello " + getIntent().getStringExtra("putUser"));
  178.     }
  179.  
  180.  
  181.     public void btnLogout(View v)
  182.     {
  183.         //declaration of shared preferences
  184.         SharedPreferences pref=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
  185.         //declaration of editor
  186.         SharedPreferences.Editor editor=pref.edit();
  187.         //remove key user and pass
  188.         editor.remove("userLogged");
  189.         //commit changes
  190.         editor.commit();
  191.         //start another window - main window
  192.         Intent iMain = new Intent(this,MainActivity.class);
  193.         this.startActivity(iMain);
  194.     }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement