Advertisement
Guest User

dorin

a guest
Mar 11th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 11.09 KB | None | 0 0
  1. \///////////////////////////////////////////////\\\mainActivity/////
  2.  
  3.  
  4. package com.example.dorinzrihen.loginapp;
  5.  
  6.         import android.app.Activity;
  7.         import android.content.Intent;
  8.         import android.content.SharedPreferences;
  9.         import android.support.v7.app.ActionBarActivity;
  10.         import android.os.Bundle;
  11.         import android.text.Editable;
  12.         import android.view.View;
  13.         import android.widget.EditText;
  14.         import android.widget.Toast;
  15.  
  16.         import junit.framework.Test;
  17.  
  18. public class MainActivity extends Activity {
  19.  
  20.     EditText user ;
  21.     EditText pass;
  22.  
  23.  
  24.     String password ;
  25.     String userName ;
  26.  
  27.     protected void onCreate(Bundle savedInstanceState) {
  28.         super.onCreate(savedInstanceState);
  29.         setContentView(R.layout.activity_main);
  30.         user =(EditText)findViewById(R.id.userName);
  31.         pass = (EditText)findViewById(R.id.pass);
  32.  
  33.         SharedPreferences prefPass = getApplicationContext().getSharedPreferences("mypass" ,MODE_PRIVATE);
  34.         password = prefPass.getString("password", "error");
  35.  
  36.         SharedPreferences prefUser = getApplicationContext().getSharedPreferences("myPref", MODE_PRIVATE);
  37.         userName = prefUser.getString("user","error");
  38.  
  39.     }
  40. //make the text from the xml remove
  41.     public void uName(View v)
  42.     {
  43.         user.setText(" ");
  44.     }
  45.     public void pas(View v)
  46.     {
  47.         pass.setText(" ");
  48.     }
  49.  
  50.  
  51.  
  52.  
  53.     //register function
  54.     public void btnR(View v)
  55.     {
  56.         Intent iRegister = new Intent(this,Register.class);
  57.         this.startActivity(iRegister);
  58.     }
  59. //check if the username and the password are similar
  60.     public void btnL(View v)
  61.     {
  62.         Toast.makeText(getApplicationContext(),user.getText().toString()+ userName  ,
  63.                 Toast.LENGTH_LONG).show();
  64.  
  65.         if((user.getText().toString().equals(userName)&&(user.getText().toString().equals(password))))
  66.         {
  67.             Intent iLogin = new Intent (this,Login.class);
  68.             iLogin.putExtra("user",user.getText().toString());
  69.             this.startActivity(iLogin);
  70.         }
  71.         else
  72.         {
  73.             Toast.makeText(getApplicationContext(), "your username are not Exists",
  74.                     Toast.LENGTH_LONG).show();
  75.         }
  76.     }
  77.  
  78.  
  79. }
  80.  
  81. /////////////////////////////////////////////////reg//////
  82.  
  83. package com.example.dorinzrihen.loginapp;
  84.  
  85. import android.app.Activity;
  86. import android.content.Intent;
  87. import android.content.SharedPreferences;
  88. import android.os.Bundle;
  89. import android.view.View;
  90. import android.widget.EditText;
  91. import android.widget.Toast;
  92.  
  93.  
  94. public class Register  extends Activity {
  95.         EditText user;
  96.         EditText pass;
  97.         EditText pass2;
  98. //set pointers
  99.         protected void onCreate(Bundle savedInstanceState)
  100.         {
  101.             super.onCreate(savedInstanceState);
  102.             setContentView(R.layout.activity_register);
  103.           user = (EditText) findViewById(R.id.user);
  104.            pass = (EditText) findViewById(R.id.password);
  105.            pass2 = (EditText) findViewById(R.id.password2);
  106.         }
  107.  
  108.     public void register(View v)
  109.     {
  110.        if(pass.getText().toString().equals(pass2.getText().toString()) )
  111.         {
  112.             SharedPreferences prefs = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
  113.             SharedPreferences.Editor editor = prefs.edit();
  114.             String userName = user.getText().toString();
  115.             editor.putString("user", userName);
  116.             editor.commit();
  117.             SharedPreferences pref = getApplicationContext().getSharedPreferences("Mypass", MODE_PRIVATE);
  118.             SharedPreferences.Editor editorpass = pref.edit();
  119.             String password = pass.getText().toString();
  120.             editorpass.putString("password", password);
  121.             editorpass.commit();
  122.             Intent iMain = new Intent(this,MainActivity.class);
  123.             iMain.putExtra("user",user.getText().toString());
  124.             iMain.putExtra("password",pass.getText().toString());
  125.             Toast.makeText(getApplicationContext(), user.getText().toString()+ pass.getText().toString() ,
  126.                     Toast.LENGTH_LONG).show();
  127.             this.startActivity(iMain);
  128.  
  129.         }
  130.         //if the passwords not the same
  131.         else
  132.         {
  133.             Toast.makeText(getApplicationContext(), "passwords are not similar",
  134.                     Toast.LENGTH_LONG).show();
  135.         }
  136.     }
  137.     public void userDel(View v)
  138.     {
  139.         user.setText(" ");
  140.     }
  141.  
  142.     public void passDel(View v)
  143.     {
  144.         pass.setText(" ");
  145.     }
  146.  
  147.     public void pass2Del(View v)
  148.     {
  149.         pass2.setText(" ");
  150.     }
  151. //check if the passwords are the same and if they are shareprefernce to the password and to the username
  152.  
  153.     //the function to go back to the main activity
  154.     public void cencel(View v)
  155.     {
  156.         Intent iMain = new Intent(this,MainActivity.class);
  157.         this.startActivity(iMain);
  158.     }
  159. }
  160.  
  161. /////////////////////////////////////////////login///
  162.  
  163.  
  164.  
  165. package com.example.dorinzrihen.loginapp;
  166.  
  167. import android.app.Activity;
  168. import android.content.Intent;
  169. import android.content.SharedPreferences;
  170. import android.os.Bundle;
  171. import android.widget.EditText;
  172. import android.widget.TextView;
  173.  
  174. public class Login extends Activity
  175. {
  176.     TextView txtHello ;
  177.  
  178.  
  179.         String accName;
  180.  
  181.     protected void onCreate(Bundle savedInstanceState) {
  182.         super.onCreate(savedInstanceState);
  183.         setContentView(R.layout.activity_login);
  184.  
  185.  
  186.         SharedPreferences prefs = getApplicationContext().getSharedPreferences("myPref", MODE_PRIVATE);
  187.         accName = prefs.getString("user","eror");
  188.  
  189.  
  190. // the username + hello
  191.         txtHello = (TextView)findViewById(R.id.helloS);
  192.         txtHello.setText("Hello" + accName);
  193.  
  194.  
  195.     }
  196.  
  197.  
  198.  
  199. //log out and delete the user name that e used
  200.     public void logout()
  201.     {
  202.         SharedPreferences prefs = getApplicationContext().getSharedPreferences("MyPref",MODE_PRIVATE);
  203.         SharedPreferences.Editor editor = prefs.edit();
  204.         editor.remove("user");
  205.         editor.commit();
  206.         Intent iMain = new Intent(this,MainActivity.class);
  207.         this.startActivity(iMain);
  208.     }
  209. }
  210.  
  211.  
  212. \///////////////////////////////////////////////\\\mainActivity///// xml
  213.  
  214.  
  215. <LinearLayout
  216.     android:layout_width="match_parent"
  217.     android:layout_height="match_parent"
  218.     android:orientation="vertical"
  219.     android:gravity="center_vertical"
  220.     android:background="#489ce997"
  221.     xmlns:android="http://schemas.android.com/apk/res/android" >
  222.  <LinearLayout
  223.      android:layout_width="match_parent"
  224.      android:layout_height="match_parent"
  225.      android:layout_weight="1"
  226.      android:orientation="vertical"
  227.      android:gravity="center">
  228.  
  229. <EditText
  230.     android:layout_width="match_parent"
  231.     android:layout_height="wrap_content"
  232.     android:textSize="30sp"
  233.     android:text="User Name"
  234.     android:background="#849bf5a9"
  235.     android:id="@+id/userName"
  236.     android:onClick="uName"
  237.     />
  238.  
  239. <EditText
  240.     android:layout_width="match_parent"
  241.     android:text="Password"
  242.     android:layout_height="wrap_content"
  243.     android:textSize="30sp"
  244.     android:background="#849bf5a9"
  245.     android:id="@+id/pass"
  246.     android:onClick="pas"
  247.  
  248.     />
  249.  
  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="vertical"
  257.         >
  258.     <Button
  259.         android:layout_width="match_parent"
  260.         android:layout_height="wrap_content"
  261.         android:text="Log in"
  262.         android:textSize="30sp"
  263.         android:gravity="center"
  264.         android:id="@+id/btnLog"
  265.         android:onClick="btnL"/>
  266.         <Button
  267.             android:layout_width="match_parent"
  268.             android:layout_height="wrap_content"
  269.             android:text="Register"
  270.             android:textSize="30sp"
  271.             android:gravity="center"
  272.             android:id="@+id/btnReg"
  273.             android:onClick="btnR"/>
  274.  
  275.     </LinearLayout>
  276.  
  277. </LinearLayout>
  278.  
  279.  
  280. /////////////////////////////////////////////////reg//////  xml
  281.  
  282.  
  283. <LinearLayout
  284.     android:layout_width="match_parent"
  285.     android:layout_height="match_parent"
  286.     android:orientation="vertical"
  287.     android:background="#7c91dd84"
  288.     xmlns:android="http://schemas.android.com/apk/res/android" >
  289.  
  290.  
  291.     <LinearLayout
  292.         android:layout_width="match_parent"
  293.         android:layout_height="match_parent"
  294.         android:gravity="center"
  295.         android:layout_weight="1"
  296.         android:orientation="vertical"
  297.  
  298.         >
  299.  
  300.         <EditText
  301.             android:text="User name"
  302.             android:layout_width="match_parent"
  303.             android:layout_height="wrap_content"
  304.             android:textSize="30sp"
  305.             android:background="#3cffffff"
  306.             android:id="@+id/user"
  307.             android:onClick="userDel"/>
  308.  
  309.         <EditText
  310.             android:layout_width="match_parent"
  311.             android:layout_height="wrap_content"
  312.             android:text="your password"
  313.             android:textSize="30sp"
  314.             android:background="#3cffffff"
  315.             android:id="@+id/password"
  316.             android:onClick="passDel"/>
  317.  
  318.         <EditText
  319.             android:layout_width="match_parent"
  320.             android:layout_height="wrap_content"
  321.             android:text="click your pass again"
  322.             android:textSize="30sp"
  323.             android:background="#3cffffff"
  324.             android:id="@+id/password2"
  325.             android:onClick="pass2Del"/>
  326.  
  327.  
  328.     </LinearLayout>
  329.  
  330.     <LinearLayout
  331.         android:layout_width="match_parent"
  332.         android:layout_height="match_parent"
  333.         android:layout_weight="1"
  334.         android:orientation="vertical"
  335.         android:gravity="center">
  336.  
  337.         <Button
  338.             android:layout_width="match_parent"
  339.             android:layout_height="wrap_content"
  340.             android:text="Register me"
  341.             android:textSize="30sp"
  342.             android:id="@+id/reg"
  343.             android:onClick="register"/>
  344.         <Button
  345.             android:layout_width="match_parent"
  346.             android:layout_height="wrap_content"
  347.             android:text="Just Cancel"
  348.             android:textSize="30sp"
  349.             android:id="@+id/cen"
  350.             android:onClick="cencel"/>
  351.  
  352.     </LinearLayout>
  353. </LinearLayout>
  354.  
  355.  
  356. /////////////////////////////////////////////login///  xml
  357.  
  358. <LinearLayout
  359.     android:layout_width="match_parent"
  360.     android:layout_height="match_parent"
  361.     android:orientation="vertical"
  362.     android:background="#5079d566"
  363.     xmlns:android="http://schemas.android.com/apk/res/android" >
  364.  
  365. <TextView
  366.     android:layout_width="match_parent"
  367.     android:layout_height="wrap_content"
  368.     android:text="hello"
  369.     android:textSize="50sp"
  370.     android:id="@+id/helloS"
  371.     />
  372.  
  373.     <Button
  374.         android:layout_width="match_parent"
  375.         android:layout_height="wrap_content"
  376.         android:text="Log out"
  377.         android:textSize="30sp"
  378.         android:id="@+id/btnOut"
  379.         android:onClick="logout"/>
  380.  
  381.  
  382.     </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement