Advertisement
Guest User

shalty login

a guest
Mar 10th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.48 KB | None | 0 0
  1. package com.example.shalty.login;
  2.  
  3. import android.content.Intent;
  4. import android.content.SharedPreferences;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.widget.EditText;
  10. import android.widget.TextView;
  11. import android.widget.Toast;
  12.  
  13. public class Reg extends AppCompatActivity {
  14.  
  15.     EditText userName;
  16.     EditText password;
  17.     EditText verPassword;
  18.     SharedPreferences prefs;
  19.  
  20.     @Override
  21.     protected void onCreate(Bundle savedInstanceState) {
  22.         super.onCreate(savedInstanceState);
  23.         setContentView(R.layout.activity_reg);
  24.  
  25.  
  26.         userName = (EditText)findViewById(R.id.userNameReg);
  27.         password = (EditText)findViewById(R.id.passReg);
  28.         verPassword = (EditText)findViewById(R.id.passVal);
  29.         prefs=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
  30.  
  31.     }
  32.     public void SignUp(View v)
  33.     {
  34.         if(password.getText().toString().equals(verPassword.getText().toString()))
  35.         {
  36.  
  37.                 if(prefs.getString(userName.getText().toString(), "notExist").equals("notExist")) {
  38.                     saveData();
  39.                     Intent logIn = new Intent(this, Login.class);
  40.                     this.startActivity(logIn);
  41.                 }
  42.                 else
  43.                 {
  44.                     Toast.makeText(this, "Username already exists Please choose another ", Toast.LENGTH_LONG).show();
  45.                     this.startActivity(new Intent(this,Reg.class));
  46.                 }
  47.  
  48.         }else {
  49.             Toast.makeText(this, "Passwords do not match, please try again", Toast.LENGTH_LONG).show();
  50.             this.startActivity(new Intent(this,Reg.class));
  51.         }
  52.     }
  53.     private void saveData()
  54.     {
  55.         SharedPreferences.Editor editor=prefs.edit();
  56.         editor.putString(userName.getText().toString(), password.getText().toString());
  57.         editor.commit();
  58.        // Toast.makeText(this, "============saveData==============", Toast.LENGTH_LONG).show();
  59.  
  60.     }
  61.     public void Cancel(View v)
  62.     {
  63.         Intent login = new Intent(this, Login.class);
  64.         this.startActivity(login);
  65.  
  66.     }
  67.  
  68.  
  69.  
  70. }
  71. =====================================================================================
  72.  
  73. package com.example.shalty.login;
  74.  
  75. import android.content.Intent;
  76. import android.content.SharedPreferences;
  77. import android.support.v7.app.AppCompatActivity;
  78. import android.os.Bundle;
  79. import android.view.View;
  80. import android.widget.Button;
  81. import android.widget.EditText;
  82. import android.widget.TextView;
  83. import android.widget.Toast;
  84.  
  85. public class Login extends AppCompatActivity {
  86.  
  87.     private EditText userName;
  88.     private EditText password;
  89.     SharedPreferences prefs;
  90.     @Override
  91.     protected void onCreate(Bundle savedInstanceState) {
  92.         super.onCreate(savedInstanceState);
  93.         setContentView(R.layout.activity_login);
  94.         prefs=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
  95.         userName =(EditText)findViewById(R.id.userName);
  96.         password =(EditText)findViewById(R.id.pass);
  97.         if(prefs.getString(userName.getText().toString(),"logdIn").toString().equals("logdIn"))
  98.         {
  99.             Intent HomePage = new Intent(this,HomePage.class);
  100.             HomePage.putExtra("Name",prefs.getString(userName.getText().toString(), "logdIn"));
  101.         }
  102.  
  103.     }
  104.  
  105.    public void signUp(View v)
  106.     {
  107.         Intent signUp = new Intent(this,Reg.class);
  108.         this.startActivity(signUp);
  109.     }
  110.     public void logIn(View v)
  111.     {
  112.         if (!prefs.getString(userName.getText().toString(), "notExist").toString().equals("notExist"))
  113.         {
  114.  
  115.             if (!prefs.getString(password.getText().toString(), "notExist").toString().equals("notExist"))
  116.             {
  117.                 Intent homPage = new Intent(this, HomePage.class);
  118.                 homPage.putExtra("name",userName.getText().toString());;
  119.                 SharedPreferences.Editor editor=prefs.edit();
  120.                 editor.putString(userName.getText().toString(), "logdIn");
  121.                 startActivity(homPage);
  122.             } else
  123.                 Toast.makeText(this, "The username or password are incorrect", Toast.LENGTH_LONG).show();
  124.         }
  125.         else
  126.             Toast.makeText(this, "Username does not exist please signup ", Toast.LENGTH_LONG).show();
  127.     }
  128. }
  129. ===================================================================================================
  130.  
  131. package com.example.shalty.login;
  132.  
  133. import android.content.Intent;
  134. import android.content.SharedPreferences;
  135. import android.support.v7.app.AppCompatActivity;
  136. import android.os.Bundle;
  137. import android.view.View;
  138. import android.widget.RelativeLayout;
  139. import android.widget.TextView;
  140.  
  141. public class HomePage extends AppCompatActivity {
  142.  
  143.     TextView hom ;
  144.     @Override
  145.     protected void onCreate(Bundle savedInstanceState) {
  146.         super.onCreate(savedInstanceState);
  147.         setContentView(R.layout.activity_home_page);
  148.         hom = (TextView)findViewById(R.id.helow);
  149.         hom.setText("Hello " + getIntent().getStringExtra("name"));
  150.  
  151.  
  152.     }
  153.     public void logUot(View v)
  154.     {
  155.         SharedPreferences prefs=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
  156.         SharedPreferences.Editor editor=prefs.edit();
  157.         editor.remove("logdIn");
  158.         editor.commit();
  159.         Intent login = new Intent(this,Login.class);
  160.         this.startActivity(login);
  161.  
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement