Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- setContentView(R.layout.activity_main);
- Button btnSignIn,btnRegister;
- RelativeLayout rootLayout;
- FirebaseAuth auth;
- FirebaseDatabase db;
- DatabaseReference users;
- @Override
- protected void attachBaseContext(Context newBase) {
- super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
- .setDefaultFontPath("fonts/Arkhip_font.ttf")
- .setFontAttrId(R.attr.fontPath)
- .build());
- setContentView(R.layout.activity_main);
- //initialization of firebase
- auth = FirebaseAuth.getInstance();
- db=FirebaseDatabase.getInstance();
- users=db.getReference("Users");
- btnRegister =(Button)findViewById(R.id.btnRegister);
- btnSignIn=(Button)findViewById(R.id.btnSignIn);
- rootLayout= (RelativeLayout) findViewById(R.id.rootLayoutMax);
- //Event
- btnRegister.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- showRegisterDialog();
- }
- });
- btnSignIn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- showLoginDialog();
- }
- });
- }
- private void showLoginDialog() {
- final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
- dialog.setTitle("SIGN IN ");
- dialog.setMessage("Please use email to sign in");
- LayoutInflater inflater = LayoutInflater.from(this);
- View login_layout = inflater.inflate(R.layout.layout_login,null);
- final MaterialEditText edtEmail = login_layout.findViewById(R.id.edtEmail);
- final MaterialEditText edtPassword = login_layout.findViewById(R.id.edtPassword);
- dialog.setView(login_layout);
- //Setting Button
- dialog.setPositiveButton("SIGN IN", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- dialogInterface.dismiss();
- //Check validation
- if (TextUtils.isEmpty(edtEmail.getText().toString())) {
- Snackbar.make(rootLayout, "Please enter email address", Snackbar.LENGTH_LONG).show();
- return;
- }
- if (TextUtils.isEmpty(edtPassword.getText().toString())) {
- Snackbar.make(rootLayout, "Please enter password", Snackbar.LENGTH_LONG).show();
- return;
- }
- if (edtPassword.getText().toString().length() < 6) {
- Snackbar.make(rootLayout, "Password too short", Snackbar.LENGTH_LONG).show();
- return;
- }
- //Login
- auth.signInWithEmailAndPassword(edtEmail.getText().toString(),edtPassword.getText().toString())
- .addOnSuccessListener(new OnSuccessListener<AuthResult>() {
- @Override
- public void onSuccess(AuthResult authResult) {
- startActivity(new Intent(MainActivity.this,Welcome.class));
- finish();
- }
- }).addOnFailureListener(new OnFailureListener() {
- @Override
- public void onFailure(@NonNull Exception e) {
- Snackbar.make(rootLayout,"Failed"+e.getMessage(),Snackbar.LENGTH_SHORT)
- .show();
- }
- });
- }
- });
- dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- dialogInterface.dismiss();
- }
- });
- dialog.show();
- }
- private void showRegisterDialog() {
- final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
- dialog.setTitle("REGISTER ");
- dialog.setMessage("Please use email to register");
- LayoutInflater inflater = LayoutInflater.from(this);
- View register_layout = inflater.inflate(R.layout.layout_register,null);
- final MaterialEditText edtEmail = register_layout.findViewById(R.id.edtEmail);
- final MaterialEditText edtPassword = register_layout.findViewById(R.id.edtPassword);
- final MaterialEditText edtName = register_layout.findViewById(R.id.edtName);
- final MaterialEditText edtPhone = register_layout.findViewById(R.id.edtPhone);
- dialog.setView(register_layout);
- //Setting Button
- dialog.setPositiveButton("REGISTER", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- dialogInterface.dismiss();
- //Check validation
- if(TextUtils.isEmpty(edtEmail.getText().toString())){
- Snackbar.make(rootLayout,"Please enter email address",Snackbar.LENGTH_LONG).show();
- return;
- }
- if(TextUtils.isEmpty(edtPhone.getText().toString())){
- Snackbar.make(rootLayout,"Please enter phone number",Snackbar.LENGTH_LONG).show();
- return;
- }
- if(TextUtils.isEmpty(edtPassword.getText().toString())){
- Snackbar.make(rootLayout,"Please enter password",Snackbar.LENGTH_LONG).show();
- return;
- }
- if(edtPassword.getText().toString().length() < 6){
- Snackbar.make(rootLayout,"Password too short",Snackbar.LENGTH_LONG).show();
- return;
- }
- //Register new user
- auth.createUserWithEmailAndPassword(edtEmail.getText().toString(),edtPassword.getText().toString())
- .addOnSuccessListener(new OnSuccessListener<AuthResult>() {
- @Override
- public void onSuccess(AuthResult authResult) {
- //That will save user to db
- User user = new User();
- user.setEmail(edtEmail.getText().toString());
- user.setPassword(edtPassword.getText().toString());
- user.setName(edtName.getText().toString());
- user.setPhone(edtPhone.getText().toString());
- //It will use email to key
- users.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
- .setValue(user)
- .addOnSuccessListener(new OnSuccessListener<Void>() {
- @Override
- public void onSuccess(Void aVoid) {
- Snackbar.make(rootLayout,"Register success fully",Snackbar.LENGTH_LONG).show();
- }
- })
- .addOnFailureListener(new OnFailureListener() {
- @Override
- public void onFailure(@NonNull Exception e) {
- Snackbar.make(rootLayout,"Failed"+e.getMessage(),Snackbar.LENGTH_LONG).show();
- }
- });
- }
- })
- .addOnFailureListener(new OnFailureListener() {
- @Override
- public void onFailure(@NonNull Exception e) {
- Snackbar.make(rootLayout,"Failed"+e.getMessage(),Snackbar.
- LENGTH_LONG).show();
- }
- });
- }
- });
- dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- dialogInterface.dismiss();
- }
- });
- dialog.show();
- }
- <?xml version="1.0" encoding="utf-8"?>
- <ripple
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:color="@color/rippleEffectColor"
- tools:targetApi="lollipop"
- >
- <item>
- <shape android:shape="rectangle">
- <solid android:color="@android:color/white"></solid>
- <stroke android:color="@android:color/white" android:width="2dp">
- </stroke>
- <corners android:radius="2dp"/>
- </shape>
- </item>
- </ripple>
Add Comment
Please, Sign In to add comment