Advertisement
Guest User

example of what we done in the class

a guest
Aug 25th, 2016
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.24 KB | None | 0 0
  1. MainActivity.java
  2. =======================
  3. package com.example.zeevm.mysharedpref;
  4.  
  5. import android.content.Intent;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.EditText;
  11. import android.widget.TextView;
  12. import android.widget.Toast;
  13.  
  14. public class MainActivity extends AppCompatActivity {
  15.  
  16.     EditText txtUserName;
  17.     EditText txtUserPass;
  18.     TextView lblMsg;
  19.  
  20.     private int totalTry=5;
  21.     Button btnLogin;
  22.     Button btnRegister;
  23.  
  24.     @Override
  25.     protected void onCreate(Bundle savedInstanceState) {
  26.         super.onCreate(savedInstanceState);
  27.         setContentView(R.layout.activity_main);
  28.         setPointer();
  29.     }
  30.  
  31.     private void setPointer()
  32.     {
  33.         txtUserName=(EditText)findViewById(R.id.txtUserName);
  34.         txtUserPass=(EditText)findViewById(R.id.txtUserPassword);
  35.         lblMsg=(TextView)findViewById(R.id.lblMsg);
  36.         btnLogin=(Button)findViewById(R.id.btnLogin);
  37.         btnRegister=(Button)findViewById(R.id.btnRegister);
  38.  
  39.         //listerners
  40.         btnLogin.setOnClickListener(new View.OnClickListener() {
  41.             @Override
  42.             public void onClick(View view) {
  43.                login();
  44.             }
  45.         });
  46.  
  47.         btnRegister.setOnClickListener(new View.OnClickListener() {
  48.             @Override
  49.             public void onClick(View view) {
  50.                 register();
  51.             }
  52.         });
  53.     }
  54.  
  55.     private void login()
  56.     {
  57.         String getUser=txtUserName.getText().toString();
  58.         String getPass=txtUserPass.getText().toString();
  59.  
  60.         if (totalTry==0) {
  61.             Toast.makeText(MainActivity.this, "You are an idiot!!", Toast.LENGTH_SHORT).show();
  62.             return;
  63.         }
  64.         /*
  65.         if (getUser.equals(MY_USER) && getPass.equals(MY_PASS))
  66.         {
  67.             lblMsg.setText("User loged...");
  68.         }
  69.         else
  70.         {
  71.             totalTry--;
  72.             Toast.makeText(MainActivity.this, "you have "+totalTry+" times", Toast.LENGTH_LONG).show();
  73.  
  74.         }
  75.         */
  76.     }
  77.  
  78.     private void register()
  79.     {
  80.         startActivity(new Intent(this,Register.class));
  81.     }
  82.  
  83. }
  84.  
  85.  
  86. main_activity.xml
  87. ==================
  88. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  89.     android:layout_width="match_parent"
  90.     android:layout_height="match_parent"
  91.     android:orientation="vertical">
  92.  
  93.     <LinearLayout
  94.         android:layout_width="match_parent"
  95.         android:layout_height="150dp"
  96.         android:orientation="vertical">
  97.  
  98.         <TextView
  99.             android:layout_width="match_parent"
  100.             android:layout_height="match_parent"
  101.             android:gravity="center"
  102.             android:text="LOGO"
  103.             android:textSize="100sp">
  104.  
  105.         </TextView>
  106.     </LinearLayout>
  107.  
  108.     <LinearLayout
  109.         android:layout_width="match_parent"
  110.         android:layout_height="100dp"
  111.         android:orientation="vertical">
  112.  
  113.         <EditText
  114.             android:id="@+id/txtUserName"
  115.             android:layout_width="match_parent"
  116.             android:layout_height="wrap_content"
  117.             android:hint="Enter your name...."
  118.             android:inputType="text"
  119.             android:textSize="22sp" />
  120.  
  121.         <EditText
  122.             android:id="@+id/txtUserPassword"
  123.             android:layout_width="match_parent"
  124.             android:layout_height="wrap_content"
  125.             android:hint="Enter Password..."
  126.             android:inputType="textPassword"
  127.             android:textSize="22sp" />
  128.     </LinearLayout>
  129.  
  130.     <LinearLayout
  131.         android:layout_width="match_parent"
  132.         android:layout_height="150dp"
  133.         android:orientation="vertical">
  134.  
  135.         <Button
  136.             android:layout_width="match_parent"
  137.             android:layout_height="wrap_content"
  138.             android:text="Login"
  139.             android:background="#00BFFF"
  140.             android:textColor="#ffffff"
  141.             android:textSize="32sp"
  142.             android:id="@+id/btnLogin"
  143.             android:layout_marginBottom="20dp"/>
  144.  
  145.         <Button
  146.             android:layout_width="match_parent"
  147.             android:layout_height="wrap_content"
  148.             android:text="Register"
  149.             android:background="#00BFFF"
  150.             android:textColor="#ffffff"
  151.             android:textSize="32sp"
  152.             android:id="@+id/btnRegister"/>
  153.     </LinearLayout>
  154.  
  155.     <LinearLayout
  156.         android:layout_width="match_parent"
  157.         android:layout_height="match_parent"
  158.         android:orientation="vertical">
  159.         <TextView
  160.             android:layout_width="match_parent"
  161.             android:layout_height="match_parent"
  162.             android:textSize="32sp"
  163.             android:text="MY LOGIN SYSTEM V1"
  164.             android:gravity="center"
  165.             android:id="@+id/lblMsg"/>
  166.     </LinearLayout>
  167. </LinearLayout>
  168.  
  169.  
  170. utlShared.java
  171. ====================
  172. package com.example.zeevm.mysharedpref;
  173.  
  174. import android.content.Context;
  175. import android.content.SharedPreferences;
  176. import android.util.Log;
  177.  
  178. /**
  179.  * Created by zeevm on 8/25/2016.
  180.  */
  181. public class utlShared {
  182.     //context to use later
  183.     Context context;
  184.     //declatrtion of shared preferences object
  185.     private SharedPreferences userPref;
  186.     //declaration of shared preferences editor
  187.     private SharedPreferences.Editor editor;
  188.  
  189.     public utlShared() {}
  190.  
  191.     public utlShared(Context context)
  192.     {
  193.         //get context to use it
  194.         this.context=context;
  195.         //declaretion of shared preferences with file name and file mode (private,public)
  196.         userPref=context.getSharedPreferences("users",Context.MODE_PRIVATE);
  197.         //declaration of editor
  198.         editor=userPref.edit();
  199.     }
  200.  
  201.     //get user and password
  202.     public void addUser(String userName, String password)
  203.     {
  204.         //stores in the phone device under data\data\package name
  205.         //put in shared preferences user name and password
  206.         editor.putString(userName,password);
  207.         //commit (save/apply) the changes.
  208.         editor.commit();
  209.     }
  210.  
  211.     public boolean checkUser(String userName)
  212.     {
  213.         //get name by key->userName
  214.         String checkString = userPref.getString(userName,"na");
  215.  
  216.         //print to logcat a custom message.....
  217.         Log.e("checkUser", "checkUser: "+checkString );
  218.         //check if userName equals to responded data, if it's na, we don't havce the user...
  219.         return !checkString.equals("na");
  220.     }
  221. }
  222.  
  223.  
  224. Register.java
  225. ====================
  226. package com.example.zeevm.mysharedpref;
  227.  
  228. import android.content.Context;
  229. import android.support.v7.app.AppCompatActivity;
  230. import android.os.Bundle;
  231. import android.view.View;
  232. import android.widget.Button;
  233. import android.widget.EditText;
  234. import android.widget.Toast;
  235.  
  236. public class Register extends AppCompatActivity {
  237.  
  238.     Button btnCancel;
  239.     Button btnRegister;
  240.     Context context;
  241.     EditText txtUser,txtPass;
  242.  
  243.     @Override
  244.     protected void onCreate(Bundle savedInstanceState) {
  245.         super.onCreate(savedInstanceState);
  246.         setContentView(R.layout.activity_register);
  247.         this.context=this;
  248.         setPointer();
  249.     }
  250.  
  251.     private void setPointer()
  252.     {
  253.         txtUser=(EditText)findViewById(R.id.txtUserName);
  254.         txtPass=(EditText)findViewById(R.id.txtPassword);
  255.         btnCancel=(Button)findViewById(R.id.btnCancel);
  256.         btnCancel.setOnClickListener(new View.OnClickListener() {
  257.             @Override
  258.             public void onClick(View view) {
  259.                 finish();
  260.             }
  261.         });
  262.  
  263.         btnRegister=(Button)findViewById(R.id.btnRegisterUser);
  264.         btnRegister.setOnClickListener(new View.OnClickListener() {
  265.             @Override
  266.             public void onClick(View view) {
  267.                 //calling the utl shared class
  268.                 utlShared myShared = new utlShared(context);
  269.                 //check if user exists
  270.                 if (myShared.checkUser(txtUser.getText().toString()))
  271.                 {
  272.                     Toast.makeText(context, "User Exists....", Toast.LENGTH_SHORT).show();
  273.                     return;
  274.                 }
  275.                 //running the method of add user
  276.                 myShared.addUser(txtUser.getText().toString(),txtPass.getText().toString());
  277.                 finish();
  278.             }
  279.         });
  280.     }
  281.  
  282. }
  283.  
  284.  
  285. activity_register.xml
  286. ===================
  287. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  288.     android:layout_width="match_parent"
  289.     android:layout_height="match_parent"
  290.     android:orientation="vertical">
  291.  
  292.     <TextView
  293.         android:layout_width="match_parent"
  294.         android:layout_height="wrap_content"
  295.         android:layout_marginTop="20dp"
  296.         android:gravity="center"
  297.         android:text="USER REGISTERTION"
  298.         android:textSize="32sp" />
  299.  
  300.     <RelativeLayout
  301.         android:layout_width="match_parent"
  302.         android:layout_height="match_parent">
  303.  
  304.         <EditText
  305.             android:layout_width="match_parent"
  306.             android:layout_height="wrap_content"
  307.             android:hint="Enter User Name...."
  308.             android:id="@+id/txtUserName"/>
  309.         <EditText
  310.             android:layout_width="match_parent"
  311.             android:layout_height="wrap_content"
  312.             android:hint="Enter password...."
  313.             android:id="@+id/txtPassword"
  314.             android:layout_below="@id/txtUserName"/>
  315.  
  316.         <EditText
  317.             android:layout_width="match_parent"
  318.             android:layout_height="wrap_content"
  319.             android:hint="Enter password again..."
  320.             android:id="@+id/txtPassword2"
  321.             android:layout_below="@id/txtPassword"/>
  322.         <Button
  323.             android:layout_width="150dp"
  324.             android:layout_height="wrap_content"
  325.             android:id="@+id/btnRegisterUser"
  326.             android:layout_below="@id/txtPassword2"
  327.             android:text="Register"/>
  328.         <Button
  329.             android:layout_width="150dp"
  330.             android:layout_height="wrap_content"
  331.             android:text="Cancel"
  332.             android:id="@+id/btnCancel"
  333.            android:layout_toEndOf="@id/btnRegisterUser"
  334.             android:layout_below="@id/txtPassword2"/>
  335.     </RelativeLayout>
  336.  
  337. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement