Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- =====Main
- package com.example.shiv2.mytest;
- 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
- {
- SharedPreferences myPref;
- SharedPreferences.Editor editor;
- private EditText userName;
- private EditText passWord;
- private String isOnline;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- setPoints();
- if(chkOnline())
- {
- Intent userLogged = new Intent(this,Logged.class);
- userLogged.putExtra("userOnline",isOnline);
- this.startActivity(userLogged);
- }
- }
- private void setPoints()
- {
- myPref = getApplicationContext().getSharedPreferences(Register.myPrefs,MODE_PRIVATE);
- editor =myPref.edit();
- userName = (EditText)findViewById(R.id.inUser);
- passWord = (EditText)findViewById(R.id.inPass);
- }
- private boolean chkOnline()
- {
- isOnline = myPref.getString("userOnline","offline");
- return !isOnline.equals("offline");
- }
- private void chkUser()
- {
- String userInput = userName.getText().toString();
- String passInput = passWord.getText().toString();
- if(userInput.trim().equals("") || passInput.trim().equals(""))
- {
- Toast.makeText(this,"Error, please fill all the fields",Toast.LENGTH_SHORT).show();
- }
- else if(!myPref.contains(userInput) || !passInput.equals(myPref.getString(userInput,passInput)))
- {
- Toast.makeText(this,"Error, wrong username or password!",Toast.LENGTH_SHORT).show();
- }
- else
- {
- Toast.makeText(this,"Success, logged in successfully!",Toast.LENGTH_SHORT).show();
- editor.putString("userOnline",userInput);
- editor.commit();
- Intent logMe = new Intent(this,Logged.class);
- this.startActivity(logMe);
- }
- }
- public void btnLog(View v)
- {
- chkUser();
- }
- public void btnReg(View v)
- {
- Intent regInt = new Intent(this,Register.class);
- this.startActivity(regInt);
- }
- }
- ======
- ====register
- package com.example.shiv2.mytest;
- 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 app0811 on 16/03/2016.
- */
- public class Register extends Activity
- {
- public final static String myPrefs = "myPref";//shared prefrences xml file name
- SharedPreferences myPref;
- SharedPreferences.Editor editor;
- private EditText userInput;
- private EditText passInput;
- private EditText rePassInput;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_register);
- setPoints();
- }
- private void setPoints()
- {
- myPref = getApplicationContext().getSharedPreferences(myPrefs,MODE_PRIVATE);
- editor =myPref.edit();
- userInput = (EditText)findViewById(R.id.setUser);
- passInput = (EditText)findViewById(R.id.setPass);
- rePassInput = (EditText)findViewById(R.id.confPass);
- }
- private void chkReg()
- {
- String userName = userInput.getText().toString();
- String passWord = passInput.getText().toString();
- String checkPass = rePassInput.getText().toString();
- if(userName.trim().length()<=0 || passWord.trim().length()<=0 || checkPass.trim().length()<=0)
- {
- Toast.makeText(this, "Error, please fill all the fields", Toast.LENGTH_SHORT).show();
- }
- else if(!checkPass.equals(passWord))
- {
- Toast.makeText(this, "Error, Password do not match!", Toast.LENGTH_SHORT).show();
- }
- else if(myPref.contains(userName))
- {
- Toast.makeText(this, "Error, this user allready exists", Toast.LENGTH_SHORT).show();
- }
- else
- {
- Toast.makeText(this, "Success, registered succesfully", Toast.LENGTH_SHORT).show();
- editor.putString(userName, passWord);
- editor.commit();
- Intent mainInt = new Intent(this,MainActivity.class);
- this.startActivity(mainInt);
- finish();
- }
- }
- public void btnReg(View v)
- {
- chkReg();
- }
- public void btnCncle(View v)
- {
- Intent cnclInten = new Intent (this,MainActivity.class);
- this.startActivity(cnclInten);
- finish();
- }
- }
- =========
- ====logged
- package com.example.shiv2.mytest;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.TextView;
- /**
- * Created by app0811 on 16/03/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("Hello " + getUserName());
- }
- private void setPoints()
- {
- userLogged = (TextView)findViewById(R.id.logMsg);
- myPrefs = getApplicationContext().getSharedPreferences(Register.myPrefs,MODE_PRIVATE);
- editor = myPrefs.edit();
- }
- private String getUserName()
- {
- String username = myPrefs.getString("userOnline","offline");
- return username;
- }
- public void btnLogOut(View v)
- {
- editor.remove("userOnline");
- editor.commit();
- Intent logOut = new Intent(this,MainActivity.class);
- this.startActivity(logOut);
- }
- }
- ======
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement