Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.com.myshared;
- 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
- {
- EditText txtUser;
- EditText txtPass;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- txtUser=(EditText)findViewById(R.id.user);
- txtPass=(EditText)findViewById(R.id.pass);
- if (checkUser())
- {
- //if key found , start the second activity.
- Intent iEntry = new Intent(this,Entry.class);
- this.startActivity(iEntry);
- }
- }
- private boolean checkUser()
- {
- //declaration of shared preferences
- SharedPreferences pref=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
- //getting value of key "user"
- String userName = pref.getString("userLogged","error");
- //return false if key "user" not exists
- return (!userName.equals("error"));
- }
- public void btnLogin(View v)
- {
- SharedPreferences pref=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
- String userName=txtUser.getText().toString();
- String password=txtPass.getText().toString();
- String userDB=pref.getString("userReg", "ErrorU");
- String passDB=pref.getString("passReg","ErrorP");
- if(userName.equals("") && password.equals("")) {
- Toast.makeText(this, "Please type username and password", Toast.LENGTH_LONG).show();
- return;
- }
- else if(userDB.equals("ErrorU") && passDB.equals("ErrorP")) {
- Toast.makeText(this, "Please register", Toast.LENGTH_LONG).show();
- return;
- }
- else if(userName.equals(userDB) && password.equals(passDB)) {
- Toast.makeText(this, "Good to see you", Toast.LENGTH_SHORT).show();
- }
- else
- {
- Toast.makeText(this, "Wrong user name or password", Toast.LENGTH_LONG).show();
- return;
- }
- //declartion of editor to edit shared preferances
- SharedPreferences.Editor editor=pref.edit();
- //insert into string value from EditText
- //create key "user" with value of userName, if key exists, it will be overwritten
- editor.putString("userLogged",userName);
- editor.putString("passLogged",password);
- //commit the changes
- editor.commit();
- Intent iEntry = new Intent(this,Entry.class);
- this.startActivity(iEntry);
- }
- public void btnReg(View v)
- {
- Intent regIntent = new Intent(this, Register.class);
- this.startActivity(regIntent);
- }
- }
- //====================================REGISTER CLASS
- package com.example.com.myshared;
- 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 txtUser, txtPass1, txtpass2;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.register);
- setPointer();
- }
- public void setPointer()
- {
- txtUser=(EditText)findViewById(R.id.user);
- txtPass1=(EditText)findViewById(R.id.pass1);
- txtpass2=(EditText)findViewById(R.id.pass2);
- }
- public void btnRegister(View v)
- {
- SharedPreferences pref=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
- //declartion of editor to edit shared preferances
- SharedPreferences.Editor editor=pref.edit();
- //insert into string value from EditText
- String userName=txtUser.getText().toString();
- String password1=txtPass1.getText().toString();
- String password2=txtpass2.getText().toString();
- String userDB=pref.getString("userReg", "ErrorU");
- if(userName.equals("")||password1.equals("")){
- Toast.makeText(this, "Please type username and password", Toast.LENGTH_LONG).show();
- return;
- }
- else if(userName.equals(userDB)) {
- Toast.makeText(this, "The username exist, please type another username", Toast.LENGTH_LONG).show();
- return;
- }
- else if(!password1.equals(password2)){
- Toast.makeText(this, "The password must be equls", Toast.LENGTH_LONG).show();
- return;
- }
- //create key "user" with value of userName, if key exists, it will be overwritten
- editor.putString("userReg", userName);
- editor.putString("passReg", password1);
- //commit the changes
- editor.commit();
- Intent iLogin = new Intent(this, MainActivity.class);
- this.startActivity(iLogin);
- }
- public void btnCancel(View v)
- {
- Intent iLogin = new Intent(this, MainActivity.class);
- this.startActivity(iLogin);
- }
- }
- //===============================ENTRY CLASS
- package com.example.com.myshared;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.TextView;
- public class Entry extends Activity {
- TextView txt;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.entry);
- txt=(TextView)findViewById(R.id.text);
- txt.setText("Hello " + getUserName());
- }
- private String getUserName()
- {
- //declaration of shared preferences
- SharedPreferences pref=getApplicationContext().getSharedPreferences("myPref", MODE_PRIVATE);
- //get string from key user, insert Error, if key not found
- String userName = pref.getString("userLogged","error");
- return userName;
- }
- public void btnLogout(View v)
- {
- //declaration of shared preferences
- SharedPreferences pref=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
- //declaration of editor
- SharedPreferences.Editor editor=pref.edit();
- //remove key user and pass
- editor.remove("userLogged");
- editor.remove("passLogged");
- editor.remove("userReg");
- editor.remove("passReg");
- //commit changes
- editor.commit();
- //start another window - main window
- Intent iMain = new Intent(this,MainActivity.class);
- this.startActivity(iMain);
- }
- }
Add Comment
Please, Sign In to add comment