Advertisement
Guest User

Untitled

a guest
Aug 27th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.38 KB | None | 0 0
  1. MainActivity.java
  2. =======================
  3.  
  4.  
  5. package com.example.adm.myapplication;
  6.  
  7. import android.app.Fragment;
  8. import android.content.Intent;
  9. import android.support.v7.app.AppCompatActivity;
  10. import android.os.Bundle;
  11. import android.view.View;
  12. import android.widget.Button;
  13. import android.widget.EditText;
  14. import android.widget.TextView;
  15. import android.widget.Toast;
  16. import android.content.Context;
  17.  
  18. public class MainActivity extends AppCompatActivity {
  19.  
  20.     EditText txtUserName;
  21.     EditText txtUserPass;
  22.     TextView lblMsg;
  23.     Context context;
  24.     private int totalTry = 5;
  25.     Button btnLogin;
  26.     Button btnRegister;
  27.  
  28.  
  29.  
  30.     @Override
  31.     protected void onCreate(Bundle savedInstanceState) {
  32.         super.onCreate(savedInstanceState);
  33.         setContentView(R.layout.activity_main);
  34.         setPointer();
  35.         this.context=this;
  36.     }
  37.  
  38.     private void setPointer() {
  39.         txtUserName = (EditText) findViewById(R.id.txtUserName);
  40.         txtUserPass = (EditText) findViewById(R.id.txtUserPassword);
  41.         lblMsg = (TextView) findViewById(R.id.lblMsg);
  42.         btnLogin = (Button) findViewById(R.id.btnLogin);
  43.         btnRegister = (Button) findViewById(R.id.btnRegister);
  44.  
  45.         //listerners
  46.         btnLogin.setOnClickListener(new View.OnClickListener() {
  47.             @Override
  48.             public void onClick(View view) {
  49.                 login(txtUserName, txtUserPass);
  50.             }
  51.         });
  52.  
  53.         btnRegister.setOnClickListener(new View.OnClickListener() {
  54.             @Override
  55.             public void onClick(View view) {
  56.                 register();
  57.             }
  58.         });
  59.     }
  60.  
  61.         public void login(EditText txtUserName, EditText txtUserPass) {
  62.  
  63.  
  64.  
  65.             String getUser= this.txtUserName.getText().toString();
  66.             String getPass= this.txtUserPass.getText().toString();
  67.  
  68.  
  69.  
  70.  
  71.        if (totalTry == 0) {
  72.             Toast.makeText(MainActivity.this, "You are an idiot!!", Toast.LENGTH_SHORT).show();
  73.             return;
  74.         }
  75.  
  76.  
  77.  
  78.             if (getUser.equals((this.txtUserName.getText().toString())))
  79.                 if (getPass.equals(((this.txtUserPass.getText().toString())))) {
  80.                     lblMsg.setText("User loged...");
  81.                     startActivity(new Intent(this, Welcom.class));
  82.                 } else {
  83.                     totalTry--;
  84.                     Toast.makeText(MainActivity.this, "you have " + totalTry + " times", Toast.LENGTH_LONG).show();
  85.  
  86.                 }
  87.         else {
  88.         totalTry--;
  89.         Toast.makeText(MainActivity.this, "you have " + totalTry + " times", Toast.LENGTH_LONG).show();
  90.  
  91.     }
  92.             }
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.     private void register() {
  101.         startActivity(new Intent(this, Register.class));
  102.     }
  103.  
  104.  
  105. }
  106.  
  107.  
  108. main_activity.xml
  109. ==================
  110.  
  111. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  112.     android:layout_width="match_parent"
  113.     android:layout_height="match_parent"
  114.     android:orientation="vertical">
  115.  
  116.     <LinearLayout
  117.         android:layout_width="match_parent"
  118.         android:layout_height="150dp"
  119.         android:orientation="vertical">
  120.  
  121.         <TextView
  122.             android:layout_width="match_parent"
  123.             android:layout_height="match_parent"
  124.             android:gravity="center"
  125.             android:text="LOGO"
  126.             android:textSize="100sp">
  127.  
  128.         </TextView>
  129.     </LinearLayout>
  130.  
  131.     <LinearLayout
  132.         android:layout_width="match_parent"
  133.         android:layout_height="100dp"
  134.         android:orientation="vertical">
  135.  
  136.         <EditText
  137.             android:id="@+id/txtUserName"
  138.             android:layout_width="match_parent"
  139.             android:layout_height="wrap_content"
  140.             android:hint="Enter your name...."
  141.             android:inputType="text"
  142.             android:textSize="22sp" />
  143.  
  144.         <EditText
  145.             android:id="@+id/txtUserPassword"
  146.             android:layout_width="match_parent"
  147.             android:layout_height="wrap_content"
  148.             android:hint="Enter Password..."
  149.             android:inputType="textPassword"
  150.             android:textSize="22sp" />
  151.     </LinearLayout>
  152.  
  153.     <LinearLayout
  154.         android:layout_width="match_parent"
  155.         android:layout_height="150dp"
  156.         android:orientation="vertical">
  157.  
  158.         <Button
  159.             android:layout_width="match_parent"
  160.             android:layout_height="wrap_content"
  161.             android:text="Login"
  162.             android:background="#00BFFF"
  163.             android:textColor="#ffffff"
  164.             android:textSize="32sp"
  165.             android:id="@+id/btnLogin"
  166.             android:layout_marginBottom="20dp"/>
  167.  
  168.         <Button
  169.             android:layout_width="match_parent"
  170.             android:layout_height="wrap_content"
  171.             android:text="Register"
  172.             android:background="#00BFFF"
  173.             android:textColor="#ffffff"
  174.             android:textSize="32sp"
  175.             android:id="@+id/btnRegister"/>
  176.     </LinearLayout>
  177.  
  178.     <LinearLayout
  179.         android:layout_width="match_parent"
  180.         android:layout_height="match_parent"
  181.         android:orientation="vertical">
  182.         <TextView
  183.             android:layout_width="match_parent"
  184.             android:layout_height="match_parent"
  185.             android:textSize="32sp"
  186.             android:text="MY LOGIN SYSTEM V1"
  187.             android:gravity="center"
  188.             android:id="@+id/lblMsg"/>
  189.     </LinearLayout>
  190. </LinearLayout>
  191.  
  192.  
  193.  
  194. utlShared.java
  195. ====================
  196.  
  197. package com.example.adm.myapplication;
  198.  
  199. import android.content.Context;
  200. import android.content.SharedPreferences;
  201. import android.text.Editable;
  202. import android.util.Log;
  203.  
  204. /**
  205.  * Created by ADM on 8/26/2016.
  206.  */
  207. public class utlShared {
  208.     //context to use later
  209.     Context context;
  210.     //declatrtion of shared preferences object
  211.     private SharedPreferences userPref;
  212.     //declaration of shared preferences editor
  213.     private SharedPreferences.Editor editor;
  214.  
  215.     public utlShared() {}
  216.  
  217.     public utlShared(Context context)
  218.     {
  219.         //get context to use it
  220.         this.context=context;
  221.         //declaretion of shared preferences with file name and file mode (private,public)
  222.         userPref=context.getSharedPreferences("users",Context.MODE_PRIVATE);
  223.         //declaration of editor
  224.         editor=userPref.edit();
  225.     }
  226.  
  227.     //get user and password
  228.     public void addUser(String userName, String password)
  229.     {
  230.         //stores in the phone device under data\data\package name
  231.         //put in shared preferences user name and password
  232.         editor.putString(userName,password);
  233.         //commit (save/apply) the changes.
  234.         editor.commit();
  235.     }
  236.  
  237.     public boolean checkUser(String userName)
  238.     {
  239.         //get name by key->userName
  240.         String checkString = userPref.getString(userName,"na");
  241.  
  242.         //print to logcat a custom message.....
  243.         Log.e("checkUser", "checkUser: "+checkString );
  244.         //check if userName equals to responded data, if it's na, we don't havce the user...
  245.         return !checkString.equals("na");
  246.     }
  247. }
  248.  
  249.  
  250. Register.java
  251. ====================
  252.  
  253. package com.example.adm.myapplication;
  254.  
  255. import android.content.Context;
  256. import android.support.v7.app.AppCompatActivity;
  257. import android.os.Bundle;
  258. import android.view.View;
  259. import android.widget.Button;
  260. import android.widget.EditText;
  261. import android.widget.Toast;
  262.  
  263. import java.util.Objects;
  264.  
  265.  
  266. public class Register extends AppCompatActivity {
  267.  
  268.     Button btnCancel;
  269.     Button btnRegister;
  270.     Context context;
  271.     EditText txtUser,txtPass,txtPass2;
  272.  
  273.  
  274.     @Override
  275.     protected void onCreate(Bundle savedInstanceState) {
  276.         super.onCreate(savedInstanceState);
  277.         setContentView(R.layout.activity_register);
  278.         this.context=this;
  279.         setPointer();
  280.     }
  281.  
  282.     private void setPointer()
  283.     {
  284.         txtUser=(EditText)findViewById(R.id.txtUserName);
  285.         txtPass=(EditText)findViewById(R.id.txtPassword);
  286.         txtPass2=(EditText)findViewById(R.id.txtPassword2);
  287.         btnCancel=(Button)findViewById(R.id.btnCancel);
  288.         btnCancel.setOnClickListener(new View.OnClickListener() {
  289.             @Override
  290.             public void onClick(View view) {
  291.                 finish();
  292.             }
  293.         });
  294.  
  295.         btnRegister=(Button)findViewById(R.id.btnRegisterUser);
  296.         btnRegister.setOnClickListener(new View.OnClickListener() {
  297.             @Override
  298.             public void onClick(View view) {
  299.  
  300.  
  301.                 //calling the utl shared class
  302.                 utlShared myShared = new utlShared(context);
  303.  
  304.                 //check if pass equals
  305.                 if (!Objects.equals(txtPass.getText().toString(), txtPass2.getText().toString()))
  306.                 {
  307.                     Toast.makeText(context, "error pass not equal bitch....",Toast.LENGTH_LONG).show();
  308.                     return;
  309.                 }
  310.  
  311.                 //check if user exists
  312.                 if (myShared.checkUser(txtUser.getText().toString()))
  313.                 {
  314.                     Toast.makeText(context, "User Exists....", Toast.LENGTH_SHORT).show();
  315.                     return;
  316.                 }
  317.                 //running the method of add user
  318.                 myShared.addUser(txtUser.getText().toString(),txtPass.getText().toString());
  319.                 finish();
  320.             }
  321.         });
  322.     }
  323.  
  324.  
  325.     }
  326.  
  327.  
  328. activity_register.xml
  329. =========================
  330.  
  331.  
  332. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  333.     android:layout_width="match_parent"
  334.     android:layout_height="match_parent"
  335.     android:orientation="vertical">
  336.  
  337.     <TextView
  338.         android:layout_width="match_parent"
  339.         android:layout_height="wrap_content"
  340.         android:layout_marginTop="20dp"
  341.         android:gravity="center"
  342.         android:text="USER REGISTERTION"
  343.         android:textSize="32sp" />
  344.  
  345.     <RelativeLayout
  346.         android:layout_width="match_parent"
  347.         android:layout_height="match_parent">
  348.  
  349.         <EditText
  350.             android:layout_width="match_parent"
  351.             android:layout_height="wrap_content"
  352.             android:hint="Enter User Name...."
  353.             android:id="@+id/txtUserName"/>
  354.         <EditText
  355.             android:layout_width="match_parent"
  356.             android:layout_height="wrap_content"
  357.             android:hint="Enter password...."
  358.             android:id="@+id/txtPassword"
  359.             android:layout_below="@id/txtUserName"/>
  360.  
  361.         <EditText
  362.             android:layout_width="match_parent"
  363.             android:layout_height="wrap_content"
  364.             android:hint="Enter password again..."
  365.             android:id="@+id/txtPassword2"
  366.             android:layout_below="@id/txtPassword"/>
  367.         <Button
  368.             android:layout_width="150dp"
  369.             android:layout_height="wrap_content"
  370.             android:id="@+id/btnRegisterUser"
  371.             android:layout_below="@id/txtPassword2"
  372.             android:text="Register"/>
  373.         <Button
  374.             android:layout_width="150dp"
  375.             android:layout_height="wrap_content"
  376.             android:text="Cancel"
  377.             android:id="@+id/btnCancel"
  378.             android:layout_toEndOf="@id/btnRegisterUser"
  379.             android:layout_below="@id/txtPassword2"/>
  380.     </RelativeLayout>
  381.  
  382. </LinearLayout>
  383.  
  384.  
  385.  
  386. Welcome.java
  387. ==================
  388. package com.example.adm.myapplication;
  389.  
  390. import android.support.v7.app.AppCompatActivity;
  391. import android.os.Bundle;
  392.  
  393. public class Welcom extends AppCompatActivity {
  394.  
  395.     @Override
  396.     protected void onCreate(Bundle savedInstanceState) {
  397.         super.onCreate(savedInstanceState);
  398.         setContentView(R.layout.activity_welcom);
  399.     }
  400. }
  401.  
  402.  
  403. Welcome.xml
  404. =====================
  405.  
  406.  
  407. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  408.     android:layout_width="match_parent"
  409.     android:layout_height="match_parent"
  410.     android:orientation="vertical">
  411.  
  412.     <LinearLayout
  413.         android:layout_width="match_parent"
  414.         android:layout_height="match_parent"
  415.         android:orientation="vertical"
  416.         android:background="@drawable/sexypolice">
  417.  
  418.         <TextView
  419.             android:layout_width="match_parent"
  420.             android:layout_height="match_parent"
  421.             android:gravity="center"
  422.             android:text="Welcom"
  423.             android:textSize="100sp"
  424.             android:textColor="#ffffff">
  425.  
  426.         </TextView>
  427.  
  428.     </LinearLayout>
  429. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement