Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- \///////////////////////////////////////////////\\\mainActivity/////
- package com.example.dorinzrihen.loginapp;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.text.Editable;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
- import junit.framework.Test;
- public class MainActivity extends Activity {
- EditText user ;
- EditText pass;
- String password ;
- String userName ;
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- user =(EditText)findViewById(R.id.userName);
- pass = (EditText)findViewById(R.id.pass);
- SharedPreferences prefPass = getApplicationContext().getSharedPreferences("mypass" ,MODE_PRIVATE);
- password = prefPass.getString("password", "error");
- SharedPreferences prefUser = getApplicationContext().getSharedPreferences("myPref", MODE_PRIVATE);
- userName = prefUser.getString("user","error");
- }
- //make the text from the xml remove
- public void uName(View v)
- {
- user.setText(" ");
- }
- public void pas(View v)
- {
- pass.setText(" ");
- }
- //register function
- public void btnR(View v)
- {
- Intent iRegister = new Intent(this,Register.class);
- this.startActivity(iRegister);
- }
- //check if the username and the password are similar
- public void btnL(View v)
- {
- Toast.makeText(getApplicationContext(),user.getText().toString()+ userName ,
- Toast.LENGTH_LONG).show();
- if((user.getText().toString().equals(userName)&&(user.getText().toString().equals(password))))
- {
- Intent iLogin = new Intent (this,Login.class);
- iLogin.putExtra("user",user.getText().toString());
- this.startActivity(iLogin);
- }
- else
- {
- Toast.makeText(getApplicationContext(), "your username are not Exists",
- Toast.LENGTH_LONG).show();
- }
- }
- }
- /////////////////////////////////////////////////reg//////
- package com.example.dorinzrihen.loginapp;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
- public class Register extends Activity {
- EditText user;
- EditText pass;
- EditText pass2;
- //set pointers
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_register);
- user = (EditText) findViewById(R.id.user);
- pass = (EditText) findViewById(R.id.password);
- pass2 = (EditText) findViewById(R.id.password2);
- }
- public void register(View v)
- {
- if(pass.getText().toString().equals(pass2.getText().toString()) )
- {
- SharedPreferences prefs = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
- SharedPreferences.Editor editor = prefs.edit();
- String userName = user.getText().toString();
- editor.putString("user", userName);
- editor.commit();
- SharedPreferences pref = getApplicationContext().getSharedPreferences("Mypass", MODE_PRIVATE);
- SharedPreferences.Editor editorpass = pref.edit();
- String password = pass.getText().toString();
- editorpass.putString("password", password);
- editorpass.commit();
- Intent iMain = new Intent(this,MainActivity.class);
- iMain.putExtra("user",user.getText().toString());
- iMain.putExtra("password",pass.getText().toString());
- Toast.makeText(getApplicationContext(), user.getText().toString()+ pass.getText().toString() ,
- Toast.LENGTH_LONG).show();
- this.startActivity(iMain);
- }
- //if the passwords not the same
- else
- {
- Toast.makeText(getApplicationContext(), "passwords are not similar",
- Toast.LENGTH_LONG).show();
- }
- }
- public void userDel(View v)
- {
- user.setText(" ");
- }
- public void passDel(View v)
- {
- pass.setText(" ");
- }
- public void pass2Del(View v)
- {
- pass2.setText(" ");
- }
- //check if the passwords are the same and if they are shareprefernce to the password and to the username
- //the function to go back to the main activity
- public void cencel(View v)
- {
- Intent iMain = new Intent(this,MainActivity.class);
- this.startActivity(iMain);
- }
- }
- /////////////////////////////////////////////login///
- package com.example.dorinzrihen.loginapp;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.widget.EditText;
- import android.widget.TextView;
- public class Login extends Activity
- {
- TextView txtHello ;
- String accName;
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- SharedPreferences prefs = getApplicationContext().getSharedPreferences("myPref", MODE_PRIVATE);
- accName = prefs.getString("user","eror");
- // the username + hello
- txtHello = (TextView)findViewById(R.id.helloS);
- txtHello.setText("Hello" + accName);
- }
- //log out and delete the user name that e used
- public void logout()
- {
- SharedPreferences prefs = getApplicationContext().getSharedPreferences("MyPref",MODE_PRIVATE);
- SharedPreferences.Editor editor = prefs.edit();
- editor.remove("user");
- editor.commit();
- Intent iMain = new Intent(this,MainActivity.class);
- this.startActivity(iMain);
- }
- }
- \///////////////////////////////////////////////\\\mainActivity///// xml
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:gravity="center_vertical"
- android:background="#489ce997"
- xmlns:android="http://schemas.android.com/apk/res/android" >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:orientation="vertical"
- android:gravity="center">
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textSize="30sp"
- android:text="User Name"
- android:background="#849bf5a9"
- android:id="@+id/userName"
- android:onClick="uName"
- />
- <EditText
- android:layout_width="match_parent"
- android:text="Password"
- android:layout_height="wrap_content"
- android:textSize="30sp"
- android:background="#849bf5a9"
- android:id="@+id/pass"
- android:onClick="pas"
- />
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:orientation="vertical"
- >
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Log in"
- android:textSize="30sp"
- android:gravity="center"
- android:id="@+id/btnLog"
- android:onClick="btnL"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Register"
- android:textSize="30sp"
- android:gravity="center"
- android:id="@+id/btnReg"
- android:onClick="btnR"/>
- </LinearLayout>
- </LinearLayout>
- /////////////////////////////////////////////////reg////// xml
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:background="#7c91dd84"
- xmlns:android="http://schemas.android.com/apk/res/android" >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:gravity="center"
- android:layout_weight="1"
- android:orientation="vertical"
- >
- <EditText
- android:text="User name"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textSize="30sp"
- android:background="#3cffffff"
- android:id="@+id/user"
- android:onClick="userDel"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="your password"
- android:textSize="30sp"
- android:background="#3cffffff"
- android:id="@+id/password"
- android:onClick="passDel"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="click your pass again"
- android:textSize="30sp"
- android:background="#3cffffff"
- android:id="@+id/password2"
- android:onClick="pass2Del"/>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:orientation="vertical"
- android:gravity="center">
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Register me"
- android:textSize="30sp"
- android:id="@+id/reg"
- android:onClick="register"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Just Cancel"
- android:textSize="30sp"
- android:id="@+id/cen"
- android:onClick="cencel"/>
- </LinearLayout>
- </LinearLayout>
- /////////////////////////////////////////////login/// xml
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:background="#5079d566"
- xmlns:android="http://schemas.android.com/apk/res/android" >
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="hello"
- android:textSize="50sp"
- android:id="@+id/helloS"
- />
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Log out"
- android:textSize="30sp"
- android:id="@+id/btnOut"
- android:onClick="logout"/>
- </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement