Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////////////////////////////////////////////////////////main activity//////////////////////////////////////////////////////////////
- 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 {
- public static final String DEFULT = "N/A" ;
- 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 prefs = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
- userName = prefs.getString("user",DEFULT);
- password = prefs.getString("password" , DEFULT);
- }
- //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)&&(pass.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();
- String password = pass.getText().toString();
- editor.putString("user", userName);
- editor.putString("password" , password);
- editor.commit();
- Intent iMain = new Intent(this,MainActivity.class);
- Toast.makeText(getApplicationContext(), "You have successfully register ",
- 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);
- }
- }
- //////////////////////////////////////////////////////////////log in/////////////////////////////////////////////////////////
- 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;
- import android.widget.Toast;
- 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",MainActivity.DEFULT);
- Toast.makeText(getApplicationContext(),accName,
- Toast.LENGTH_LONG).show();
- // 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);
- }
- }
- ////////////////////////////////////////////////////////////main activity xml//////////////////////////////////////////////////////
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:gravity="center_vertical"
- android:background="#3b00c4ff"
- 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">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Log in "
- android:textSize="60sp"
- android:textStyle="bold"
- android:textColor="#b8ffffff"/>
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="User name"
- android:textSize="30sp"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textSize="30sp"
- android:background="#849bf5ef"
- android:id="@+id/userName"
- android:onClick="uName"
- />
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Password"
- android:textSize="30sp"
- />
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textSize="30sp"
- android:background="#849bf5ef"
- android:id="@+id/pass"
- android:inputType="textPassword"
- 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="#3b00c4ff"
- 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"
- >
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Username"
- android:textSize="30sp"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textSize="30sp"
- android:background="#849bf5ef"
- android:id="@+id/user"
- android:onClick="userDel"/>
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Password"
- android:textSize="30sp"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textSize="30sp"
- android:background="#849bf5ef"
- android:id="@+id/password"
- android:inputType="textPassword"
- android:onClick="passDel"/>
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Confirm password"
- android:textSize="30sp"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textSize="30sp"
- android:background="#849bf5ef"
- android:id="@+id/password2"
- android:inputType="textPassword"
- 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>
- //////////////////////////////////////////////////////////////log in xml/////////////////////////////////////////////////////////
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:background="#3b00c4ff"
- 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