Advertisement
Guest User

my login app

a guest
Jan 12th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.84 KB | None | 0 0
  1. //strings
  2. -------------------
  3. <resources>
  4.     <string name="app_name">MyLogin</string>
  5.     <string name="myLogo">LOGO</string>
  6.     <string name="hintUserName">Enter user name...</string>
  7.     <string name="hintPassword">Enter user password...</string>
  8.     <string name="hintChecked">password checked</string>
  9.     <string name="btnLogin">Login</string>
  10.     <string name="msgLogo">Hello Esra !!!</string>
  11.     <string name="newUser">New User</string>
  12.     <string name="btnRegister">Register</string>
  13.     <string name="btnCanel">Cancel</string>
  14. </resources>
  15. ----------------------------
  16. //logInActivity
  17. ---------------------------
  18. package com.example.android.loginapp;
  19.  
  20.         import android.content.Context;
  21.         import android.content.Intent;
  22.         import android.graphics.Color;
  23.         import android.support.v7.app.AppCompatActivity;
  24.         import android.os.Bundle;
  25.         import android.util.Log;
  26.         import android.view.View;
  27.         import android.widget.Button;
  28.         import android.widget.EditText;
  29.         import android.widget.TextView;
  30.         import android.widget.Toast;
  31.  
  32. public class LoginActivity extends AppCompatActivity {
  33.  
  34.     final String TAG="LoginScreen";
  35.  
  36.     EditText txtUser,txtPass;
  37.     Button btnLogin;
  38.     TextView txtLogo;
  39.     final String USER_NAME="Esra",USER_PASS="12345";
  40.     Context context;
  41.  
  42.     @Override
  43.     protected void onCreate(Bundle savedInstanceState) {
  44.         super.onCreate(savedInstanceState);
  45.         setContentView(R.layout.activity_login);
  46.         setPointer();
  47.     }
  48.  
  49.     private void setPointer()
  50.     {
  51.         this.context=this;
  52.         txtUser=(EditText) findViewById(R.id.txtUser);
  53.         txtPass=(EditText)findViewById(R.id.txtPass);
  54.         txtLogo=(TextView)findViewById(R.id.txtLogo);
  55.         txtLogo.setOnLongClickListener(new View.OnLongClickListener() {
  56.             @Override
  57.             public boolean onLongClick(View v) {
  58.                 Toast.makeText(context, context.getResources().getString(R.string.msgLogo), Toast.LENGTH_SHORT).show();
  59.                 return true;
  60.             }
  61.         });
  62.  
  63.         final int orgHintColor=txtLogo.getCurrentHintTextColor();
  64.         btnLogin=(Button)findViewById(R.id.btnLogin);
  65.         btnLogin.setOnClickListener(new View.OnClickListener() {
  66.             @Override
  67.             public void onClick(View roni) {
  68.                 String userName=txtUser.getText().toString();
  69.                 String userPass=txtPass.getText().toString();
  70.                 //validation for user name
  71.                 if (userName.length()<1)
  72.                 {
  73.                     txtUser.setText("");
  74.                     txtUser.setHintTextColor(Color.RED);
  75.                     return;
  76.                 }
  77.                 if (userPass.length()<1)
  78.                 {
  79.                     txtPass.setText("");
  80.                     txtPass.setHintTextColor(Color.RED);
  81.                     return;
  82.                 }
  83.                 //check user
  84.                 if (Utl.checkUser(userName,userPass,USER_NAME,USER_PASS))
  85.                 {
  86.                     //user is correct -true
  87.                     /*
  88.                     Toast.makeText(context, "Hello my Master", Toast.LENGTH_SHORT).show();
  89.                     txtPass.setText("");
  90.                     txtPass.setHintTextColor(orgHintColor);
  91.                     txtUser.setText("");
  92.                     txtUser.setHintTextColor(orgHintColor);
  93.                     */
  94.                     //move now to second screen - which is our main screen
  95.                     Intent myIntent = new Intent(context,TaskActivity.class);
  96.                     myIntent.putExtra("userName",USER_NAME);
  97.                     startActivity(myIntent);
  98.                     finish();
  99.                     Log.i(TAG, "onClick: User OK");
  100.                 }
  101.                 else
  102.                 {
  103.                     //user is incorrect -false
  104.                     txtPass.setText("");
  105.                     Toast.makeText(context, "You are not allowed!!", Toast.LENGTH_LONG).show();
  106.                     Log.e(TAG, "onClick: Incorrect User" );
  107.                 }
  108.             }
  109.         });
  110.     }
  111. }
  112. ------------------
  113. //registerActivity
  114. ------------------
  115. package com.example.android.loginapp;
  116.  
  117. import android.support.v7.app.AppCompatActivity;
  118. import android.os.Bundle;
  119. import android.widget.Button;
  120. import android.widget.EditText;
  121.  
  122. public class RegisterActivity extends AppCompatActivity {
  123.         EditText txtUser,txtPass,txtCheckPass;
  124.         Button btnReg,btnCancel;
  125.     @Override
  126.     protected void onCreate(Bundle savedInstanceState) {
  127.         super.onCreate(savedInstanceState);
  128.         setContentView(R.layout.activity_register);
  129.         setPointer();
  130.     }
  131.     private void setPointer()
  132.     {
  133.         txtUser=(EditText)findViewById(R.id.txtUserReg);
  134.         txtPass=(EditText)findViewById(R.id.txtPassReg);
  135.         txtCheckPass=(EditText)findViewById(R.id.txtCheckPass);
  136.         btnReg=(Button)findViewById(R.id.btnReg);
  137.         btnCancel=(Button)findViewById(R.id.btnCancel);
  138.     }
  139. }
  140. -----------------------
  141. //taskActivity
  142. -----------------------
  143. package com.example.android.loginapp;
  144.  
  145. import android.support.v7.app.AppCompatActivity;
  146. import android.os.Bundle;
  147. import android.widget.TextView;
  148. import android.widget.Toast;
  149.  
  150. public class TaskActivity extends AppCompatActivity {
  151.  
  152.     TaskActivity context;
  153.     TextView txtLogo;
  154.     boolean isBackPressed=false;
  155.  
  156.     @Override
  157.     protected void onCreate(Bundle savedInstanceState) {
  158.         super.onCreate(savedInstanceState);
  159.         setContentView(R.layout.activity_task);
  160.         setPointer();
  161.         getExtra();
  162.     }
  163.  
  164.     private void setPointer()
  165.     {
  166.         this.context=this;
  167.         txtLogo=(TextView)findViewById(R.id.txtHello);
  168.     }
  169.  
  170.     private void getExtra()
  171.     {
  172.         String myData = getIntent().getStringExtra("userName");
  173.         txtLogo.setText("Hello "+myData);
  174.         Toast.makeText(context, "user "+myData+" is loged in...", Toast.LENGTH_SHORT).show();
  175.     }
  176.  
  177.     @Override
  178.     public void onBackPressed() {
  179.         if (!isBackPressed)
  180.         {
  181.             isBackPressed=true;
  182.             Toast.makeText(context, "Press again to exit", Toast.LENGTH_SHORT).show();
  183.         }
  184.         else
  185.         {
  186.             finish();
  187.         }
  188.     }
  189.     }
  190.  
  191. -------------------------------
  192. //Utl
  193. --------------------------
  194. package com.example.android.loginapp;
  195.  
  196. /**
  197.  * Created by Android on 09/01/2017.
  198.  */
  199.  
  200. public class Utl {
  201.  
  202.     public static boolean checkUser(String userName, String userPass, String myUser, String myPass) {
  203.         return (userName.equals(myUser) && userPass.equals(myPass));
  204.     }
  205.  
  206. }
  207. ------------------------------------
  208. //UtlShared
  209. ---------------------------------------
  210. package com.example.android.loginapp;
  211.  
  212. import android.content.SharedPreferences;
  213.  
  214. /**
  215.  * Created by Android on 09/01/2017.
  216.  */
  217.  
  218. public class UtlShared {
  219.     SharedPreferences userPref;
  220.     SharedPreferences.Editor editor;
  221.     publi
  222. }
  223. --------------------------------
  224. //activity_login
  225. ----------------------------
  226. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  227.     android:layout_width="match_parent"
  228.     android:layout_height="match_parent"
  229.     android:orientation="vertical">
  230.  
  231.     <!-- my logo layout -->
  232.     <LinearLayout
  233.         android:layout_width="match_parent"
  234.         android:layout_height="match_parent"
  235.         android:orientation="vertical"
  236.         android:layout_weight="1"
  237.         >
  238.         <TextView
  239.             android:layout_width="match_parent"
  240.             android:layout_height="match_parent"
  241.             android:text="@string/myLogo"
  242.             android:textSize="90sp"
  243.             android:gravity="center"
  244.             android:id="@+id/txtLogo"
  245.             />
  246.     </LinearLayout>
  247.  
  248.     <!-- my Edit text box -->
  249.     <LinearLayout
  250.         android:layout_width="match_parent"
  251.         android:layout_height="match_parent"
  252.         android:orientation="vertical"
  253.         android:layout_weight="1">
  254.         <EditText
  255.             android:layout_width="match_parent"
  256.             android:layout_height="match_parent"
  257.             android:hint="@string/hintUserName"
  258.             android:textSize="25sp"
  259.             android:layout_weight="1"
  260.             android:id="@+id/txtUser"/>
  261.         <LinearLayout
  262.             android:layout_width="match_parent"
  263.             android:layout_height="5px"
  264.             android:orientation="vertical"
  265.             android:background="#000"/>
  266.         <EditText
  267.             android:layout_width="match_parent"
  268.             android:layout_height="match_parent"
  269.             android:hint="@string/hintPassword"
  270.             android:textSize="25sp"
  271.             android:inputType="numberPassword"
  272.             android:layout_weight="1"
  273.             android:id="@+id/txtPass"
  274.             />
  275.  
  276.     </LinearLayout>
  277.  
  278.     <!-- my Login Button -->
  279.     <LinearLayout
  280.         android:layout_width="match_parent"
  281.         android:layout_height="match_parent"
  282.         android:orientation="vertical"
  283.         android:layout_weight="1">
  284.         <Button
  285.             android:layout_width="match_parent"
  286.             android:layout_height="wrap_content"
  287.             android:background="#009fff"
  288.             android:textColor="#ffffff"
  289.             android:text="@string/btnLogin"
  290.             android:textSize="26sp"
  291.             android:id="@+id/btnLogin"/>
  292.     </LinearLayout>
  293. </LinearLayout>
  294. ---------------------------------
  295. //activity_register
  296. --------------------------------
  297. <LinearLayout android:orientation="vertical"
  298.     xmlns:android="http://schemas.android.com/apk/res/android"
  299.     android:layout_width="match_parent"
  300.     android:layout_height="match_parent">
  301.     <LinearLayout
  302.         android:layout_height="match_parent"
  303.         android:layout_width="match_parent"
  304.         android:orientation="vertical"
  305.         android:layout_weight="1">
  306.         <TextView
  307.             android:layout_width="match_parent"
  308.             android:layout_height="match_parent"
  309.             android:text="@string/newUser"
  310.             android:textSize="@dimen/regttl"
  311.             android:gravity="center"
  312.             />
  313.      </LinearLayout>
  314.  
  315.     <LinearLayout
  316.         android:layout_width="match_parent"
  317.         android:layout_height="match_parent"
  318.         android:layout_weight="1"
  319.         android:orientation="vertical">
  320.         <EditText
  321.             android:layout_width="match_parent"
  322.             android:layout_height="wrap_content"
  323.             android:layout_weight="1"
  324.             android:hint="@string/hintUserName"
  325.             android:textSize="@dimen/regEditText"
  326.             android:id="@+id/txtUserReg"/>
  327.         <EditText
  328.             android:layout_width="match_parent"
  329.             android:layout_height="wrap_content"
  330.             android:layout_weight="1"
  331.             android:hint="@string/hintPassword"
  332.             android:textSize="@dimen/regEditText"
  333.             android:id="@+id/txtPassReg"/>
  334.         <EditText
  335.             android:layout_width="match_parent"
  336.             android:layout_height="wrap_content"
  337.             android:layout_weight="1"
  338.             android:hint="@string/hintChecked"
  339.             android:textSize="@dimen/regEditText"
  340.             android:id="@+id/txtCheckPass"/>
  341.     </LinearLayout>
  342.     <LinearLayout
  343.         android:layout_width="match_parent"
  344.         android:layout_height="match_parent"
  345.         android:layout_weight="1"
  346.         android:orientation="horizontal"
  347.         android:layout_gravity="center">
  348.         <Button
  349.             android:layout_width="match_parent"
  350.             android:layout_height="wrap_content"
  351.             android:layout_weight="1"
  352.             android:background="@color/btnbg"
  353.             android:textColor="@color/btnText"
  354.             android:text="@string/btnRegister"
  355.             android:layout_margin="@dimen/btnMargin"
  356.             android:id="@+id/btnReg"/>
  357.         <Button
  358.             android:layout_width="match_parent"
  359.             android:layout_height="wrap_content"
  360.             android:layout_weight="1"
  361.             android:background="@color/btnbg"
  362.             android:textColor="@color/btnText"
  363.             android:text="@string/btnCanel"
  364.             android:layout_margin="@dimen/btnMargin"
  365.             android:id="@+id/btnCancel"/>
  366.     </LinearLayout>
  367. </LinearLayout>
  368. --------------------------------
  369. //activity_task
  370. -----------------------------
  371. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  372.     android:layout_width="match_parent"
  373.     android:layout_height="match_parent"
  374.     android:orientation="vertical">
  375.  
  376.     <TextView
  377.         android:layout_width="match_parent"
  378.         android:layout_height="wrap_content"
  379.         android:textSize="32sp"
  380.         android:text="place holder"
  381.         android:gravity="center"
  382.         android:layout_marginTop="15dp"
  383.         android:id="@+id/txtHello"/>
  384.  
  385. </LinearLayout>
  386. ------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement