Advertisement
Guest User

example of register and login

a guest
Aug 25th, 2016
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.62 KB | None | 0 0
  1. Register.java
  2. ====================
  3. package com.example.zeevm.mysharedpref;
  4.  
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.Button;
  9.  
  10. public class Register extends AppCompatActivity {
  11.  
  12.     Button btnCancel;
  13.     @Override
  14.     protected void onCreate(Bundle savedInstanceState) {
  15.         super.onCreate(savedInstanceState);
  16.         setContentView(R.layout.activity_register);
  17.         setPointer();
  18.     }
  19.  
  20.     private void setPointer()
  21.     {
  22.         btnCancel=(Button)findViewById(R.id.btnCancel);
  23.         btnCancel.setOnClickListener(new View.OnClickListener() {
  24.             @Override
  25.             public void onClick(View view) {
  26.                 finish();
  27.             }
  28.         });
  29.     }
  30.  
  31. }
  32.  
  33.  
  34.  
  35. activity_register.xml
  36. ==============================
  37. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  38.     android:layout_width="match_parent"
  39.     android:layout_height="match_parent"
  40.     android:orientation="vertical">
  41.  
  42.     <TextView
  43.         android:layout_width="match_parent"
  44.         android:layout_height="wrap_content"
  45.         android:layout_marginTop="20dp"
  46.         android:gravity="center"
  47.         android:text="USER REGISTERTION"
  48.         android:textSize="32sp" />
  49.  
  50.     <RelativeLayout
  51.         android:layout_width="match_parent"
  52.         android:layout_height="match_parent">
  53.  
  54.         <EditText
  55.             android:layout_width="match_parent"
  56.             android:layout_height="wrap_content"
  57.             android:hint="Enter User Name...."
  58.             android:id="@+id/txtUserName"/>
  59.         <EditText
  60.             android:layout_width="match_parent"
  61.             android:layout_height="wrap_content"
  62.             android:hint="Enter password...."
  63.             android:id="@+id/txtPassword"
  64.             android:layout_below="@id/txtUserName"/>
  65.  
  66.         <EditText
  67.             android:layout_width="match_parent"
  68.             android:layout_height="wrap_content"
  69.             android:hint="Enter password again..."
  70.             android:id="@+id/txtPassword2"
  71.             android:layout_below="@id/txtPassword"/>
  72.         <Button
  73.             android:layout_width="150dp"
  74.             android:layout_height="wrap_content"
  75.             android:id="@+id/btnRegisterUser"
  76.             android:layout_below="@id/txtPassword2"
  77.             android:text="Register"/>
  78.         <Button
  79.             android:layout_width="150dp"
  80.             android:layout_height="wrap_content"
  81.             android:text="Cancel"
  82.             android:id="@+id/btnCancel"
  83.            android:layout_toEndOf="@id/btnRegisterUser"
  84.             android:layout_below="@id/txtPassword2"/>
  85.     </RelativeLayout>
  86.  
  87. </LinearLayout>
  88.  
  89.  
  90.  
  91. MainActivity.java
  92. =====================
  93. package com.example.zeevm.mysharedpref;
  94.  
  95. import android.content.Intent;
  96. import android.support.v7.app.AppCompatActivity;
  97. import android.os.Bundle;
  98. import android.view.View;
  99. import android.widget.Button;
  100. import android.widget.EditText;
  101. import android.widget.TextView;
  102. import android.widget.Toast;
  103.  
  104. public class MainActivity extends AppCompatActivity {
  105.  
  106.     EditText txtUserName;
  107.     EditText txtUserPass;
  108.     TextView lblMsg;
  109.     final String MY_USER="zeev";
  110.     final String MY_PASS="12345";
  111.     private int totalTry=5;
  112.     Button btnLogin;
  113.     Button btnRegister;
  114.  
  115.     @Override
  116.     protected void onCreate(Bundle savedInstanceState) {
  117.         super.onCreate(savedInstanceState);
  118.         setContentView(R.layout.activity_main);
  119.         setPointer();
  120.     }
  121.  
  122.     private void setPointer()
  123.     {
  124.         txtUserName=(EditText)findViewById(R.id.txtUserName);
  125.         txtUserPass=(EditText)findViewById(R.id.txtUserPassword);
  126.         lblMsg=(TextView)findViewById(R.id.lblMsg);
  127.         btnLogin=(Button)findViewById(R.id.btnLogin);
  128.         btnRegister=(Button)findViewById(R.id.btnRegister);
  129.  
  130.         //listerners
  131.         btnLogin.setOnClickListener(new View.OnClickListener() {
  132.             @Override
  133.             public void onClick(View view) {
  134.                login();
  135.             }
  136.         });
  137.  
  138.         btnRegister.setOnClickListener(new View.OnClickListener() {
  139.             @Override
  140.             public void onClick(View view) {
  141.                 register();
  142.             }
  143.         });
  144.     }
  145.  
  146.     private void login()
  147.     {
  148.         String getUser=txtUserName.getText().toString();
  149.         String getPass=txtUserPass.getText().toString();
  150.  
  151.         if (totalTry==0) {
  152.             Toast.makeText(MainActivity.this, "You are an idiot!!", Toast.LENGTH_SHORT).show();
  153.             return;
  154.         }
  155.  
  156.         if (getUser.equals(MY_USER) && getPass.equals(MY_PASS))
  157.         {
  158.             lblMsg.setText("User loged...");
  159.         }
  160.         else
  161.         {
  162.             totalTry--;
  163.             Toast.makeText(MainActivity.this, "you have "+totalTry+" times", Toast.LENGTH_LONG).show();
  164.  
  165.         }
  166.     }
  167.  
  168.     private void register()
  169.     {
  170.         startActivity(new Intent(this,Register.class));
  171.     }
  172.  
  173. }
  174.  
  175.  
  176. activity_main.xml
  177. ======================
  178. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  179.     android:layout_width="match_parent"
  180.     android:layout_height="match_parent"
  181.     android:orientation="vertical">
  182.  
  183.     <LinearLayout
  184.         android:layout_width="match_parent"
  185.         android:layout_height="150dp"
  186.         android:orientation="vertical">
  187.  
  188.         <TextView
  189.             android:layout_width="match_parent"
  190.             android:layout_height="match_parent"
  191.             android:gravity="center"
  192.             android:text="LOGO"
  193.             android:textSize="100sp">
  194.  
  195.         </TextView>
  196.     </LinearLayout>
  197.  
  198.     <LinearLayout
  199.         android:layout_width="match_parent"
  200.         android:layout_height="100dp"
  201.         android:orientation="vertical">
  202.  
  203.         <EditText
  204.             android:id="@+id/txtUserName"
  205.             android:layout_width="match_parent"
  206.             android:layout_height="wrap_content"
  207.             android:hint="Enter your name...."
  208.             android:inputType="text"
  209.             android:textSize="22sp" />
  210.  
  211.         <EditText
  212.             android:id="@+id/txtUserPassword"
  213.             android:layout_width="match_parent"
  214.             android:layout_height="wrap_content"
  215.             android:hint="Enter Password..."
  216.             android:inputType="textPassword"
  217.             android:textSize="22sp" />
  218.     </LinearLayout>
  219.  
  220.     <LinearLayout
  221.         android:layout_width="match_parent"
  222.         android:layout_height="150dp"
  223.         android:orientation="vertical">
  224.  
  225.         <Button
  226.             android:layout_width="match_parent"
  227.             android:layout_height="wrap_content"
  228.             android:text="Login"
  229.             android:background="#00BFFF"
  230.             android:textColor="#ffffff"
  231.             android:textSize="32sp"
  232.             android:id="@+id/btnLogin"
  233.             android:layout_marginBottom="20dp"/>
  234.  
  235.         <Button
  236.             android:layout_width="match_parent"
  237.             android:layout_height="wrap_content"
  238.             android:text="Register"
  239.             android:background="#00BFFF"
  240.             android:textColor="#ffffff"
  241.             android:textSize="32sp"
  242.             android:id="@+id/btnRegister"/>
  243.     </LinearLayout>
  244.  
  245.     <LinearLayout
  246.         android:layout_width="match_parent"
  247.         android:layout_height="match_parent"
  248.         android:orientation="vertical">
  249.         <TextView
  250.             android:layout_width="match_parent"
  251.             android:layout_height="match_parent"
  252.             android:textSize="32sp"
  253.             android:text="MY LOGIN SYSTEM V1"
  254.             android:gravity="center"
  255.             android:id="@+id/lblMsg"/>
  256.     </LinearLayout>
  257. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement