Hello every one and welcome back to the 5th part of "Phones gallery" series. In the last part we have designed the login screen and if you remember we connect to Firebase and implement the Google authentication.
If you didn't see the previous parts you can read them from here :
- Phones gallery [part 01] | Android studio create a splash screen with animations
- Phones gallery [part 02] | Android studio create on boarding screen 1/2
- Phones gallery [part 03] | Android studio create on boarding screen 2/2
- Phones gallery [part 04] | Android studio design the login screen
So in this part we will implement the google login.
SHA1 and SHA-256
- First of all you need to go to firebase console by clicking here.
- Choose your project (if it is your first project then you will find one project).
- Go to project settings
- Scroll down until you find add fingerprint (click on it)
- Now go to android studio and open your project
- In the right side you will find an option named Gradle click on it
- Run this "Your project name">Tasks>android>signingReports
- Now you will get a bunch of codes :
- Just copy the sha1 and paste it inside the firebase console
- Repeat the same steps with the sha-256
Allow google login from firebase console
- First of all you need to go to firebase console by clicking here.
- Go to authentication
- Click on get started
- Click on "Sign-in methods"
- Choose the Google option
- Click on enable then choose an support email and click save:
- That's it we have done with Firebase, lets write some codes
Dependencies and plugins
- Go to android studio, open your project.
- In the left side click on Project in order to open project files
- Expand Gradle scripts and open build.gradle (Module)
- Add those lines inside your dependencies
implementation 'com.google.firebase:firebase-auth:21.0.1'
implementation 'com.google.android.gms:play-services-auth:19.2.0'
implementation 'com.firebaseui:firebase-ui-auth:4.3.2'
- Outside your dependencies add this line (In case you face a problem or add it from the begining to avoid it)
apply plugin: 'com.google.gms.google-services'
- Now syn the project
Loading dialog
We need this loading dialog when the process start.So let's create this dialog
First we need to create a layout to our loading dialog, just go to res>layout and create new layout file.
Here my loadin_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@drawable/splash_bg">
<ProgressBar
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="50dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:indeterminateTint="@color/white"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/loading"
android:textColor="@color/white"
android:textSize="16sp"
android:fontFamily="@font/ubuntu"
android:textStyle="bold"
app:layout_constraintTop_toBottomOf="@id/progress"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginTop="10dp"
android:layout_marginBottom="50dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
And here my loadingDialog.java
package m.phonesgallery.phonesgallery.helpers;
import android.app.Activity;
import android.app.Dialog;
import android.view.ViewGroup;
import m.phonesgallery.phonesgallery.R;
public class LoadingDialog {
Activity activity;
Dialog dialog;
public LoadingDialog(Activity activity) {
this.activity = activity;
}
public void start(){
dialog = new Dialog(activity);
dialog.setContentView(R.layout.loading_dialog);
dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
dialog.setCancelable(false);
dialog.show();
}
public void end(){
dialog.dismiss();
}
}
Login.java
This java file contains only the Google login. In the next tutorials we will implement the email and password authentication.
So here my "Login.java" file :
package m.phonesgallery.phonesgallery;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.GoogleAuthProvider;
import m.phonesgallery.phonesgallery.helpers.LoadingDialog;
public class Login extends AppCompatActivity {
//XML
SignInButton googleBtn;
//Helpers
private final int RC_SIGN_IN = 1;
GoogleSignInClient googleSignInClient;
//firebase
FirebaseAuth firebaseAuth;
//Dialogs
LoadingDialog loadingDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//XML
googleBtn = findViewById(R.id.googleBtn);
//firebase
firebaseAuth = FirebaseAuth.getInstance();
//Dialogs
loadingDialog = new LoadingDialog(this);
//Google login
googleLoginHelper();
googleBtn.setOnClickListener(this::googleClick);
}
private void googleLoginHelper() {
GoogleSignInOptions googleSignInOptions = new GoogleSignInOptions
.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
googleSignInClient = GoogleSignIn.getClient(this,googleSignInOptions);
}
private void googleClick(View view) {
Intent signInIntent = googleSignInClient.getSignInIntent();
startActivityForResult(signInIntent,RC_SIGN_IN);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN){
loadingDialog.start();
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
//Login successfull
GoogleSignInAccount account = task.getResult(ApiException.class);
FirebaseAuthWithGoogle(account.getIdToken());
} catch (ApiException e) {
//login Failed
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
loadingDialog.end();
}
}
}
public void FirebaseAuthWithGoogle(String idToken){
AuthCredential credential = GoogleAuthProvider.getCredential(idToken,null);
firebaseAuth.signInWithCredential(credential).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
//the authentication is done we will continue this next part
saveToDatabase();
loadingDialog.end();
Toast.makeText(Login.this, "Login done", Toast.LENGTH_SHORT).show();//we will continue next time thanks for watching
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
//the authentication failed we must stop the dialog
loadingDialog.end();
Toast.makeText(Login.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
private void saveToDatabase() {
}
}
Youtube
For more understanding watch the video :
Thanks for watching i hope it's helpful.