Advertisement
Guest User

MyLoginScreen

a guest
Jan 5th, 2017
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.34 KB | None | 0 0
  1. Utl.java
  2. ===================
  3. public class Utl {
  4.     public static boolean checkUser(String userName,String userPass,String myUser,String myPass)
  5.     {
  6.         return (userName.equals(myUser) && userPass.equals(myPass));
  7.     }
  8. }
  9.  
  10.  
  11. LoginActivity.java
  12. ============================
  13.  
  14. public class LoginActivity extends AppCompatActivity {
  15.  
  16.     final String TAG="LoginScreen";
  17.  
  18.     EditText txtUser,txtPass;
  19.     Button btnLogin;
  20.     TextView txtLogo;
  21.     final String USER_NAME="Firas",USER_PASS="12345";
  22.     Context context;
  23.  
  24.     @Override
  25.     protected void onCreate(Bundle savedInstanceState) {
  26.         super.onCreate(savedInstanceState);
  27.         setContentView(R.layout.activity_login);
  28.         setPointer();
  29.     }
  30.  
  31.     private void setPointer()
  32.     {
  33.         this.context=this;
  34.         txtUser=(EditText) findViewById(R.id.txtUser);
  35.         txtPass=(EditText)findViewById(R.id.txtPass);
  36.         txtLogo=(TextView)findViewById(R.id.txtLogo);
  37.         final int orgHintColor=txtLogo.getCurrentHintTextColor();
  38.         btnLogin=(Button)findViewById(R.id.btnLogin);
  39.         btnLogin.setOnClickListener(new View.OnClickListener() {
  40.             @Override
  41.             public void onClick(View roni) {
  42.                 String userName=txtUser.getText().toString();
  43.                 String userPass=txtPass.getText().toString();
  44.                 //validation for user name
  45.                 if (userName.length()<1)
  46.                 {
  47.                     txtUser.setText("");
  48.                     txtUser.setHintTextColor(Color.RED);
  49.                     return;
  50.                 }
  51.                 if (userPass.length()<1)
  52.                 {
  53.                     txtPass.setText("");
  54.                     txtPass.setHintTextColor(Color.RED);
  55.                     return;
  56.                 }
  57.                 //check user
  58.                 if (Utl.checkUser(userName,userPass,USER_NAME,USER_PASS))
  59.                 {
  60.                     //user is correct -true
  61.                     Toast.makeText(context, "Hello my Master", Toast.LENGTH_SHORT).show();
  62.                     txtPass.setText("");
  63.                     txtPass.setHintTextColor(orgHintColor);
  64.                     txtUser.setText("");
  65.                     txtUser.setHintTextColor(orgHintColor);
  66.                     Log.i(TAG, "onClick: User OK");
  67.                 }
  68.                 else
  69.                 {
  70.                     //user is incorrect -false
  71.                     txtPass.setText("");
  72.                     Toast.makeText(context, "You are not allowed!!", Toast.LENGTH_LONG).show();
  73.                     Log.e(TAG, "onClick: Incorrect User" );
  74.                 }
  75.             }
  76.         });
  77.     }
  78. }
  79.  
  80.  
  81. activity_main.xml
  82. ==========================
  83. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  84.     android:layout_width="match_parent"
  85.     android:layout_height="match_parent"
  86.     android:orientation="vertical">
  87.  
  88.     <!-- my logo layout -->
  89.     <LinearLayout
  90.         android:layout_width="match_parent"
  91.         android:layout_height="match_parent"
  92.         android:orientation="vertical"
  93.         android:layout_weight="1"
  94.         >
  95.         <TextView
  96.             android:layout_width="match_parent"
  97.             android:layout_height="match_parent"
  98.             android:text="@string/myLogo"
  99.             android:textSize="90sp"
  100.             android:gravity="center"
  101.             android:id="@+id/txtLogo"
  102.             />
  103.     </LinearLayout>
  104.  
  105.     <!-- my Edit text box -->
  106.     <LinearLayout
  107.         android:layout_width="match_parent"
  108.         android:layout_height="match_parent"
  109.         android:orientation="vertical"
  110.         android:layout_weight="1">
  111.         <EditText
  112.             android:layout_width="match_parent"
  113.             android:layout_height="match_parent"
  114.             android:hint="@string/hintUserName"
  115.             android:textSize="25sp"
  116.             android:layout_weight="1"
  117.             android:id="@+id/txtUser"/>
  118.         <LinearLayout
  119.             android:layout_width="match_parent"
  120.             android:layout_height="5px"
  121.             android:orientation="vertical"
  122.             android:background="#000"/>
  123.         <EditText
  124.             android:layout_width="match_parent"
  125.             android:layout_height="match_parent"
  126.             android:hint="@string/hintPassword"
  127.             android:textSize="25sp"
  128.             android:inputType="numberPassword"
  129.             android:layout_weight="1"
  130.             android:id="@+id/txtPass"
  131.             />
  132.  
  133.     </LinearLayout>
  134.  
  135.     <!-- my Login Button -->
  136.     <LinearLayout
  137.         android:layout_width="match_parent"
  138.         android:layout_height="match_parent"
  139.         android:orientation="vertical"
  140.         android:layout_weight="1">
  141.         <Button
  142.             android:layout_width="match_parent"
  143.             android:layout_height="wrap_content"
  144.             android:background="#009fff"
  145.             android:textColor="#ffffff"
  146.             android:text="@string/btnLogin"
  147.             android:textSize="26sp"
  148.             android:id="@+id/btnLogin"/>
  149.     </LinearLayout>
  150. </LinearLayout>
  151.  
  152.  
  153. strings.xml
  154. =====================
  155. <resources>
  156.     <string name="app_name">MyLogin</string>
  157.     <string name="myLogo">LOGO</string>
  158.     <string name="hintUserName">Enter user name...</string>
  159.     <string name="hintPassword">Enter user password...</string>
  160.     <string name="btnLogin">Login</string>
  161. </resources>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement