Advertisement
lieutenant74

myTasks golan

Sep 27th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.02 KB | None | 0 0
  1. activity_main
  2. ******************
  3. <LinearLayout android:layout_height="match_parent"
  4.     xmlns:android="http://schemas.android.com/apk/res/android"
  5.     android:layout_width="match_parent"
  6.     android:orientation="vertical"
  7.     android:gravity="center"
  8.     android:background="#DCDCDC">
  9.  
  10.     <LinearLayout
  11.         android:layout_width="match_parent"
  12.         android:layout_height="match_parent"
  13.         android:layout_weight="1"
  14.         android:gravity="center"
  15.         android:orientation="vertical">
  16.  
  17.         <ImageView
  18.             android:id="@+id/imageView"
  19.             android:layout_width="183dp"
  20.             android:layout_height="match_parent"
  21.             android:src="@drawable/task" />
  22.  
  23.     </LinearLayout>
  24.  
  25.     <LinearLayout
  26.         android:layout_width="match_parent"
  27.         android:layout_height="match_parent"
  28.         android:orientation="vertical"
  29.         android:layout_weight="1"
  30.         android:gravity="center"
  31.         android:background="#A9A9A9">
  32.  
  33.         <EditText
  34.             android:id="@+id/userlogin"
  35.             android:layout_width="300dp"
  36.             android:layout_height="wrap_content"
  37.             android:layout_marginTop="20dp"
  38.             android:hint="Enter your user name.."/>
  39.  
  40.         <EditText
  41.             android:id="@+id/userpass"
  42.             android:layout_width="300dp"
  43.             android:layout_height="wrap_content"
  44.             android:layout_marginTop="20dp"
  45.             android:hint="Enter your password.."
  46.             android:inputType="textPassword"/>
  47.  
  48.     </LinearLayout>
  49.  
  50.     <LinearLayout
  51.         android:layout_width="match_parent"
  52.         android:layout_height="match_parent"
  53.         android:orientation="horizontal"
  54.         android:layout_weight="1"
  55.         android:gravity="center"
  56.         android:background="#A9A9A9">
  57.  
  58.         <Button
  59.             android:id="@+id/btnLogin"
  60.             android:layout_width="164dp"
  61.             android:layout_height="wrap_content"
  62.             android:text="Login"
  63.             android:onClick="onClick"
  64.             android:textColor="#ffffff"
  65.             android:textSize="22sp"
  66.             android:background="#AFEEEE"
  67.             android:layout_margin="10dp"/>
  68.  
  69.         <Button
  70.             android:id="@+id/btnRegister"
  71.             android:layout_width="164dp"
  72.             android:layout_height="wrap_content"
  73.             android:text="Register"
  74.             android:onClick="onClick"
  75.             android:textColor="#ffffff"
  76.             android:textSize="22sp"
  77.             android:background="#AFEEEE"
  78.             android:layout_margin="10dp"/>
  79.  
  80.     </LinearLayout>
  81.  
  82.     <LinearLayout
  83.         android:layout_width="match_parent"
  84.         android:layout_height="match_parent"
  85.         android:orientation="vertical"
  86.         android:layout_weight="1"
  87.         android:gravity="center">
  88.  
  89.         <TextView
  90.             android:layout_width="match_parent"
  91.             android:layout_height="wrap_content"
  92.             android:textSize="18dp"
  93.             android:gravity="center"
  94.             android:text="Note this app has been posted by G.V"/>
  95.  
  96.     </LinearLayout>
  97.  
  98. </LinearLayout>
  99.  
  100. main_activity.java
  101. **************
  102. package com.example.mytasks;
  103.  
  104. import android.content.Intent;
  105. import android.support.v7.app.AppCompatActivity;
  106. import android.os.Bundle;
  107. import android.view.View;
  108. import android.widget.EditText;
  109. import android.widget.Toast;
  110.  
  111. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  112.  
  113.     EditText lUser,lPass;
  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.         lUser=(EditText)findViewById(R.id.userlogin);
  124.         lPass=(EditText)findViewById(R.id.userpass);
  125.     }
  126.  
  127.     @Override
  128.     public void onClick(View view) {
  129.         switch (view.getId()){
  130.             case R.id.btnLogin:
  131.                 //                         Key                       Value
  132.                 if (!SharedPref.checkPass(lUser.getText().toString(),lPass.getText().toString(),this))
  133.                 {
  134.                     Toast.makeText(this, "Invalid user name or pass", Toast.LENGTH_LONG).show();
  135.                     return;
  136.                 }
  137.                 Intent intent = new Intent(this, Tasks.class);
  138.                 intent.putExtra("userName",lUser.getText().toString());
  139.                 startActivity(intent);
  140.                 break;
  141.  
  142.             case R.id.btnRegister:
  143.                 startActivity(new Intent(this,Register.class));
  144.                 break;
  145.  
  146.             default:
  147.                 Toast.makeText(this, "Error in button", Toast.LENGTH_SHORT).show();
  148.         }
  149.  
  150.     }
  151.     private void checkLogin() {
  152.     }
  153. }
  154. sharedPref.java
  155. *******************
  156. package com.example.mytasks;
  157.  
  158. import android.content.Context;
  159. import android.content.SharedPreferences;
  160.  
  161. /**
  162.  * Created by גולן on 27/09/2017.
  163.  */
  164.  
  165. public class SharedPref {
  166.  
  167.     //for login screen
  168.     public static boolean checkPass(String userName,String userPass, Context context)
  169.     {
  170.         //declaration of shared preferences
  171.         SharedPreferences prefs=context.getSharedPreferences("myPrefs",Context.MODE_PRIVATE);
  172.         //get user pass
  173.         String spPass=prefs.getString(userName,"na");
  174.         //check if password matches
  175.         if (userPass.equals(spPass))
  176.         {
  177.             return true;
  178.         }
  179.         return false;
  180.     }
  181.  
  182.     public static boolean checkUserExists(String userName, Context context)
  183.     {
  184.         //declaration of shared preferences
  185.         SharedPreferences prefs=context.getSharedPreferences("myPrefs",Context.MODE_PRIVATE);
  186.         //get user pass
  187.         String spPass=prefs.getString(userName,"na");
  188.         //check if password matches
  189.         return !spPass.equals("na");
  190.     }
  191.  
  192.     public static void registerUser(String userName, String userPass, Context context)
  193.     {
  194.         //declaration of shared preferences
  195.         SharedPreferences prefs=context.getSharedPreferences("myPrefs",Context.MODE_PRIVATE);
  196.  
  197.         //declaration of shared preferences Editor
  198.         SharedPreferences.Editor editor = prefs.edit();
  199.  
  200.         //put data inside, we use hash map , so we need KEY,VALUE (K,V)
  201.         editor.putString(userName,userPass);
  202.  
  203.         //sending data to my shared prefernces file, we have to commit the changes
  204.         editor.commit();
  205.     }
  206. }
  207.  
  208. activity_register.xml
  209. ************************
  210. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  211.     android:layout_width="match_parent"
  212.     android:layout_height="match_parent"
  213.     android:orientation="vertical">
  214.  
  215.     <LinearLayout
  216.         android:layout_width="match_parent"
  217.         android:layout_height="match_parent"
  218.         android:layout_weight="1"
  219.         android:orientation="vertical"
  220.         android:gravity="center">
  221.         <TextView
  222.             android:layout_width="wrap_content"
  223.             android:layout_height="wrap_content"
  224.             android:text="REGISTER"
  225.             android:layout_gravity="center"
  226.             android:textSize="32sp"/>
  227.     </LinearLayout>
  228.     <LinearLayout
  229.         android:layout_width="match_parent"
  230.         android:layout_height="match_parent"
  231.         android:layout_weight="1"
  232.         android:orientation="vertical">
  233.         <EditText
  234.             android:layout_width="match_parent"
  235.             android:layout_height="wrap_content"
  236.             android:hint="Enter user name..."
  237.             android:id="@+id/rName"/>
  238.         <EditText
  239.             android:layout_width="match_parent"
  240.             android:layout_height="wrap_content"
  241.             android:hint="Enter password..."
  242.             android:id="@+id/rpass"
  243.             android:inputType="textPassword"/>
  244.         <EditText
  245.             android:layout_width="match_parent"
  246.             android:layout_height="wrap_content"
  247.             android:hint="Retype password..."
  248.             android:id="@+id/rCheck"
  249.             android:inputType="textPassword"/>
  250.  
  251.     </LinearLayout>
  252.     <LinearLayout
  253.         android:layout_width="match_parent"
  254.         android:layout_height="match_parent"
  255.         android:layout_weight="1"
  256.         android:orientation="horizontal">
  257.         <Button
  258.             android:id="@+id/rCancel"
  259.             android:layout_width="match_parent"
  260.             android:layout_height="wrap_content"
  261.             android:layout_margin="20dp"
  262.             android:layout_weight="1"
  263.             android:background="#009fff"
  264.             android:onClick="onClick"
  265.             android:text="Cancel"
  266.             android:textColor="#ffffff"
  267.             android:textSize="22sp"
  268.             />
  269.  
  270.         <Button
  271.             android:id="@+id/rRegister"
  272.             android:layout_width="match_parent"
  273.             android:layout_height="wrap_content"
  274.             android:layout_margin="20dp"
  275.             android:layout_weight="1"
  276.             android:background="#009fff"
  277.             android:onClick="onClick"
  278.             android:text="Register"
  279.             android:textColor="#ffffff"
  280.             android:textSize="22sp" />
  281.     </LinearLayout>
  282.  
  283. </LinearLayout>
  284.  
  285. register.java
  286. ******************
  287. package com.example.mytasks;
  288.  
  289. import android.support.v7.app.AppCompatActivity;
  290. import android.os.Bundle;
  291. import android.view.View;
  292. import android.widget.EditText;
  293. import android.widget.Toast;
  294.  
  295. public class Register extends AppCompatActivity implements View.OnClickListener{
  296.  
  297.     EditText rUser, rPass, rCheck;
  298.  
  299.     @Override
  300.     protected void onCreate(Bundle savedInstanceState) {
  301.         super.onCreate(savedInstanceState);
  302.         setContentView(R.layout.activity_register);
  303.         setPointer();
  304.     }
  305.  
  306.     private void setPointer() {
  307.         rUser = (EditText) findViewById(R.id.rName);
  308.         rPass = (EditText) findViewById(R.id.rpass);
  309.         rCheck = (EditText) findViewById(R.id.rCheck);
  310.     }
  311.  
  312.     @Override
  313.     public void onClick(View view) {
  314.         switch (view.getId()) {
  315.             case R.id.rCancel:
  316.                 finish();
  317.                 break;
  318.  
  319.             case R.id.rRegister:
  320.                 registerUser();
  321.                 break;
  322.  
  323.             default:
  324.                 Toast.makeText(this, "Error in buttons", Toast.LENGTH_SHORT).show();
  325.         }
  326.     }
  327.  
  328.     private void registerUser() {
  329.         //check if all fields filled
  330.         if (rUser.getText().toString().isEmpty() ||
  331.                 rPass.getText().toString().isEmpty() ||
  332.                 rCheck.getText().toString().isEmpty())
  333.         {
  334.             Toast.makeText(this, "Please fill all fields", Toast.LENGTH_LONG).show();
  335.             return;
  336.         }
  337.  
  338.         //check if minimum password length > 2
  339.         if (rPass.getText().toString().length()<3)
  340.         {
  341.             Toast.makeText(this, "Password is too short (3 minimum)", Toast.LENGTH_SHORT).show();
  342.             return;
  343.         }
  344.  
  345.         //check if password match
  346.         if (!rPass.getText().toString().equals(rCheck.getText().toString()))
  347.         {
  348.             Toast.makeText(this, "Password not match", Toast.LENGTH_LONG).show();
  349.             return;
  350.         }
  351.  
  352.         //check if user exists
  353.         if (SharedPref.checkUserExists(rUser.getText().toString(),this))
  354.         {
  355.             Toast.makeText(this, "User already exists", Toast.LENGTH_LONG).show();
  356.             return;
  357.         }
  358.  
  359.         //register user and finitu la comedia
  360.         SharedPref.registerUser(rUser.getText().toString(),rPass.getText().toString(),this);
  361.         finish();
  362.     }
  363.     }
  364.  
  365. activity_tasks.xml
  366. *****************
  367. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  368.     android:layout_width="match_parent"
  369.     android:layout_height="match_parent"
  370.     android:orientation="vertical">
  371.  
  372.     <TextView
  373.         android:layout_width="match_parent"
  374.         android:layout_height="wrap_content"
  375.         android:text="hello"
  376.         android:gravity="center"
  377.         android:layout_marginTop="10dp"
  378.         android:textSize="30sp"
  379.         android:id="@+id/userName"/>
  380.  
  381. </LinearLayout>
  382.  
  383. tasks.java
  384. ***************
  385. package com.example.mytasks;
  386.  
  387. import android.content.Context;
  388. import android.support.v7.app.AppCompatActivity;
  389. import android.os.Bundle;
  390. import android.widget.TextView;
  391. import android.widget.Toast;
  392.  
  393. public class Tasks extends AppCompatActivity {
  394.  
  395.     TextView userName;
  396.     Context context;
  397.  
  398.     @Override
  399.     protected void onCreate(Bundle savedInstanceState) {
  400.         super.onCreate(savedInstanceState);
  401.         setContentView(R.layout.activity_tasks);
  402.         setPointer();
  403.  
  404.         //get the data from previous activity.
  405.         Bundle extras = getIntent().getExtras();
  406.         if (extras==null)
  407.         {
  408.             Toast.makeText(context, "Error in getting info", Toast.LENGTH_SHORT).show();
  409.         }
  410.         else
  411.         {
  412.             String uName=extras.getString("userName");
  413.             userName.setText("Hello "+uName);
  414.         }
  415.  
  416.     }
  417.  
  418.     private void setPointer() {
  419.         this.context=this;
  420.         userName=(TextView)findViewById(R.id.userlogin);
  421.     }
  422. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement