Authenticate Using Google Sign-In on Android - Androhub

Google Sign In Banner

Authenticate Using Google Sign-In on Android

Hello to all, today we are going to learn how to implement Google Sign In authentication in our Android app and let user do Sign In in single click and do Sign Out also.

Google Sign-In lets users sign in to your Android app with their existing Google account and get their profile information like name, email, profile pic and other details.

VIDEO DEMO

Add the Google Service Plugin

1. Add the dependency to your project-level build.gradle:

2. Add the plugin to your app-level build.gradle:

Add Google Play Services

In your app-level build.gradle file, declare Google Play Services as a dependency:

Finally your app-level build.gradle will look like below:

Since we will load user profile pic so we will be needing Picasso dependency also.

Get a Configuration File

To get the configuration file go to this link and follow the Video tutorial given above.

Now after getting configuration file, copy and paste the .json file under app directory and follow the below steps to Add Google Sign In in your app.

1. Create an xml layout naming activity_main.xml and add the below code to it. In this layout i have taken two Button one is for inbuilt or default Google Sign In Button and second one custom Sign In Button and a TextView and ImageView to display user information.

2. After creating follow the below steps to Add Google Sign-In and Sign-Out :

  • Configure Google Sign-In : Configure Google Sign-In to request users’ ID and basic profile information, create a GoogleSignInOptions object with the DEFAULT_SIGN_IN parameter. To request users’ email addresses as well, create the GoogleSignInOptions object with the requestEmail option. After that create a GoogleSignInClient object with the options you specified.

  • On Click of button call the below method to start Google Sign-In using getSignInIntent().

Account Selection Dialog
Account Selection Dialog

 

  • After the user signs in, you can get a GoogleSignInAccount object for the user in the activity’s onActivityResult method.

The GoogleSignInAccount object contains information about the signed-in user, such as the user’s name.

  • Get Profile Information : Use the GoogleSignIn.getLastSignedInAccount method or account instance  to request profile information for the currently signed in user.

Get User Profile Information
Get User Profile Information

 

  • Sign Out : Call the below method to do Sign Out.This code clears which account is connected to the app. To sign in again, the user must choose their account again.

  • Disconnect Accounts :  It is highly recommended that you provide users that signed in with Google the ability to disconnect their Google account from your app. If the user deletes their account, you must delete the information that your app obtained from the Google APIs.

  • Silent Sign In : If some user is already authenticates with your app and to check that put the below code in your activity onStart method.

3. Full MainActivity code:

4. Finally all done , now you can also add Google Sing-In to your Android app.

Thanks. :)

 

Post comment

Your email address will not be published. Required fields are marked *