How can I make Android registration have both email AND username be unique?

I am currently registering users on Android with an email, password and a username. I currently have the identifier set to the email. The issue is, when I register a new user, the user can register with a username that is already in use. If I change the identifier to the username, I get the same problem with the user being able to use an email that is already in use. How can I modify my code to make it so that if the user is trying to register with a username that is already in use, it will let the user know that the username is already taken? Here is all of my code for registration:
BaseActivity.java:

public class BaseActivity extends AppCompatActivity {
 

 private ProgressDialog mProgressDialog;
 

 public void showProgressDialog() {
 if (mProgressDialog == null) {
 mProgressDialog = new ProgressDialog(this);
 mProgressDialog.setMessage(getString(R.string.loading));
 mProgressDialog.setIndeterminate(true);
 mProgressDialog.setCancelable(false);
 }
 mProgressDialog.show();
 }
 
 public void hideProgressDialog() {
 if (mProgressDialog != null && mProgressDialog.isShowing()) {
 mProgressDialog.dismiss();
 }
 }
 
 @Override
 public void onDestroy() {
 super.onDestroy();
 hideProgressDialog();
 }
 
}

CreateAccountActivity.java:

public class CreateAccountActivity extends BaseActivity {
 
 private EditText inputUsername;
 private EditText inputEmail;
 private EditText inputPassword;
 
 private Button buttonCreateAccount;
 
 private TextWatcher textWatcher = new TextWatcher() {
 @Override
 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
 

 }
 
 @Override
 public void onTextChanged(CharSequence s, int start, int before, int count) {
 checkFields();

 }
 
 @Override
 public void afterTextChanged(Editable s) {
 
 }
 };
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_create_account);
 
 inputUsername = (EditText) findViewById(R.id.createAccountUsernameInput);
 inputEmail = (EditText) findViewById(R.id.createAccountEmailInput);
 inputPassword = (EditText) findViewById(R.id.createAccountPasswordInput);
 
 inputUsername.addTextChangedListener(textWatcher);
 inputEmail.addTextChangedListener(textWatcher);
 inputPassword.addTextChangedListener(textWatcher);
 
 buttonCreateAccount = (Button) findViewById(R.id.createAccountButton);
 
 TextView textViewLogin = (TextView) findViewById(R.id.loginLink);
 
 buttonCreateAccount.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
 createUser();
 }
 });
 
 assert textViewLogin != null;
 textViewLogin.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
 Intent intent = new Intent(CreateAccountActivity.this, LoginActivity.class);
 startActivity(intent);
 finish();
 }
 });
 }
 
 private void createUser() {
 showProgressDialog();
 
 BackendlessUser user = new BackendlessUser();
 user.setProperty(BackendSettings.USERNAME_KEY, inputUsername.getText().toString().trim());
 user.setEmail(inputEmail.getText().toString().trim());
 user.setPassword(inputPassword.getText().toString());
 
 Backendless.UserService.register(user, new AsyncCallback<BackendlessUser>() {
 @Override
 public void handleResponse(BackendlessUser backendlessUser) {
 hideProgressDialog();
 Toast.makeText(CreateAccountActivity.this, Defaults.REGISTER_SUCCESS_MESSAGE, Toast.LENGTH_SHORT).show();
 Intent intent = new Intent(CreateAccountActivity.this, MainActivity.class);
 startActivity(intent);
 finish();
 }
 
 @Override
 public void handleFault(BackendlessFault backendlessFault) {
 hideProgressDialog();
 String errorCode = backendlessFault.getCode();
 String errorMessage;
 switch (errorCode) {
 case "3040":
 errorMessage = BackendSettings.ERROR_3040;
 break;
 case "3033":
 errorMessage = BackendSettings.ERROR_3033;
 break;
 default:
 errorMessage = "An unknown error occurred. Try again.";
 }
 Toast.makeText(CreateAccountActivity.this, errorMessage, Toast.LENGTH_SHORT).show();
 }
 });
 }
 
 private void enableButton() {
 buttonCreateAccount.setEnabled(true);
 }
 
 private void disableButton() {
 buttonCreateAccount.setEnabled(false);
 }
 
 private void checkFields() {
 String username = inputUsername.getText().toString().trim();
 String email = inputEmail.getText().toString().trim();
 String password = inputPassword.getText().toString();
 
 if (username.isEmpty() || email.isEmpty() || password.isEmpty()) {
 disableButton();
 } else {
 enableButton();
 }
 }
 
}

Hi Alexiz,

This issue can be solved by using our Business Logic service.
You should add the event handler on registration and check there if a user provides a unique name.

Regards Ilya

Please excuse my ‘noobness’, but exactly how can I accomplish this. I read through your link and I think I sort of get it, but still not sure how to implement this.

I created a new EventHandler (Event: Register, Call Timing: before), I am not sure what I should put in the beforeRegister method though.

I think you should use Data Service API for checking if the User table already contains a row with the set username in the event handler.
I understand that it is a quite blurry description but due our Support Policy, we don’t provide some code snippets with ready-made decisions.
Regards, Ilya

Wow. So Backendless just wants money. I see. Well thanks, I will be using another service. Thanks again.

Hi, Alexiz.

I hope you understand the situation - we cannot afford ourselves to dig into every customer’s code(nor any other mbaas provider can). What we can do - is provide a reliable service and make our documentation clear and readable. And we do our best at achieving this.
Guess the best you can do is read Business Logic documentation as Ilya mentioned and see how you can use it for your purposes.