3
votes

Aim

Allow users to register their preferred display name, home address, email, and password by utilizing Firebase Authentication (Email and Password)

Java Class

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseAuthException;

public class RegistrationActivity extends AppCompatActivity {

    private TextInputLayout jRegisterName;
    private TextInputLayout jRegisterAddress;
    private TextInputLayout jRegisterEmail;
    private TextInputLayout jRegisterPassword;
    private Button jRegisterRegBtn;

    //Firebase
    private FirebaseAuth mAuth;


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

        //Firebase Auth
        mAuth = FirebaseAuth.getInstance();

        jRegisterName = (TextInputLayout) findViewById(R.id.registerName);
        jRegisterAddress = (TextInputLayout) findViewById(R.id.registerAddress);
        jRegisterEmail = (TextInputLayout) findViewById(R.id.registerEmail);
        jRegisterPassword = (TextInputLayout) findViewById(R.id.registerPassword);
        jRegisterRegBtn = (Button) findViewById(R.id.registerRegBtn);

        jRegisterRegBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String userDisplayName = jRegisterName.getEditText().toString();
                String userHomeAddress = jRegisterAddress.getEditText().toString();
                String userEmail = jRegisterEmail.getEditText().toString().trim();
                String userPassword = jRegisterPassword.getEditText().toString().trim();

                registerUser(userDisplayName, userHomeAddress, userEmail, userPassword);

            }

            private void registerUser(String userDisplayName, String userHomeAddress, String userEmail, String userPassword) {
                mAuth.createUserWithEmailAndPassword(userEmail, userPassword).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                if (task.isSuccessful()) {
                                    Intent intentMain = new Intent(RegistrationActivity.this, MainActivity.class);
                                    startActivity(intentMain);
                                    finish();
                                }else if(!task.isSuccessful()){
                                    FirebaseAuthException e = (FirebaseAuthException )task.getException();
                                    Toast.makeText(RegistrationActivity.this, "Failed Registration: "+e.getMessage(), Toast.LENGTH_SHORT).show();
                                    return;
                                }
                            }
                        });
            }
        });

    }
}

Problem

After the user has entered the input such as

Name : JohnSmith

HomeAddress : 18 King Street

Email : [email protected]

Password : 123456789pass

The user fails to register and the message below prompts out

"Failed Registration: The email address is badly formatted"

3
@YCF_L I mean the message that prompts out after I register is "Failed Registration: The email address is badly formatted". I'll edit my questionThe Employee 123
An error prompts out Incompatible Types as the in the XML file, I am using TextInputLayout. I can only use "getEditText()". Should I post the XML codes?The Employee 123

3 Answers

4
votes

Check your inputType on the xml file. There is 3 types of inputs on email.

android:inputType="textWebEmailAddress"
android:inputType="textEmailAddress"
android:inputType="textEmailSubject"

More here https://developer.android.com/reference/android/text/InputType.html

2
votes

Achieve by doing this simply

  • you need to dosetInputType

android:inputType="textEmailAddress"

  • You can done with programmatic also

emailEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS

Happy to help you


0
votes

Email is wrongly formatted

Try to add input type as textEmailAddress in the layout file

android:inputType="textEmailAddress"