How to integrate Google Sign-In button in Android Application
build.gradle(Project)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.3"
classpath 'com.google.gms:google-services:4.3.10'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}build.gradle (App)
plugins {
id 'com.android.application'
id 'com.google.gms.google-services'
}
android {
compileSdkVersion 31
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.learnoset.googlesignin"
minSdkVersion 19
targetSdkVersion 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
testImplementation 'junit:junit:'
implementation platform('com.google.firebase:firebase-bom:28.4.2')
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'com.google.android.gms:play-services-auth:19.2.0'
}google-services.json
// Replace this file with your own google-service.json file. You ca get this file from Firebase
{
"project_info": {
"project_number": "306407888156",
"project_id": "signin-15b66",
"storage_bucket": "signin-15b66.appspot.com"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:306407888156:android:421d29c936e0905b3896d9",
"android_client_info": {
"package_name": "com.learnoset.googlesignin"
}
},
"oauth_client": [
{
"client_id": "306407888156-9lbrvac246v834h3eepjlnjsonjvqcmv.apps.googleusercontent.com",
"client_type": 1,
"android_info": {
"package_name": "com.learnoset.googlesignin",
"certificate_hash": "ce5dbf10dd6cb3a8952c3fde991ee53a45caa940"
}
},
{
"client_id": "306407888156-dq4fa1cefqmdo5d6g7u77ehk4s2bcdtk.apps.googleusercontent.com",
"client_type": 3
}
],
"api_key": [
{
"current_key": "AIzaSyCtKJCHQcaDOFoN0ViuyiJz2dpCbP6zrJM"
}
],
"services": {
"appinvite_service": {
"other_platform_oauth_client": [
{
"client_id": "306407888156-dq4fa1cefqmdo5d6g7u77ehk4s2bcdtk.apps.googleusercontent.com",
"client_type": 3
}
]
}
}
}
],
"configuration_version": "1"
}MainActivity.java
package com.learnoset.googlesignin;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
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.api.ApiException;
import com.google.android.gms.tasks.Task;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView signInBtn = findViewById(R.id.signInBtn);
GoogleSignInOptions googleSignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
GoogleSignInClient googleSignInClient = GoogleSignIn.getClient(this, googleSignInOptions);
GoogleSignInAccount googleSignInAccount = GoogleSignIn.getLastSignedInAccount(this);
// checking if user already signed in
if (googleSignInAccount != null) {
// open home activity
startActivity(new Intent(MainActivity.this, HomeActivity.class));
finish();
}
ActivityResultLauncher<Intent> activityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
// getting signed in account after user selected an account from google accounts dialog
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(result.getData());
handleSignInTask(task);
}
});
signInBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent signInIntent = googleSignInClient.getSignInIntent();
activityResultLauncher.launch(signInIntent);
}
});
}
private void handleSignInTask(Task<GoogleSignInAccount> task) {
try {
GoogleSignInAccount account = task.getResult(ApiException.class);
// getting account data
final String getFullname = account.getDisplayName();
final String getEmail = account.getEmail();
final Uri getPhotoUrl = account.getPhotoUrl();
// opening HomeActivity
startActivity(new Intent(MainActivity.this, HomeActivity.class));
finish();
} catch (ApiException e) {
e.printStackTrace();
Toast.makeText(this, "Failed or Cancelled", Toast.LENGTH_SHORT).show();
}
}
}activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/google_icon"
android:adjustViewBounds="true"
android:layout_marginStart="60dp"
android:layout_centerInParent="true"
android:id="@+id/signInBtn"
android:layout_marginEnd="60dp"/>
</RelativeLayout>activity_home.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/fullName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Full Name" />
<TextView
android:id="@+id/emailId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Email Id" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/signOutBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Sign Out" />
</LinearLayout>
</RelativeLayout>HomeActivity.java
package com.learnoset.googlesignin;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
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;
public class HomeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
final TextView email = findViewById(R.id.emailId);
final TextView fullname = findViewById(R.id.fullName);
final AppCompatButton signOutBtn = findViewById(R.id.signOutBtn);
GoogleSignInOptions googleSignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
GoogleSignInClient googleSignInClient = GoogleSignIn.getClient(this, googleSignInOptions);
GoogleSignInAccount googleSignInAccount = GoogleSignIn.getLastSignedInAccount(this);
// get details for signed in user
final String getFullName = googleSignInAccount.getDisplayName();
final String getEmail = googleSignInAccount.getEmail();
email.setText("Email : " + getEmail);
fullname.setText("FullName : " + getFullName);
signOutBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// sign out
googleSignInClient.signOut();
// opening MainActivity to sign in again
startActivity(new Intent(HomeActivity.this, MainActivity.class));
finish();
}
});
}
}Output
Projects with Source Code + Video Tutorials
You can download our Java and Android Studio Projects with Source Code and Video Tutorials.
Create your own Web Browser Source Code
Bottom Navigation Bar with Fragments + Animation
Login and Register Activity Material UI design in Android Studio
If you have any Questions or Queries
You can mail us at info.learnoset@gmail.com
Follow us to learn Coding and get in touch with new Technologies.
