1 You have to design 2 screens.
a) Registration screen
b) Dashboard screen
2) Registration screen should contain below fields:
a) First Name
b) Last Name
c) Email
d) Mobile Number
e) Password
f) Confirm Password
3) Registration Form should have standard validation like:
a) User can enter max 50 characters for First Name
b) User can enter max 100 characters for Last Name
c) Email standard validation (xx@xx.xx)
d) Password should be in hidden
e) Users can enter max 20 characters for Passwords.
4) Registration screen records should be inserted into the Firebase Realtime Database.
5) After submitting a record on the Registration screen, the user should redirect to the Dashboard screen.
6) On Dashboard Screen, Get all the users data from database and display list of users.
I have done the authentication is working fine but not showing in realtime database.
What I have tried:
Package com.example.registration;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
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.crashlytics.buildtools.reloc.org.apache.http.util.TextUtils;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
public class MainActivity extends AppCompatActivity {
private EditText edfname,edlname,edemail,edpassword,edcpassword,edphone;
private Button btregister;
FirebaseAuth fAuth;
FirebaseDatabase firebaseDatabase;
DatabaseReference databaseReference;
RegisterInfo registerInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edfname = findViewById(R.id.edfname);
edlname = findViewById(R.id.edlname);
edemail = findViewById(R.id.edemail);
edpassword = findViewById(R.id.edpassword);
edcpassword = findViewById(R.id.edcpassword);
edphone = findViewById(R.id.edphone);
firebaseDatabase = FirebaseDatabase.getInstance();
databaseReference = firebaseDatabase.getReference("RegisterInfo");
registerInfo = new RegisterInfo();
btregister = findViewById(R.id.btregister);
btregister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String fname = edfname.getText().toString().trim();
String lname = edlname.getText().toString().trim();
String email= edemail.getText().toString().trim();
String password = edpassword.getText().toString().trim();
String cpassword = edcpassword.getText().toString().trim();
String phone = edphone.getText().toString().trim();
fAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
Toast.makeText(MainActivity.this, "User created", Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(),Dashboard.class));
}
else{
Toast.makeText(MainActivity.this, "Error !" + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
if (TextUtils.isEmpty(fname) && TextUtils.isEmpty(lname) && TextUtils.isEmpty(email) && TextUtils.isEmpty(password) && TextUtils.isEmpty(cpassword)&& TextUtils.isEmpty(phone) ) {
Toast.makeText(MainActivity.this, "Please add some data.", Toast.LENGTH_SHORT).show();
} else {
addDatatoFirebase(fname,lname,email,password,cpassword,phone);
}
}
});
}
private void addDatatoFirebase(String fname, String lname, String email,String password, String cpassword, String phone) {
registerInfo.setRfname(fname);
registerInfo.setRlanme(lname);
registerInfo.setRemail(email);
registerInfo.setRpassword(password);
registerInfo.setRcpassword(cpassword);
registerInfo.setRphone(phone);
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
databaseReference.setValue(registerInfo);
Toast.makeText(MainActivity.this, "data added", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(MainActivity.this, "Fail to add data " + error, Toast.LENGTH_SHORT).show();
}
});
}
}