Android Login with Google Tutorial

Today Almost all web and mobile apps allow you to login with Google and Facebook Login. Android Login with Google is a really useful feature for both the app developer and the user, since almost everybody tend to have a google and facebook account and moreover with android login with google you don’t need to remember your UserId and password. In this Android login with google tutorial we will describe how to integrate Google Login to your Android Application, We will be creating a sample Google Login app describing how to integrate google login and retrieve user profile. The user can login using her existing google account. Integration of Google Login to the App allows the app developers to perform actions on user behalf.

Pre-requisites of Android Login with Google API

Android Studio installed on your PC (Unix or Windows). You can learn how to install it here .

A compatible Android device that runs Android 2.3 or newer and includes the Google Play Store or an emolator with an AVD that runs the Google APIs platform based on Android 4.2.2 or newer and has Google Play Services version 8.3.0 or newer.

The latest version of the Android SDK, including the SDK Tools component.

The project must be configured to compile against Android 2.3 (Gingerbread) or newer.

Installing/Updating Google Play Services

To update/Install The Google Play Services SDK:

In Android Studio, select Tools > Android > SDK Manager.

Scroll to the bottom of the package list and select Extras > Google Play services.

The package is downloaded to your computer and installed in your SDK environment at android-sdk-folder/extras/google/google_play_services.

Get a Configuration File

The configuration file provides service-specific information for your app. Go to Google Developer’s Page. To get it, you must select an existing project for your app or create a new one. You’ll also need to provide a package name for your app.

Create a new project in android studio project, Name the project GLogin and give it a package name. Choose the activity name as LoginActivity.

Now add app name and package name on Google Developers page as shown below.

Click on Choose and configure services button

Choose Google Sign-In the service page.

We will continue on this page, but first, we have to generate digitally signed a public certificate which will be.

Generate SHA-1 fingerprint

In order to consume google plus services, first we need to enable the Google Plus API on google console and we need to register our digitally signed .apk file’s public certificate in the Google APIs Console.
Java key tool an be used to generate the SHA-1 fingerprint.

Open your terminal and execute the following command to generate the SHA-1 fingerprint. If it asks for a password, type android and press enter.

The above layout of our project i.e. Android Login with Google, consist of a LinearLayout and RelativeLayout inside a parent LinearLayourt. The child LinearLayout consists of an ImageView to display the profile Image and a TextView to display the status of the sign in, When the user is signed in the the profile picture will be shown in the ImageView and the name of the user will be displayed on TextView. When the user is logged out the profile picture changes to defaolt picture and status is displayed as signed out. The RelativeLayout consists of com.google.android.gms.common.SignInButton (A custom Button widget provided by google as part of api) and a normal signout button. User will click on the Sign-In Button to android login with google in the app. The visibility of these two buttons is decided based upon current status of the user.

Create a new fragment GPlusFragment.java and perform the following steps.

The above code creates a signInIntent and onClick() method, handle google login button taps by creating a sign-in intent with the getSignInIntent() method, and starting the intent with startActivityForResult. The second argument uniquely identifies your request. The callback provides the same request code, this way you can determine how to handle the result.
Starting the intent prompts the user to select a Google account to log in google. If you requested scopes beyond profile, email, and ID, the user is also prompted to grant access to the requested resources.

In above code snippet of android login with google, we have added a click listener for the sign out button, it invokes the signOut() method of the google api. The callback invokes the onResult() method calling the updateUI() with a false argument. Let’s discuss the updateUI() method.

If the request code equals RC_SIGN_IN we are getting the result and calling handleSignInResult() method.

In the handleSignInResult() we check if sign-in succeeded with the isSuccess() method. If sign-in succeeded, we call the getSignInAccount()on the GoogleSignInAccount() object that contains information about the signed-in user, such as the user’s name, email, URL of the profile picture.

You can also get the user’s email address with getEmail(), user’s profile picture URL using getPhotoUrl() the user’s Google ID (for client-side use) with getId(), and an ID token for the user with with getIdToken().

In case the user was already logged in to google and has returned to the app, we want to get signed in automatically without the user having to sign in again, so in the onStart() method of the GPlusFragment we are calling the silentSignIn() method of the google login api and will be using the user’s cached information.

If the cached details are valid, the OptionalPendingResult will be equal to done and the GoogleSignInResult will be available otherwise, it will attempt to login the user.

We are using three helper methods in Android Login with Google App, showProgressDialog() to show a Progress Dialog in the form of a rotating circle while signing in ,hideProgressDialog() method to hide the Progress Dialog on successful login and LoadProfileImage() to load the profile picture of the user in the profile picture image view. Add the following code to the fragment class.

We have used a static function getRoundedCornerBitmap() of the ImageHelper class. Create a new class ImageHelper.java and put the code from the file in the link. => ImageHelper.java
This method accepts a Bitmap image and returns an image with rounded corners as shown in the video.

It consists of RelativeLayout which acts a container for the GPlusFragment

You can add the image resources used in the project, by downloading the project from Download Now button at the top of the post and copying the images from the drawable’s folder.
Other resource files such as strings.xml, dimens.xml, colors.xml can be downloaded from the below links.strings.xmldimens.xmlcolors.xml

Now, run the app (Android Login with Google) on your phone or emulator where you are already using your Google/Gmail account, and you should be able to sign into the android application using Google Sign-In.

What’s Next!!!

You can experiment with different user permissions and try to access the information from the user. You can follow our post
and integrating the login with the Navigation Drawer using the following tutorial => Android Navigation Drawer for Sliding Menu / Sidebar.
Till then stay tuned for more tutorials.. and Don’t forget to subscribe our blog for latest android tutorials. Also do Like our Facebook Page or Add us on Twitter.

My partner and I absolutely love your blog and find nearly all of your post’s to be just what I’m looking for. Would you offer guest writers to write content available for you? I wouldn’t mind publishing a post or elaborating on a lot of the subjects you write related to here. Again, awesome web site!

Just wish to say your article is as amazing. The clearness in your post is just cool and i could assume you’re an expert on this subject. Well with your permission let me to grab your feed to keep up to date with forthcoming post. Thanks a million and please carry on the rewarding work.

Popular Tags

ABOUT

This is one stop solution for both android developers and learners.
Learners can refer our basic tutorials and developers can refer Material Design, Networking and Intermediate tutorials. Stick with us for a while and we promise you will become an Android Rockstar. All the Best !!