Advertisement
Guest User

H/W Login

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