Java – Android – Firebase – Email address malformed

Android – Firebase – Email address malformed… here is a solution to the problem.

Android – Firebase – Email address malformed

Aim

Allows users to authenticate (email and password) > Firebase by using < em

Java classes

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;
                                }
                            }
                        });
            }
        });

}
}

Question

After the user input, as it is done

Name: John Smith

Home address: 18 King Street

Email: [email protected]

Pass: 123456789pass

If the user fails to register, the following message is displayed

“Registration failed: Email address format error”

Solution

Check the inputType in the xml file. There are 3 types of input for emails.

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

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

Related Problems and Solutions