Advertisement
Guest User

Login

a guest
Mar 14th, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1. package ie.app.barbershop;
  2.  
  3. import android.content.SharedPreferences;
  4. import android.os.Bundle;
  5. import android.support.design.widget.FloatingActionButton;
  6. import android.support.design.widget.Snackbar;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.support.v7.widget.Toolbar;
  9. import android.view.View;
  10. import android.view.Menu;
  11. import android.view.MenuItem;
  12. import android.widget.Button;
  13. import android.util.Log;
  14. import android.content.Intent;
  15. import android.widget.TextView;
  16. import android.widget.Toast;
  17. import android.view.View.OnClickListener;
  18.  
  19.  
  20. public class MainActivity extends AppCompatActivity {
  21.  
  22.  
  23. private Button loginButton;
  24. private Button registerNowButton;
  25. private SharedPreferences settings;
  26. private boolean mIsbackButtonPressed;
  27.  
  28.  
  29.  
  30.  
  31.  
  32. public void onCreate(Bundle savedInstanceState) {
  33. super.onCreate(savedInstanceState);
  34.  
  35. settings = getSharedPreferences("login", 0);
  36. if (settings.getBoolean("loggedIn", false))
  37. startHomeScreen();
  38.  
  39. setContentView(R.layout.activity_main);
  40.  
  41. }
  42.  
  43. public void register(View v) {startActivity(new Intent(this, Register.class));
  44.  
  45. private void startHomeScreen() {
  46.  
  47. Intent intent = new Intent(MainActivity.this, Landing.class);
  48. MainActivity.this.startActivity(intent);
  49.  
  50.  
  51. public void login(View v) {
  52.  
  53. CharSequence username = ((TextView) findViewById(R.id.userName))
  54. .getText();
  55.  
  56. CharSequence password = ((TextView) findViewById(R.id.passWord))
  57. .getText();
  58.  
  59. String validUsername = settings.getString("username","");
  60. String validPassword = settings.getString("password","");
  61.  
  62. if (username.length() <= 0 || password.length() <= 0)
  63. Toast.makeText(this, "You must enter a valid email & password",
  64. Toast.LENGTH_SHORT).show();
  65. else if (!username.toString().matches(validUsername)
  66. || !password.toString().matches(validPassword))
  67. Toast.makeText(this, "Unable to validate your email & password",
  68. Toast.LENGTH_SHORT).show();
  69. else if (!mIsBackButtonPressed) {
  70.  
  71. SharedPreferences.Editor editor = settings.edit();
  72. editor.putBoolean("loggedin", true);
  73. editor.commit();
  74.  
  75. startHomeScreen();
  76. this.finish();
  77.  
  78.  
  79.  
  80.  
  81. }
  82. }
  83.  
  84. //When a user clicks the login button they are brought to the landing page
  85. Button btn1 = (Button) findViewById(R.id.loginButton);
  86.  
  87. btn1.setOnClickListener(new View.OnClickListener() {
  88. @Override
  89. public void onClick(View v) {
  90. startActivity(new Intent(MainActivity.this, Landing.class));
  91. }
  92. });
  93.  
  94.  
  95. //When a user clicks the register button they are brought to the registration page
  96. Button btn = (Button) findViewById(R.id.registerNowButton);
  97.  
  98. btn.setOnClickListener(new View.OnClickListener() {
  99. @Override
  100. public void onClick(View v) {
  101. startActivity(new Intent(MainActivity.this, Register.class));
  102. }
  103.  
  104. });
  105.  
  106.  
  107. }
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117. //Action Bar
  118. @Override
  119. public boolean onCreateOptionsMenu(Menu menu) {
  120. // Inflate the menu; this adds items to the action bar if it is present.
  121. getMenuInflater().inflate(R.menu.menu_main, menu);
  122. return true;
  123. }
  124.  
  125.  
  126.  
  127.  
  128. //When user clicks contact us they are brought to the contact us page
  129. @Override
  130. public boolean onOptionsItemSelected(MenuItem item) {
  131. switch (item.getItemId())
  132. {
  133. case R.id.contactUs : startActivity (new Intent(this, ContactUs.class));
  134. break;
  135. }
  136. return super.onOptionsItemSelected(item);
  137. }
  138.  
  139.  
  140.  
  141. public boolean onOptionsItemsSelected(MenuItem item){
  142. int id = item.getItemId();
  143.  
  144.  
  145. if (id == R.id.action_settings) {
  146. return true;
  147. }
  148.  
  149. return super.onOptionsItemSelected(item);
  150. }
  151.  
  152. public void loginButtonPressed(View view)
  153. {
  154. Log.v("Login", "Logged In");
  155. }
  156.  
  157. public void registerButtonPressed(View view)
  158. {
  159. Log.v("Register","Registered Now");
  160. }
  161.  
  162.  
  163.  
  164.  
  165.  
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement