Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- =====================================xml activity_man==========================================================
- <RelativeLayout
- android:layout_height="match_parent"
- android:layout_width="match_parent"
- android:id="@+id/mainTag"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textSize="30sp"
- android:hint="@string/hintUser"
- android:gravity="center"
- android:id="@+id/inUser"
- android:layout_marginTop="15sp"
- />
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textSize="30sp"
- android:inputType="textPassword"
- android:hint="@string/hintPass"
- android:gravity="center"
- android:id="@+id/inPass"
- android:layout_below="@+id/inUser"
- android:layout_marginTop="5sp"
- />
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textSize="30sp"
- android:text="@string/btnLog"
- android:id="@+id/btnLog"
- android:layout_below="@+id/inPass"
- android:layout_marginTop="40sp"
- android:padding="20sp"
- android:onClick="btnLogIn"
- />
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/btnRegi"
- android:textSize="25sp"
- android:id="@+id/btnReg"
- android:layout_below="@+id/btnLog"
- android:layout_marginTop="10sp"
- android:padding="10sp"
- android:onClick="btnRegister"
- />
- </RelativeLayout>
- =============================================================================================================================
- =========================================Java MainActivity===================================================================
- package com.example.igor.mylogin;
- 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 MainActivity extends Activity
- {
- public final static String PREFERENCE = "account";//name of the sharedprefrences file!
- private SharedPreferences myPrefs; //calling SharedPrefrences Class
- private SharedPreferences.Editor editor; //calling the editor for SharedPrefrences
- private EditText userTxt; //username input
- private EditText passTxt; //password input
- private static String chkOnline; //will be used to check if the user is online!
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- setPoints();
- /*check if user exists or not!
- if user exists go to login page automaticly
- */
- if(chkUser())
- {
- //if keyfound start second activity
- Intent logIntent = new Intent(this, Logged.class);
- logIntent.putExtra("user", chkOnline);
- this.startActivity(logIntent);
- }
- }
- //ASSISTANCE METHODS
- //method that casts all our local variables with the ids of xml
- private void setPoints()
- {
- userTxt = (EditText)findViewById(R.id.inUser);
- passTxt = (EditText)findViewById(R.id.inPass);
- myPrefs = getApplicationContext().getSharedPreferences(PREFERENCE,MODE_PRIVATE);
- editor = myPrefs.edit();
- }
- private boolean chkUser()
- {
- chkOnline = myPrefs.getString("logged", "error");
- if(chkOnline.equals("error"))
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- //inputs the username into logged
- //creates a value of username in the key logged making it appear full and not defualt
- private void apprOnline(String user)
- {
- editor.putString("logged",user);
- editor.commit();
- }
- //method that checks if the user filled all the fields, if the username is taken
- //if tha password matches and creates a user if all statements above are done right
- private void chkLog()
- {
- String user = userTxt.getText().toString();
- String pass = passTxt.getText().toString();
- if(user.trim().length() > 0 && pass.trim().length()>0)
- {
- String userName = null;
- String userPass = null;
- //if myPrefs contains username and the password input them into
- //userName and userPass Variables
- if(myPrefs.contains("user") && myPrefs.contains("password") )
- {
- userName = myPrefs.getString("user","");
- userPass = myPrefs.getString("password","");
- }
- //check if pass word and username match!
- if(user.equals(userName) && pass.equals(userPass))
- {
- //pop a toast welcome
- Toast.makeText(this,"Logged in succesfully",Toast.LENGTH_SHORT).show();
- //if the statement is true put a String value into logged
- apprOnline(user);
- // move to logged page
- Intent logIn = new Intent(this, Logged.class);
- this.startActivity(logIn);
- }
- else
- {
- //if pass and user do not match pop a toast
- Toast.makeText(this,"Wrong Username or Password",Toast.LENGTH_LONG).show();
- }
- }
- else
- {
- //user didnt add username or password
- Toast.makeText(this,"Please enter Username and Password",Toast.LENGTH_LONG).show();
- }
- }
- //BUTTONS
- //button that activates the the chkLog method and checks if all the fields and data are valid!
- public void btnLogIn(View v)
- {
- chkLog();
- }
- //button that takes the user to the registery page
- public void btnRegister(View v)
- {
- Intent rgstIntent = new Intent(this,Register.class);
- this.startActivity(rgstIntent);
- }
- }
- =======================================================================================================================================
- ==============================================xml activity_register====================================================================
- <RelativeLayout
- android:layout_height="match_parent"
- android:layout_width="match_parent"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Registration"
- android:gravity="center"
- android:textSize="20sp"
- android:id="@+id/regLine"
- android:layout_marginTop="20sp"
- />
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="@string/hintUser"
- android:gravity="center"
- android:textSize="30sp"
- android:id="@+id/setUser"
- android:layout_below="@+id/regLine"
- android:layout_marginTop="15sp"
- />
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="@string/hintPass"
- android:inputType="textPassword"
- android:gravity="center"
- android:textSize="30sp"
- android:id="@+id/setPass"
- android:layout_below="@+id/setUser"
- android:layout_marginTop="30sp" />
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="@string/confPass"
- android:inputType="textPassword"
- android:gravity="center"
- android:textSize="30sp"
- android:id="@+id/confPass"
- android:layout_below="@+id/setPass"
- android:layout_marginTop="30sp" />
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/btnRegi"
- android:textSize="30sp"
- android:id="@+id/tryReg"
- android:layout_below="@+id/confPass"
- android:layout_marginTop="10sp"
- android:onClick="btnRegister"
- />
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/canc"
- android:textSize="20sp"
- android:id="@+id/regCanc"
- android:layout_below="@+id/tryReg"
- android:layout_marginTop="20sp"
- android:onClick="btnCancle"
- />
- </RelativeLayout>
- ===================================================================================================================================
- ==========================================Java Register============================================================================
- package com.example.igor.mylogin;
- 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;
- /**
- * Created by Igor on 3/7/2016.
- */
- public class Register extends Activity
- {
- private SharedPreferences myPrefs;//calling SharedPrefrences Class
- private SharedPreferences.Editor editor;//calling the editor for SharedPrefrences
- private EditText rgsUser; //the name a user wish to register with
- private EditText rgsPass; //the password user wishes to use
- private EditText chkPass; //check if user entered the password correctly
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_register);
- setPoints();
- }
- //sets all the points connects our local variables with xml id's
- private void setPoints()
- {
- rgsUser = (EditText)findViewById(R.id.setUser);
- rgsPass = (EditText)findViewById(R.id.setPass);
- chkPass = (EditText)findViewById(R.id.confPass);
- myPrefs = getApplicationContext().getSharedPreferences(MainActivity.PREFERENCE, MODE_PRIVATE);
- editor = myPrefs.edit();
- }
- //method that checks all users data inpouted in the fields and registration of account if
- //data is valid
- private void chkReg()
- {
- String userName = rgsUser.getText().toString();
- String passWord = rgsPass.getText().toString();
- String checkPass = chkPass.getText().toString();
- String chkUser = null;// will be used to check if the name is taken
- if(userName.trim().length()<=0 || passWord.trim().length()<=0 || checkPass.trim().length()<=0)
- {
- //if one of the fields is empty pop toast message
- Toast.makeText(this,"Error, please fill all the fields",Toast.LENGTH_LONG).show();
- }
- else if(!checkPass.equals(passWord))
- {
- //if confirm password doesnt equal to the password pop toast message
- Toast.makeText(this,"Error, please make sure the passwords match!",Toast.LENGTH_LONG).show();
- }
- else if(userName.equals(myPrefs.getString("user",chkUser)))
- {
- Toast.makeText(this,"Error, This username is already taken",Toast.LENGTH_LONG).show();
- }
- else
- {
- //pop message that data has been saved successfully!
- Toast.makeText(this,"Success! Account Registered!",Toast.LENGTH_LONG).show();
- //if everything works save the data using the following keys
- editor.putString("user", userName);
- editor.putString("password", passWord);
- //commit the file
- editor.commit();
- //move to the main page (login scrren)
- Intent toLog = new Intent(this, MainActivity.class);
- this.startActivity(toLog);
- }
- }
- //calls the chkReg method checking if all the fields and data are valid!
- public void btnRegister(View v)
- {
- chkReg();
- }
- //activates the canlce button
- public void btnCancle(View v)
- {
- Intent cnlReg = new Intent(this,MainActivity.class);
- this.startActivity(cnlReg);
- }
- }
- =============================================================================================================================
- =============================================xml activity_logged=============================================================
- <RelativeLayout
- android:layout_height="wrap_content"
- android:layout_width="match_parent"
- android:id="@+id/logPage"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/logMsg"
- android:layout_marginTop="25sp"
- android:textSize="50sp"
- />
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/btnOut"
- android:layout_below="@+id/logMsg"
- android:text="@string/btnLogOut"
- android:textSize="30sp"
- android:gravity="center"
- android:layout_marginTop="20sp"
- android:onClick="exitBtn"
- />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textAppearance="?android:attr/textAppearanceSmall"
- android:text="Created by Igor"
- android:id="@+id/whoLog"/>
- </RelativeLayout>
- ==============================================================================================================================
- ============================================Java Logged=======================================================================
- package com.example.igor.mylogin;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.TextView;
- /**
- * Created by Igor on 3/7/2016.
- */
- public class Logged extends Activity
- {
- SharedPreferences myPrefs; //calling SharedPrefrences Class
- SharedPreferences.Editor editor;//calling the editor for SharedPrefrences
- TextView userLogged; //what kind of user is logged in
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_logged);
- setPoints();
- userLogged.setText("Greetings " + getUserName());
- }
- //ASSISTANCE METHODS
- //sets all the points
- private void setPoints()
- {
- userLogged = (TextView)findViewById(R.id.logMsg);
- myPrefs = getApplicationContext().getSharedPreferences(MainActivity.PREFERENCE,MODE_PRIVATE);
- editor = myPrefs.edit();
- }
- //returns a String value of key user
- private String getUserName()
- {
- String userName = myPrefs.getString("user","offline");
- return userName;
- }
- public void exitBtn(View v)
- {
- editor.remove("logged");
- editor.commit();
- Intent exitPage = new Intent(this, MainActivity.class);
- this.startActivity(exitPage);
- }
- }
- ============================================================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement