Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BlogSingleActivity extends AppCompatActivity {
- private Button mSubmitBook;;
- private DatabaseReference mDatabaseBookUser;
- private FirebaseAuth firebaseAuth;
- List<Blog> Info;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_blog_single);
- firebaseAuth= FirebaseAuth.getInstance();
- mSubmitBook = (Button) findViewById(R.id.SubmitBook);
- mDatabaseBookUser = FirebaseDatabase.getInstance().getReference("Users");
- Info = new ArrayList<>();
- mSubmitBook.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- if(view== mSubmitBook){
- addName();
- }
- }
- });
- }
- public class MainActivity extends AppCompatActivity implements View.OnClickListener{
- private Button buttonRegister;
- private EditText mMatricField;
- private EditText mNameField;
- private EditText editTextEmail;
- private EditText editTextPassword;
- private TextView textViewSignin;
- private ProgressDialog progressDialog;
- private FirebaseAuth firebaseAuth;
- private DatabaseReference mDatabase;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- firebaseAuth= FirebaseAuth.getInstance();
- if(firebaseAuth.getCurrentUser() !=null){
- //profile activity here
- finish();
- startActivity(new Intent(getApplicationContext(),ProfileActivity.class));
- }
- mDatabase= FirebaseDatabase.getInstance().getReference().child("Users");
- progressDialog=new ProgressDialog(this);
- buttonRegister=(Button) findViewById(R.id.buttonRegister);
- mMatricField= (EditText) findViewById(R.id.matricField);
- mNameField=(EditText) findViewById(R.id.nameField);
- 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(){
- final String matric=mMatricField.getText().toString().trim();
- final String name= mNameField.getText().toString().trim();
- String email=editTextEmail.getText().toString().trim();
- String password=editTextPassword.getText().toString().trim();
- if(TextUtils.isEmpty(matric)){
- //password is empty
- Toast.makeText(this, "Please enter matric number", Toast.LENGTH_SHORT).show();
- //stopping the function execution further
- return;
- }
- if(TextUtils.isEmpty(name)){
- //password is empty
- Toast.makeText(this, "Please enter your name", Toast.LENGTH_SHORT).show();
- //stopping the function execution further
- return;
- }
- if(TextUtils.isEmpty(email)){
- //email is empty
- Toast.makeText(this,"Please enter email",Toast.LENGTH_SHORT).show();
- //stopping the function execution further
- return;
- }
- if(TextUtils.isEmpty(password)){
- //password is empty
- Toast.makeText(this, "Please enter password", Toast.LENGTH_SHORT).show();
- //stopping the function execution further
- return;
- }
- //if validation are ok
- //we will first show a progressbar
- 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()){
- String user_id=firebaseAuth.getCurrentUser().getUid();
- DatabaseReference current_user_db= mDatabase.child(user_id);
- current_user_db.child("name").setValue(name);
- current_user_db.child("matric").setValue(matric);
- current_user_db.child("image").setValue("default");
- //user is successfully registered and logged in
- //we will start the profile activity here
- //display a toast only
- finish();
- startActivity(new Intent(getApplicationContext(),ProfileActivity.class));
- Toast.makeText(MainActivity.this, "Registered Successfully", Toast.LENGTH_SHORT).show();
- }else{
- Toast.makeText(MainActivity.this, "Could not register... Please try again", Toast.LENGTH_SHORT).show();
- }
- }
- });
- }
- @Override
- public void onClick(View view) {
- if(view== buttonRegister){
- registerUser();
- }
- if(view== textViewSignin){
- //will open login activity here
- startActivity(new Intent(this, LoginActivity.class));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement