0% found this document useful (0 votes)
50 views3 pages

Account Creation in Android App

Uploaded by

api-635142331
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views3 pages

Account Creation in Android App

Uploaded by

api-635142331
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd

package com.example.

project02;

import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.project02.databinding.ActivityCreateAccountBinding;
import com.example.project02.db.AppDatabase;
import com.example.project02.db.UserDAO;
import com.example.project02.db.Util;

public class CreateAccountActivity extends AppCompatActivity {

UserDAO mUserDAO;
EditText mNewUsername;
EditText mNewPassword;
Button mSubmitCreateAccount;
Button mNewAccountBackButton;

ActivityCreateAccountBinding mActivityCreateAccountBinding;

public static Intent intentFactory(Context packageContext){


Intent intent = new Intent(packageContext, CreateAccountActivity.class);
return intent;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_account);

mActivityCreateAccountBinding = ActivityCreateAccountBinding.inflate(getLayoutInflater());
setContentView(mActivityCreateAccountBinding.getRoot());

mSubmitCreateAccount = mActivityCreateAccountBinding.submitCreateAccount;
mNewAccountBackButton = mActivityCreateAccountBinding.newAccountBackButton;
mNewUsername = mActivityCreateAccountBinding.newUsername;
mNewPassword = mActivityCreateAccountBinding.newPassword;
mNewAccountBackButton = mActivityCreateAccountBinding.newAccountBackButton;

getDatabase();
mSubmitCreateAccount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
User newUser = getUserFromDisplay();
boolean createCheck = checkExistingUsers(newUser);

if (newUser.getUsername().equals("")) {
Util.toastMaker(getApplicationContext(),"No Username entered");
}
else if (createCheck) {
//alert dialog success
Util.toastMaker(getApplicationContext(), "Account successfully created");
Intent intent = MainActivity.intentFactory(getApplicationContext());
startActivity(intent);
} else {
//alert dialog fail
Util.toastMaker(getApplicationContext(), "Username already taken");
}
}
});

mNewAccountBackButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = MainActivity.intentFactory(getApplicationContext());
startActivity(intent);
}
});
}

private boolean checkExistingUsers(User newUser) {


User existingUser = mUserDAO.getUserByUsername(newUser.getUsername());
if (existingUser != null) {
return false;
}
if (existingUser == null) {
mUserDAO.insert(newUser);
}
return true;
}

private User getUserFromDisplay() {


String username = "No User found";
String password = "No Password found";

username = mNewUsername.getText().toString();
password = mNewPassword.getText().toString();

User user = new User(username, password);


return user;

private void getDatabase() {


mUserDAO = Room.databaseBuilder(this, AppDatabase.class, AppDatabase.DB_NAME)
.allowMainThreadQueries()
.build()
.getUserDAO();
}
}

You might also like