Advertisement
Guest User

Untitled

a guest
Jun 5th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. package com.example.demirkorac.labexam1;
  2.  
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.SharedPreferences;
  6. import android.os.Bundle;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.view.View;
  9. import android.widget.EditText;
  10. import android.widget.Toast;
  11.  
  12. public class MainActivity extends AppCompatActivity {
  13.  
  14. private EditText newUsername, newPassword, etLocation,yearOfBirth;
  15. SharedPreferences preferences;
  16. private Intent intent;
  17.  
  18.  
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23.  
  24. newUsername = findViewById(R.id.newUsername);
  25. newPassword = findViewById(R.id.newPassword);
  26. etLocation = findViewById(R.id.etLocation);
  27. yearOfBirth=findViewById(R.id.yearOfBirth);
  28.  
  29. preferences = this.getSharedPreferences("mypreferences", Context.MODE_PRIVATE);
  30. }
  31.  
  32. public void register(View v) {
  33. if(validateUser(newUsername.getText().toString(), newPassword.getText().toString(), etLocation.getText().toString(),yearOfBirth.getText().toString())) {
  34. preferences.edit().putString("username", newUsername.getText().toString()).apply();
  35. preferences.edit().putString("password", newPassword.getText().toString()).apply();
  36. preferences.edit().putString("location", etLocation.getText().toString()).apply();
  37. preferences.edit().putString("yearOfBirth", yearOfBirth.getText().toString()).apply();
  38.  
  39. Toast.makeText(this, "Successfully registered!", Toast.LENGTH_SHORT).show();
  40. intent = new Intent(this, LoginActivity.class);
  41. startActivity(intent);
  42. }
  43. }
  44.  
  45. public boolean validateUser(String username, String password, String location, String yearOfBirth) {
  46. if(username.equals("") || password.equals("") || location.equals("")) {
  47. Toast.makeText(this, "No empty fields are allowed", Toast.LENGTH_SHORT).show();
  48. } else if(username.length() > 4){
  49. Toast.makeText(this, "The username should not exceed 4 characters", Toast.LENGTH_SHORT).show();
  50. } else if(password.length() < 3) {
  51. Toast.makeText(this, "The password length should be minimum 3 characters", Toast.LENGTH_SHORT).show();
  52. } else if(yearOfBirth.length() !=4) {
  53. Toast.makeText(this, "The year of birth should be 4 digits long", Toast.LENGTH_SHORT).show();
  54. }else {
  55. return true;
  56. }
  57.  
  58. return false;
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement