Skip to content

Instantly share code, notes, and snippets.

@crazy-diya
Last active August 7, 2020 06:47
Show Gist options
  • Save crazy-diya/532562514ea9fb944b31e1d3f58b20af to your computer and use it in GitHub Desktop.
Save crazy-diya/532562514ea9fb944b31e1d3f58b20af to your computer and use it in GitHub Desktop.
Email And Password Requre,Store and validation using FirebaseAuth in Android Studio Java
package com.example.whatsappmyapp;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
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.FirebaseUser;
public class RegisterActivity extends AppCompatActivity {
private Button CreateAccountButton;
private EditText UserEmail, UserPassword;
private TextView AlreadyHaveAccount;
private FirebaseAuth mAuth;
private ProgressDialog loadingBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mAuth = FirebaseAuth.getInstance();
IntializeFields();
AlreadyHaveAccount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SendUserToLoginActivity();
}
});
CreateAccountButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CreateNewAccount();
}
});
}
private void CreateNewAccount() {
String email = UserEmail.getText().toString().trim();
String password = UserPassword.getText().toString().trim();
if (TextUtils.isEmpty(email)){
Toast.makeText(this, "Please Enter Email...", Toast.LENGTH_SHORT).show();
}
if (TextUtils.isEmpty(password)){
Toast.makeText(this, "Please Enter Password...", Toast.LENGTH_SHORT).show();
}else{
loadingBar.setTitle("Creating New Account");
loadingBar.setMessage("Please wait, while we ware creating new account for you...");
loadingBar.setCanceledOnTouchOutside(true);
loadingBar.show();
mAuth.createUserWithEmailAndPassword(email,password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
SendUserToLoginActivity();
Toast.makeText(RegisterActivity.this, "Account Created Successfully...", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
}
else{
String message = task.getException().toString();
Toast.makeText(RegisterActivity.this, "Error : "+message , Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
}
}
});
}
}
private void IntializeFields() {
CreateAccountButton = (Button) findViewById(R.id.register_button);
UserEmail = (EditText) findViewById(R.id.register_email);
UserPassword = (EditText) findViewById(R.id.register_password);
AlreadyHaveAccount = (TextView) findViewById(R.id.already_have_account_link);
loadingBar = new ProgressDialog(this);
}
private void SendUserToLoginActivity() {
Intent LoginIntent = new Intent(RegisterActivity.this,LoginActivity.class);
startActivity(LoginIntent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment