Advertisement
Guest User

LoginSystem

a guest
Mar 12th, 2016
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.38 KB | None | 0 0
  1. package com.example.ayob.loginsystem;
  2.  
  3. import android.content.Intent;
  4. import android.content.SharedPreferences;
  5. import android.os.Bundle;
  6. import android.support.design.widget.FloatingActionButton;
  7. import android.support.design.widget.Snackbar;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.support.v7.widget.Toolbar;
  10. import android.view.View;
  11. import android.view.Menu;
  12. import android.view.MenuItem;
  13. import android.widget.EditText;
  14. import android.widget.TextView;
  15. import android.widget.Toast;
  16.  
  17. public class MainActivity extends AppCompatActivity {
  18.  
  19.  
  20.     EditText userTxt;
  21.     EditText passTxt ;
  22.       SharedPreferences Prefs;
  23.       SharedPreferences.Editor editor;
  24.  
  25.     @Override
  26.     protected void onCreate(Bundle savedInstanceState) {
  27.         super.onCreate(savedInstanceState);
  28.         setContentView(R.layout.activity_main);
  29.         setPointers();
  30.         Toast.makeText(this,Prefs.getString("logged", "Error"),Toast.LENGTH_SHORT).show();
  31.  
  32.         if(CheckUser())
  33.         {
  34.             Intent intent=new Intent(this,Display_Activity.class);
  35.             // Send  user name to Logged screen
  36.             intent.putExtra("userinput", Prefs.getString("logged", "Error"));
  37.             // Go to Disply logged screen
  38.             this.startActivity(intent);
  39.         }
  40.  
  41.     }
  42.     // Checking if user exist in the SharedPreferences XML file
  43.  
  44.     private boolean CheckUser()
  45.     {
  46.  
  47.         String userName = Prefs.getString("logged","Error");
  48.         // If user not exist return false
  49.         return (!userName.equals("Error"));
  50.     }
  51.  
  52.      //pointer  initioialazation
  53.     private void setPointers()
  54.     {
  55.  
  56.          Prefs =    getApplicationContext().getSharedPreferences("users",MODE_PRIVATE);
  57.          editor =   Prefs.edit();
  58.          userTxt = (EditText)findViewById(R.id.txt_userName);
  59.          passTxt = (EditText)findViewById(R.id.txt_Password);
  60.     }
  61.  // show Registeration Activity
  62.     public void btn_show_register(View v)
  63.     {
  64.         Intent reg=new Intent(this,RegisterActivity.class);
  65.         startActivity(reg);
  66.     }
  67.    // Login To the System
  68.    public void btnLogin(View v)
  69.    {
  70.  
  71.        SharedPreferences pref = getApplicationContext().getSharedPreferences("users", MODE_PRIVATE);
  72.  
  73.        String logUserName=userTxt.getText().toString().toLowerCase();
  74.        String logPassword=passTxt.getText().toString();
  75.        String loggeedUser=pref.getString(logUserName, "Error");
  76.  
  77.        // validate user input not empty
  78.        if(logUserName.isEmpty() || logPassword.isEmpty())
  79.        {
  80.            Toast.makeText(this,"One of more fields is empty",Toast.LENGTH_SHORT).show();
  81.            return;
  82.        }
  83.        // if user name or password doesn't exist in "users" sharedpreferences xml file
  84.  
  85.        else if(!pref.contains(logUserName) || !logPassword.equals(loggeedUser))
  86.        {
  87.            Toast.makeText(this,"Invalid user name or password ",Toast.LENGTH_SHORT).show();
  88.            return;
  89.        }
  90.        // if proper data
  91.        else
  92.        {
  93.            SharedPreferences.Editor editor=pref.edit();
  94.            editor.putString("Logged",logUserName).commit();
  95.            Toast.makeText(this,"You have successfuly logged",Toast.LENGTH_SHORT).show();
  96.            Intent loggedIntent = new Intent(this, Display_Activity.class);
  97.            // Send  user name to Logged screen
  98.            loggedIntent.putExtra("userPut", pref.getString("Logged", "Error"));
  99.            // Go to logged screen
  100.            this.startActivity(loggedIntent);
  101.        }
  102.  
  103.    }
  104.  
  105. _________________________________________________________________________________--
  106. public class RegisterActivity  extends Activity
  107.  
  108.  
  109.   {
  110.       EditText UserName;//(EditText)findViewById(R.id.et_user);
  111.       EditText Password1;//=(EditText)findViewById(R.id.et_password);
  112.       EditText Password2;//=(EditText)findViewById(R.id.et_confirm_pass);
  113.  
  114.       @Override
  115.       protected void onCreate(Bundle savedInstanceState) {
  116.           super.onCreate(savedInstanceState);
  117.           setContentView(R.layout.register_activity);
  118.  
  119.          UserName=(EditText)findViewById(R.id.et_user);
  120.          Password1=(EditText)findViewById(R.id.et_password);
  121.          Password2=(EditText)findViewById(R.id.et_confirm_pass);
  122.       }
  123.       public void btnCancel(View v)
  124.       {
  125.           Intent MainIntent=new Intent(this,MainActivity.class);
  126.           // Go to login screen
  127.           this.startActivity(MainIntent);
  128.       }
  129.  
  130.       public void btnUserRegister(View v)
  131.       {
  132.  
  133.           SharedPreferences pref=getApplicationContext().getSharedPreferences("users", MODE_PRIVATE);
  134.           SharedPreferences.Editor editor=pref.edit();
  135.  
  136.           String regUserName=UserName.getText().toString().toLowerCase();
  137.           String regPassword1=Password1.getText().toString();
  138.          String regPassword2=Password2.getText().toString();
  139.  
  140.           // If any field is empty
  141.           if(regUserName.isEmpty() || regPassword1.isEmpty() || regPassword2.isEmpty())
  142.           {
  143.               Toast.makeText(this, "One of more fields is empty", Toast.LENGTH_SHORT).show();
  144.               return;
  145.           }
  146.           // if user name exist
  147.           else if(pref.contains(regUserName))
  148.           {
  149.               Toast.makeText(this,"User name is exists",Toast.LENGTH_SHORT).show();
  150.               return;
  151.           }
  152.           // if passwords match
  153.           else if(!regPassword1.equals(regPassword2))
  154.           {
  155.               Toast.makeText(this,"Passwords don't match",Toast.LENGTH_SHORT).show();
  156.               return;
  157.           }
  158.           // if proper data
  159.           Toast.makeText(this,"Registration was successful",Toast.LENGTH_SHORT).show();
  160.           // Adding a new user
  161.           editor.putString(regUserName, regPassword1).commit();
  162.           Intent registeredIntent = new Intent(this, MainActivity.class);
  163.           // Go to login screen
  164.           this.startActivity(registeredIntent);
  165.  
  166.       }
  167.  
  168.  
  169. ____________________________________________________________________________________
  170.  
  171. package com.example.ayob.loginsystem;
  172.  
  173. import android.app.Activity;
  174. import android.content.Intent;
  175. import android.content.SharedPreferences;
  176. import android.os.Bundle;
  177. import android.view.Menu;
  178. import android.view.View;
  179. import android.widget.TextView;
  180.  
  181. /**
  182.  * Created by Ayob on 11/03/2016.
  183.  */
  184. public class Display_Activity  extends Activity
  185.  
  186. {
  187.     TextView txtUserName;
  188.     @Override
  189.     protected void onCreate(Bundle savedInstanceState) {
  190.         super.onCreate(savedInstanceState);
  191.         setContentView(R.layout.user_display);
  192.  
  193.         txtUserName=(TextView)findViewById(R.id.txt_hello);
  194.         // Print "Hello "+ user name from main activity
  195.         txtUserName.setText("Hello " + getIntent().getStringExtra("userPut"));
  196.     }
  197.  
  198.     public void btnLogOut(View v)
  199.     {
  200.         SharedPreferences pref=getApplicationContext().getSharedPreferences("users",MODE_PRIVATE);
  201.         SharedPreferences.Editor editor=pref.edit();
  202.         // Remove last logged user
  203.         editor.remove("Logged").commit();
  204.         Intent MainIntent=new Intent(this,MainActivity.class);
  205.         // Go to login screen
  206.         this.startActivity(MainIntent);
  207.     }
  208. }
  209.  
  210.  
  211. _____________________________________________________________________________________________________________
  212.  
  213. <LinearLayout
  214.     android:layout_width="match_parent"
  215.     android:layout_height="match_parent"
  216.     android:orientation="vertical"
  217.     xmlns:android="http://schemas.android.com/apk/res/android" >
  218. <LinearLayout
  219.     android:layout_width="match_parent"
  220.     android:layout_height="match_parent"
  221.     android:orientation="horizontal"
  222.     android:layout_weight="1">
  223.     <EditText
  224.         android:layout_height="match_parent"
  225.         android:layout_width="match_parent"
  226.         android:hint="User"
  227.         android:textSize="30sp"
  228.         android:layout_weight="1"
  229.         android:gravity="center"
  230.         android:id="@+id/txt_userName"/>
  231.  
  232.  
  233. </LinearLayout>
  234.  
  235.     <LinearLayout
  236.         android:layout_width="match_parent"
  237.         android:layout_height="match_parent"
  238.         android:orientation="horizontal"
  239.         android:layout_weight="1"
  240.         android:layout_gravity="center">
  241.  
  242.         <EditText
  243.             android:layout_height="match_parent"
  244.             android:layout_width="match_parent"
  245.             android:hint="Password"
  246.             android:textSize="30sp"
  247.             android:layout_weight="1"
  248.             android:gravity="center"
  249.             android:id="@+id/txt_Password"/>
  250.  
  251.     </LinearLayout>
  252.     <LinearLayout
  253.         android:layout_width="match_parent"
  254.         android:layout_height="match_parent"
  255.         android:orientation="horizontal"
  256.         android:layout_weight="1"
  257.         android:layout_gravity="center">
  258.   <Button android:layout_height="match_parent"
  259.        android:layout_width="match_parent"
  260.       android:text="Login"
  261.       android:textSize="30dp"
  262.       android:layout_weight="1"
  263.       android:onClick="btnLogin"/>
  264.     </LinearLayout>
  265.  
  266.  
  267. <LinearLayout
  268. android:layout_width="match_parent"
  269. android:layout_height="match_parent"
  270. android:orientation="horizontal"
  271. android:layout_weight="1">
  272.     <Button android:layout_height="match_parent"
  273.         android:layout_width="match_parent"
  274.         android:text="Register"
  275.         android:textSize="30dp"
  276.         android:layout_weight="1"
  277.         android:onClick="btn_show_register"/>
  278. </LinearLayout>
  279. </LinearLayout>
  280.  
  281. _______________________________________________________________________________________________________________
  282.  
  283. <LinearLayout
  284.     android:layout_width="match_parent"
  285.     android:layout_height="match_parent"
  286.     android:orientation="vertical"
  287.     xmlns:android="http://schemas.android.com/apk/res/android" >
  288.     <LinearLayout
  289.         android:layout_width="match_parent"
  290.         android:layout_height="match_parent"
  291.         android:orientation="horizontal"
  292.         android:layout_weight="1">
  293.         <EditText
  294.             android:layout_height="match_parent"
  295.             android:layout_width="match_parent"
  296.             android:hint="User"
  297.             android:textSize="30sp"
  298.             android:layout_weight="1"
  299.             android:gravity="center"
  300.             android:id="@+id/et_user"/>
  301.  
  302.  
  303.     </LinearLayout>
  304.  
  305.     <LinearLayout
  306.         android:layout_width="match_parent"
  307.         android:layout_height="match_parent"
  308.         android:orientation="horizontal"
  309.         android:layout_weight="1"
  310.         android:layout_gravity="center">
  311.  
  312.         <EditText
  313.             android:layout_height="match_parent"
  314.             android:layout_width="match_parent"
  315.             android:hint="Password"
  316.             android:textSize="30sp"
  317.             android:layout_weight="1"
  318.             android:gravity="center"
  319.             android:id="@+id/et_password"/>
  320.  
  321.     </LinearLayout>
  322.     <LinearLayout
  323.         android:layout_width="match_parent"
  324.         android:layout_height="match_parent"
  325.         android:orientation="horizontal"
  326.         android:layout_weight="1"
  327.         android:layout_gravity="center">
  328.         <EditText
  329.             android:layout_height="match_parent"
  330.             android:layout_width="match_parent"
  331.             android:hint="confirm password"
  332.             android:textSize="30sp"
  333.             android:layout_weight="1"
  334.             android:gravity="center"
  335.               android:id="@+id/et_confirm_pass"/>
  336.     </LinearLayout>
  337.     <LinearLayout
  338.         android:layout_width="match_parent"
  339.         android:layout_height="match_parent"
  340.         android:orientation="horizontal"
  341.         android:layout_weight="1"
  342.         android:layout_gravity="center">
  343.         <Button android:layout_height="match_parent"
  344.             android:layout_width="match_parent"
  345.             android:text="Register"
  346.             android:textSize="30dp"
  347.             android:layout_weight="1"
  348.             android:id="@+id/btn_register"
  349.             android:onClick="btnUserRegister"/>
  350.     </LinearLayout>
  351.  
  352.  
  353.     <LinearLayout
  354.         android:layout_width="match_parent"
  355.         android:layout_height="match_parent"
  356.         android:orientation="horizontal"
  357.         android:layout_weight="1">
  358.         <Button android:layout_height="match_parent"
  359.             android:layout_width="match_parent"
  360.             android:text="Cancel"
  361.             android:textSize="30dp"
  362.             android:layout_weight="1"
  363.             android:id="@+id/btn_cancel"
  364.             android:onClick="btnCancel"/>
  365.     </LinearLayout>
  366. </LinearLayout>
  367.  
  368.  
  369. __________________________________________________________________________________________________________________
  370.  
  371.  
  372.  
  373. <LinearLayout
  374.     android:layout_width="match_parent"
  375.     android:layout_height="match_parent"
  376.     xmlns:android="http://schemas.android.com/apk/res/android"
  377.     android:orientation="vertical"
  378.     >
  379.     <TextView android:layout_width="match_parent"
  380.         android:layout_height="wrap_content"
  381.         android:hint=""
  382.         android:gravity="center"
  383.         android:textSize="35dp" />
  384.  
  385.  
  386.     <TextView android:layout_width="match_parent"
  387.  
  388.         android:layout_height="wrap_content"
  389.         android:hint="hello user"
  390.         android:gravity="center"
  391.         android:textSize="35dp"
  392.         android:id="@+id/txt_hello"
  393.  
  394.         />
  395.     <Button
  396.         android:layout_gravity="center"
  397.         android:padding="20dp"
  398.         android:layout_width="wrap_content"
  399.         android:layout_height="wrap_content"
  400.         android:text="LogOut"
  401.         android:textSize="25dp"
  402.         android:onClick="btnLogOut"/>
  403.     </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement