Sahil Sahil -4 years ago 72
Android Question

Firebase User registeration

I am fairly new at android app development, I am trying to make an activity from where users can sign up into the database, this is the code

public class RegisterActivity extends AppCompatActivity {

TextInputLayout mEmail, mPassword;
Button mRegisterButton;
FirebaseAuth mAuth;
Button loginAccountButton;
ProgressDialog progressDialogue;

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

mPassword = (TextInputLayout) findViewById(R.id.password);
mEmail = (TextInputLayout) findViewById(R.id.email);
loginAccountButton = (Button) findViewById(R.id.loginalready);
mRegisterButton = (Button) findViewById(R.id.registerButton);
progressDialogue = new ProgressDialog(RegisterActivity.this);

mAuth = FirebaseAuth.getInstance();

loginAccountButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(RegisterActivity.this,login.class));
}
});

mRegisterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
registeruser();
}

private void registeruser() {
String email = mEmail.getEditText().getText().toString().trim();
String password = mPassword.getEditText().getText().toString().trim();
progressDialogue.setMessage("Please wait");
progressDialogue.show();
mAuth.createUserWithEmailAndPassword(email,password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
progressDialogue.dismiss();
Toast.makeText(RegisterActivity.this,"You're now registered",Toast.LENGTH_LONG).show();
startActivity(new Intent(RegisterActivity.this,MainActivity.class));
finish();
}
else{
Toast.makeText(RegisterActivity.this,"Something went horribly wrong",Toast.LENGTH_LONG).show();
}
}
});

}
});



}}


All the classes are imported properly and there seems to be no way around it, the code was working a few hours ago but now it just won't budge every time I try and run the code it gives me the Toast something went horribly wrong

this is the activity_register.xml

<android.support.design.widget.TextInputLayout
android:layout_width="368dp"
android:layout_height="wrap_content"
android:id="@+id/email"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
tools:layout_editor_absoluteY="8dp">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="EMAIL" />
</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
android:layout_width="368dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/email"
android:id="@+id/password"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="PASSWORD" />
</android.support.design.widget.TextInputLayout>

<Button
android:id="@+id/registerButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:text="REGISTER"
app:layout_constraintTop_toBottomOf="@+id/password"
tools:layout_editor_absoluteX="280dp" />

<Button
android:id="@+id/loginalready"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:text="login"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:text="login?"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/password"
app:layout_constraintVertical_bias="0.051" />

Answer Source

Give android:id="" to your EditText instead of the TextInputLayout as follows

<android.support.design.widget.TextInputLayout
    android:layout_width="368dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    app:layout_constraintTop_toBottomOf="@+id/displayName"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/email"
        android:hint="EMAIL" />
</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
    android:layout_width="368dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    app:layout_constraintTop_toBottomOf="@+id/email"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/password"
        android:hint="PASSWORD" />
</android.support.design.widget.TextInputLayout>

In Your Java File create Objects Of EdiText instead of TextInputLayout and get values as follows.

EdiText mEmail, mPassword;

    mPassword = (EdiText) findViewById(R.id.password);
    mEmail = (EdiText) findViewById(R.id.email);

Values Are entered by user in EditText not in TextInputLayout so You have to get values from the EditText

I hope its work for You! Thanks

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download