Advertisement
Tommymadison

cant seem to make the empty fields work

Feb 8th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.05 KB | None | 0 0
  1. package com.example.tommy.ttuf;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.content.Intent;
  5. import android.support.annotation.NonNull;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.text.TextUtils;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.EditText;
  13. import android.widget.TextView;
  14. import android.widget.Toast;
  15.  
  16. import com.google.android.gms.tasks.OnCompleteListener;
  17. import com.google.android.gms.tasks.Task;
  18. import com.google.firebase.auth.AuthResult;
  19. import com.google.firebase.auth.FirebaseAuth;
  20. import com.google.firebase.auth.FirebaseUser;
  21. import com.google.firebase.auth.UserProfileChangeRequest;
  22.  
  23. public class SignUpActivity extends AppCompatActivity implements View.OnClickListener{
  24.     private FirebaseAuth mAuth;
  25.     private Button btnSignup;
  26.     private EditText etUsername,etEmail,etPassword;
  27.     private TextView tvSignin;
  28.  
  29.     private ProgressDialog progressDialog;
  30.  
  31.  
  32.     @Override
  33.     protected void onCreate(Bundle savedInstanceState) {
  34.         super.onCreate(savedInstanceState);
  35.         setContentView(R.layout.activity_sign_up);
  36.  
  37.  
  38.         mAuth = FirebaseAuth.getInstance();
  39.  
  40.         btnSignup=(Button)findViewById(R.id.btnSignup);
  41.         etEmail=(EditText)findViewById(R.id.etEmail);
  42.         etPassword=(EditText)findViewById(R.id.etPassword);
  43.         etUsername=(EditText)findViewById(R.id.etUsername);
  44.         tvSignin=(TextView) findViewById(R.id.tvSignin);
  45.  
  46.         progressDialog=new ProgressDialog(this);
  47.  
  48.  
  49.  
  50.         //if the user is logged in
  51.  
  52.         if (mAuth.getCurrentUser()!=null){
  53.             //if the user is not logged in
  54.             finish();
  55.             startActivity(new Intent(getApplicationContext(),SignInActivity.class));
  56.  
  57.         }
  58.  
  59.  
  60.  
  61.  
  62.  
  63.         btnSignup.setOnClickListener(new View.OnClickListener() {
  64.             @Override
  65.             public void onClick(View view) {
  66.  
  67.                 String getemail=etEmail.getText().toString().trim();
  68.                 String getpassword=etPassword.getText().toString().trim();
  69.                 String getusername=etUsername.getText().toString().trim();
  70.  
  71.  
  72.                 callsignup(getemail,getpassword,getusername);
  73.  
  74.                 progressDialog.setMessage("Registering User...");
  75.                 progressDialog.show();
  76.  
  77.  
  78.             }
  79.  
  80.         });
  81.  
  82.  
  83.     }
  84.  
  85.     //this part of the code is to create new users
  86.  
  87.     private void callsignup (String email,String password,String username){
  88.         if (TextUtils.isEmpty(email)){
  89.             Toast.makeText(this,"Please enter email",Toast.LENGTH_SHORT).show();
  90.             return;
  91.         }
  92.  
  93.         if (TextUtils.isEmpty(password)){
  94.             Toast.makeText(this,"Please enter password",Toast.LENGTH_SHORT).show();
  95.             return;
  96.         }
  97.  
  98.         if (TextUtils.isEmpty(username)){
  99.             Toast.makeText(this,"Please enter username",Toast.LENGTH_SHORT).show();
  100.             return;
  101.         }
  102.  
  103.         mAuth.createUserWithEmailAndPassword(email, password )
  104.                 .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  105.                     @Override
  106.                     public void onComplete(@NonNull Task<AuthResult> task) {
  107.  
  108.                             // Sign in success, update UI with the signed-in user's information
  109.                             Log.d("Testing", "Signup Succesful"+task.isSuccessful());
  110.  
  111.                         if (!task.isSuccessful()) {
  112.                             Toast.makeText(SignUpActivity.this, "Signup failed", Toast.LENGTH_SHORT).show();
  113.                             progressDialog.dismiss();
  114.                         }
  115.                         else {
  116.                             userProfile();
  117.                             Toast.makeText(SignUpActivity.this, "Account created ", Toast.LENGTH_SHORT).show();
  118.                             Log.d("Testing","Account Created");
  119.                             progressDialog.dismiss();
  120.  
  121.                         }
  122.  
  123.                     }
  124.  
  125.                 });
  126.  
  127.     }
  128.  
  129.  
  130.     //set user display name
  131.     private void userProfile(){
  132.         FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
  133.         if (user != null){
  134.             UserProfileChangeRequest profileUpdates=new UserProfileChangeRequest.Builder()
  135.                     .setDisplayName(etUsername.getText().toString().trim()).build();
  136.  
  137.  
  138.  
  139.             user.updateProfile(profileUpdates)
  140.                     .addOnCompleteListener(new OnCompleteListener<Void>(){
  141.  
  142.                         @Override
  143.                         public void onComplete(@NonNull Task<Void> task) {
  144.                             if(task.isSuccessful()){
  145.                                 Log.d("Testing","User profile updated");
  146.                             }
  147.  
  148.                         }
  149.  
  150.                     });
  151.  
  152.         }
  153.  
  154.     }
  155.  
  156.  
  157.  
  158.  
  159.     @Override
  160.     public void onClick(View view) {
  161.         if (view==tvSignin){
  162.             finish();
  163.             startActivity(new Intent(this,SignInActivity.class));
  164.         }
  165.     }
  166.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement