I have created a login-register system by using Firebase auth. Now I want to show the registration information (name, email, phone) in my app. I want to reflect the registration information to text view but I couldn't find the way to do that.
Registration class: public class RegisterActivity extends AppCompatActivity {
EditText mName, mEmail, mPassword, mPhone;
Button mRegisterButton;
TextView mLoginButton;
FirebaseAuth firebaseAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mName = findViewById(R.id.userName);
mEmail = findViewById(R.id.userMail);
mPassword = findViewById(R.id.userPassword);
mPhone = findViewById(R.id.userPhone);
mRegisterButton = findViewById(R.id.register_button);
mLoginButton = findViewById(R.id.goToLogin);
firebaseAuth = FirebaseAuth.getInstance();
if(firebaseAuth.getCurrentUser() != null){
startActivity(new Intent(getApplicationContext(),MainActivity.class));
finish();
}
mRegisterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = mEmail.getText().toString().trim();
String password = mPassword.getText().toString().trim();
if(TextUtils.isEmpty(email))
{
mEmail.setError("Email is required");
return;
}
if(TextUtils.isEmpty(password))
{
mPassword.setError("Password is required");
return;
}
if(password.length() < 0)
{
mPassword.setError("Password Must Be >= 6 Characters");
return;
}
firebaseAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
Toast.makeText(RegisterActivity.this, "User Created.", Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(),MainActivity.class));
Log.d(TAG, "createUserWithEmail:success");
FirebaseUser user = firebaseAuth.getCurrentUser();
}else{
Log.w(TAG, "createUserWithEmail:failure", task.getException());
Toast.makeText(RegisterActivity.this, "Error!" task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
});
mLoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(getApplicationContext(),LoginActivity.class));
}
});
}
}
CodePudding user response:
Kotlin code below;
fun onCreate() {
val curreentUser = FirebaseAuth.getInstance().currentUser
val name = currentUser?.displayName
val email = currentUser?.email
val phone = currentUser?.phoneNumber
val textView = findViewById(R.id.your_text_view_id)
textView.text = "$name, $email, $phone"
}
CodePudding user response:
Assuming that you have a TextView added in your layout, especially added to display the data, then right after the following line of code:
FirebaseUser user = firebaseAuth.getCurrentUser();
Add these lines:
String name = user.getDisplayName();
String email = user.getEmail();
TextView textView = findViewById(R.id.textViewId);
String text = name "/" email;
textView.setText(text);
Please also note that when you are using email and password authentication the phone number doesn't exist. So if you try to use it, you'll get null.