How to prevent duplicate entry in registration activity
<p>I would like it that when a user registers a username in my app if that username is already taken that he/she will get a toast saying "Username is already taken".</p>

<p>I already included the UNIQUE constraint in my Database and it works fine but in the activity itself I would like to let the user know that the Username they are trying to register already exist.</p>

<pre><code>    package com.fullfrontalgames.numberfighter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Register extends Activity {
    EditText UsernameReg, PasswordReg, PasswordConfirm, EmailReg;

    Button Register;

    DBAdapter db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);

        // get Instance of Database Adapter
        final DBAdapter db = new DBAdapter(this);
        db.open();

        // Get reference of views.
        UsernameReg = (EditText)findViewById(R.id.UsernameReg);
        PasswordReg = (EditText)findViewById(R.id.PasswordReg);
        EmailReg = (EditText)findViewById(R.id.EmailReg);
        PasswordConfirm = (EditText)findViewById(R.id.PasswordConfirm);

        Register = (Button)findViewById(R.id.Register);
        Register.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String Username = UsernameReg.getText().toString();
                String Password = PasswordReg.getText().toString();
                String PassConfirm = PasswordConfirm.getText().toString();
                String Email = EmailReg.getText().toString();
                String TakenUsername = db.getData();

                // Check if any of the field are vacant.
                if (Username.equals("") || Password.equals("") || PassConfirm.equals("")) {
                    Toast.makeText(getApplicationContext(), "Field Vacant", Toast.LENGTH_LONG)
                            .show();
                    return;
                }
                // Check if both passwords matches
                if (!Password.equals(PassConfirm)) {
                    Toast.makeText(getApplicationContext(), "Password does not match",
                            Toast.LENGTH_LONG).show();
                    return;
                }
                if (Username.equals(TakenUsername))

                {
                    Toast.makeText(getApplicationContext(), "Username Already Taken",
                            Toast.LENGTH_LONG).show();
                    return;
                } else {
                    // Save data in database
                    db.insertPlayer(Username, Password, Email);
                    Toast.makeText(getApplicationContext(), "Account Successfully Created ",
                            Toast.LENGTH_LONG).show();
                    startActivity(new Intent("com.fullfrontalgames.numberfighter.Login"));
                }
            }

        });

    }

}
</code></pre>
