Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.78 KB | None | 0 0
  1. package com.yardbird.justice.yardbird;
  2.  
  3.  
  4. import android.app.AlertDialog;
  5. import android.content.DialogInterface;
  6. import android.content.Intent;
  7. import android.content.SharedPreferences;
  8. import android.os.Bundle;
  9. import android.support.v7.app.AppCompatActivity;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.EditText;
  13. import android.widget.ImageButton;
  14. import android.widget.Toast;
  15.  
  16. import com.kosalgeek.genasync12.AsyncResponse;
  17. import com.kosalgeek.genasync12.PostResponseAsyncTask;
  18.  
  19. import java.util.HashMap;
  20.  
  21. public class LoginActivity extends AppCompatActivity {
  22. public static final String PREFS = "prefFile";
  23.  
  24. EditText UsernameEt, PasswordEt;
  25.  
  26. @Override
  27. protected void onCreate(Bundle savedInstanceState) {
  28. super.onCreate(savedInstanceState);
  29. setContentView(R.layout.activity_login);
  30. onButtonClick();
  31. UsernameEt = (EditText) findViewById(R.id.edUsername);
  32. PasswordEt = (EditText) findViewById(R.id.edPassword);
  33. }
  34.  
  35.  
  36. @Override
  37. public void onBackPressed() {
  38. //super.onBackPressed();
  39. // finish();
  40.  
  41. Intent a = new Intent(Intent.ACTION_MAIN);
  42. a.addCategory(Intent.CATEGORY_HOME);
  43. a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  44. startActivity(a);
  45. }
  46.  
  47. public void onLogin(View view) {
  48. final String username = UsernameEt.getText().toString();
  49. String password = PasswordEt.getText().toString();
  50.  
  51. HashMap postData = new HashMap();
  52. postData.put("txtUsername", username);
  53. postData.put("txtPassword", password);
  54.  
  55. PostResponseAsyncTask task = new PostResponseAsyncTask(LoginActivity.this, postData, new AsyncResponse() {
  56. @Override
  57. public void processFinish(String s) {
  58. if (s.contains("success")) {
  59. //if the username and password match the corresponding contents, then the user
  60. //navigates to the appropriate page
  61.  
  62. SharedPreferences preferences = getSharedPreferences(PREFS, 0);
  63. SharedPreferences.Editor editor = preferences.edit();
  64. editor.putString("username", username);
  65. editor.commit();
  66.  
  67. Toast.makeText(LoginActivity.this, "Successful login", Toast.LENGTH_LONG).show();
  68. Intent intent = new Intent(LoginActivity.this, MainActivity.class);
  69. startActivity(intent);
  70. } else {
  71. //if the username and/or password is invalid, a dialog box appears informs them of possible errors
  72. AlertDialog.Builder dialogBox = new AlertDialog.Builder(LoginActivity.this);
  73. dialogBox.setMessage("These reasons cause errors..."+"\n1. Incorrect username and/or password"+"\n2. Username and/or password fields are empty")
  74. .setCancelable(false)
  75. .setNegativeButton("Okay", new DialogInterface.OnClickListener() {
  76. public void onClick(DialogInterface dialog, int which) {
  77. dialog.cancel();//closes the dialog box
  78. }
  79. });
  80.  
  81. AlertDialog dialog = dialogBox.create();
  82. dialog.setTitle("An Error was Detected!");
  83. dialog.show();
  84. }
  85. }
  86. });
  87. task.execute("http://collectoy.000webhostapp.com/phpfiles/login.php");
  88. }
  89.  
  90. public void onButtonClick() {
  91. Button signUp = (Button) findViewById(R.id.btnSignUp);
  92. Button FMP = (Button) findViewById(R.id.forgotPasswordButton);
  93. ImageButton usernameHelpButton = (ImageButton) findViewById(R.id.imageButton1);
  94. ImageButton passwordHelpButton = (ImageButton) findViewById(R.id.imageButton2);
  95.  
  96. /** If the help button next to the username textfield is pressed, a dialog box appears and
  97. * informs the user to enter their username to gain access to Yardbird's menu
  98. */
  99. usernameHelpButton.setOnClickListener (new View.OnClickListener() {
  100. @Override
  101. public void onClick(View v) {
  102. AlertDialog.Builder dialogBox = new AlertDialog.Builder(LoginActivity.this);
  103. dialogBox.setMessage("Please enter your username")
  104. .setCancelable(false)
  105. .setNegativeButton("Okay", new DialogInterface.OnClickListener() {
  106. public void onClick(DialogInterface dialog, int which) {
  107. dialog.cancel();//closes the dialog box
  108. }
  109. });
  110.  
  111. AlertDialog dialog = dialogBox.create();
  112. dialog.setTitle("Help?");
  113. dialog.show();
  114. }
  115. }
  116. );
  117.  
  118. /** If the help button next to the password textfield is pressed, a dialog box appears and
  119. * informs the user to enter their password to gain access to Yardbird's menu
  120. */
  121. passwordHelpButton.setOnClickListener (new View.OnClickListener() {
  122. @Override
  123. public void onClick(View v) {
  124. AlertDialog.Builder dialogBox = new AlertDialog.Builder(LoginActivity.this);
  125. dialogBox.setMessage("Please enter your password")
  126. .setCancelable(false)
  127. .setNegativeButton("Okay", new DialogInterface.OnClickListener() {
  128. public void onClick(DialogInterface dialog, int which) {
  129. dialog.cancel();//closes the dialog box
  130. }
  131. });
  132.  
  133. AlertDialog dialog = dialogBox.create();
  134. dialog.setTitle("Help?");
  135. dialog.show();
  136. }
  137. }
  138. );
  139.  
  140. //By pressing the Sign-Up button, the Sign-up page appears
  141. signUp.setOnClickListener (new View.OnClickListener() {
  142. @Override
  143. public void onClick(View v) {
  144. Intent i = new Intent(getApplicationContext(), SignUpActivity.class);
  145. startActivity(i);
  146. }
  147. });
  148.  
  149. //By pressing the forgot my password button, the forgot my password page opens
  150. FMP.setOnClickListener(new View.OnClickListener() {
  151. @Override
  152. public void onClick(View v) {
  153. Intent i = new Intent(getApplicationContext(), ForgotPassActivity.class);
  154. startActivity(i);
  155. }
  156. });
  157. }
  158.  
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement