Advertisement
Guest User

my task

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