Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends AppCompatActivity implements View.OnClickListener{
- private Button buttonRegister;
- private EditText editTextEmail;
- private EditText editTextPassword;
- private TextView textViewSignin;
- private ProgressDialog progressDialog;
- private FirebaseAuth firebaseAuth;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- firebaseAuth = FirebaseAuth.getInstance();
- progressDialog = new ProgressDialog(this);
- if(firebaseAuth.getCurrentUser() != null){
- //Profile activity
- finish();
- startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
- }
- buttonRegister = (Button) findViewById(R.id.buttonRegister);
- editTextEmail = (EditText) findViewById(R.id.editTextEmail);
- editTextPassword = (EditText) findViewById(R.id.editTextPassword);
- textViewSignin = (TextView) findViewById((R.id.textViewSignin));
- buttonRegister.setOnClickListener(this);
- textViewSignin.setOnClickListener(this);
- }
- private void registerUser(){
- String email = editTextEmail.getText().toString().trim();
- String password = editTextPassword.getText().toString().trim();
- if(TextUtils.isEmpty(email)){
- //email is empty
- Toast.makeText(this, "Please enter email", Toast.LENGTH_LONG).show();
- //stopping the function execution further
- return;
- }
- if(TextUtils.isEmpty(password)){
- //email is empty
- Toast.makeText(this, "Please enter password", Toast.LENGTH_LONG).show();
- //stopping the function execution further
- return;
- }
- //if validations are Okay
- //Register User
- progressDialog.setMessage("Registering User...");
- progressDialog.show();
- firebaseAuth.createUserWithEmailAndPassword(email, password)
- .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
- @Override
- public void onComplete(@NonNull Task<AuthResult> task) {
- if(task.isSuccessful()){
- //user is successfully registered
- // Toast.makeText(MainActivity.this, "Registered Successfully", Toast.LENGTH_LONG).show();
- finish();
- startActivity((new Intent(getApplicationContext(), ProfileActivity.class)));
- }
- else{
- Toast.makeText(MainActivity.this, "Could not register, Please try again!", Toast.LENGTH_LONG).show();
- }
- progressDialog.dismiss();
- }
- });
- }
- @Override
- public void onClick(View view) {
- if(view == buttonRegister) {
- registerUser();
- }
- if (view == textViewSignin){
- //open login activity here
- startActivity(new Intent(this,LoginActivity.class));
- }
- }
- }
- public class ProfileActivity extends AppCompatActivity implements View.OnClickListener {
- private FirebaseAuth firebaseAuth;
- private DatabaseReference databaseReference;
- private TextView textViewUserEmail;
- private Button buttonLogout, saveUserInformationBtn;
- EditText userName, userAddress;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_profile);
- firebaseAuth = FirebaseAuth.getInstance();
- if (firebaseAuth.getCurrentUser() == null) {
- finish();
- startActivity(new Intent(this, LoginActivity.class));
- }
- databaseReference = FirebaseDatabase.getInstance().getReference();
- FirebaseUser user = firebaseAuth.getCurrentUser();
- textViewUserEmail = (TextView) findViewById(R.id.textViewUserEmail);
- saveUserInformationBtn = (Button) findViewById(R.id.userInfoSave);
- userName = (EditText) findViewById(R.id.userName);
- userAddress = (EditText) findViewById(R.id.userAddress);
- textViewUserEmail.setText("Welcome " + user.getEmail());
- buttonLogout = (Button) findViewById(R.id.buttonLogout);
- buttonLogout.setOnClickListener(this);
- saveUserInformationBtn.setOnClickListener(this);
- }
- private void saveUserInformation(){
- String name = userName.getText().toString().trim();
- String address = userAddress.getText().toString().trim();
- Userinformation userinformation = new Userinformation(name, address);
- FirebaseUser user = firebaseAuth.getCurrentUser();
- databaseReference.child(user.getUid()).setValue(userinformation);
- Toast.makeText(this, "Information Saved...", Toast.LENGTH_LONG). show();
- }
- @Override
- public void onClick(View view) {
- if(view == buttonLogout){
- firebaseAuth.signOut();
- finish();
- startActivity(new Intent(this,LoginActivity.class));
- }
- if (view == saveUserInformationBtn){
- saveUserInformation();
- }
- }
- }
Add Comment
Please, Sign In to add comment