Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.demirkorac.labexam1;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- private EditText newUsername, newPassword, etLocation,yearOfBirth;
- SharedPreferences preferences;
- private Intent intent;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- newUsername = findViewById(R.id.newUsername);
- newPassword = findViewById(R.id.newPassword);
- etLocation = findViewById(R.id.etLocation);
- yearOfBirth=findViewById(R.id.yearOfBirth);
- preferences = this.getSharedPreferences("mypreferences", Context.MODE_PRIVATE);
- }
- public void register(View v) {
- if(validateUser(newUsername.getText().toString(), newPassword.getText().toString(), etLocation.getText().toString(),yearOfBirth.getText().toString())) {
- preferences.edit().putString("username", newUsername.getText().toString()).apply();
- preferences.edit().putString("password", newPassword.getText().toString()).apply();
- preferences.edit().putString("location", etLocation.getText().toString()).apply();
- preferences.edit().putString("yearOfBirth", yearOfBirth.getText().toString()).apply();
- Toast.makeText(this, "Successfully registered!", Toast.LENGTH_SHORT).show();
- intent = new Intent(this, LoginActivity.class);
- startActivity(intent);
- }
- }
- public boolean validateUser(String username, String password, String location, String yearOfBirth) {
- if(username.equals("") || password.equals("") || location.equals("")) {
- Toast.makeText(this, "No empty fields are allowed", Toast.LENGTH_SHORT).show();
- } else if(username.length() > 4){
- Toast.makeText(this, "The username should not exceed 4 characters", Toast.LENGTH_SHORT).show();
- } else if(password.length() < 3) {
- Toast.makeText(this, "The password length should be minimum 3 characters", Toast.LENGTH_SHORT).show();
- } else if(yearOfBirth.length() !=4) {
- Toast.makeText(this, "The year of birth should be 4 digits long", Toast.LENGTH_SHORT).show();
- }else {
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement