In this Example, You will simply Login Firebase Cloud Database using Android , this is a Live Application Example :
package com.example.bschandel.indservices;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
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;
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);
progressDialog = new ProgressDialog(this);
buttonRegister = (Button)findViewById(R.id.buttonRegister);
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
editTextPassword = (EditText)findViewById(R.id.editTextPassword);
textViewSignin = (TextView) findViewById(R.id.textViewSignin);
firebaseAuth = FirebaseAuth.getInstance();
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,"Email is Empty, Please Enter Email ",Toast.LENGTH_SHORT).show();
//stopping the function execution
return;
}
if (TextUtils.isEmpty(password)) {
//password is empty
Toast.makeText(this,"Password is Empty, Please Enter a Password",Toast.LENGTH_SHORT).show();
//stopping the function execution
return;
}
// if validations are ok
// we will first show a progressDialog
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()) {
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
}
}
}
Now just copy and paste this code and use it... wherever you want a login activities....
Comments
Post a Comment