Sunday, January 8, 2017

Simple Android Tutorial Login Using Google Account



Now days, much of application work with required create an account, there are lot of way to create an account to the apps : Using valid email, Facebook  and etc.

Generally, those who develop an apps using Facebook and Google as the way to register beside the valid email.


The information that taken from Google and Facebook generally are name of account, email, profile picture and other public information.

Beside that, you also can post something to your Facebook home from your android application. I will post this topic soon. Interesting right?

For use your google account, you have to had Google email. After that fill the public information in your Google + account.

See also : Simple Android Tutorial Login Using Facebook Account

but, i think all of us have been had a Google plus account, right? So let start the implementation Login using Google plus account.

1. COMPILE THE LIBRARIES

Compile the following libraries in your build.gradle (Module : app):

In your build.gradle (Project : YourProjectName),  paste the classpath below :

classpath 'com.google.gms:google-services:3.0.0'

2. REGISTER YOUR PACKAGE NAME AND SHA-1 KEY

Actually this step for get the google-service.json. To get that file, you have to register your package project's name and SHA-1 key.

First find your SHA-1 key, take a look at the Figure 1 :

Figure 1

Guideline :

  1. Klik Gradle tab on the right side of your android studio IDE
  2. Klik the icon Refresh all the gradle projects
  3. Open :app/task/android/signingReport. Double click of signingReport.
  4. Your SHA-1 Key appears on the console.

Afte you find your SHA-1 Key, Open Developers.google console. Create new project or your can use your existing project.

To generating the google.service.json, just follow the figures below :

Figure 1

Guideline Figure 1 :
  1. Choose your project name (or create new one)
  2. Paste your package name
  3. Click the button Choose and configure services ~>

Figure 2

Guidelines fFigure 2 :
  1. Choose Google SignIn Mode
  2. Paste your SHA-1 Key
  3. Click Enable Google Sign In

Figure 3

Guideline Figure 3 :
  1. Just click the button Generate configuration files

Figure 4

Guideline Figure 4 :
  1. Download the google-service.json and place inside folder app in you directory project. Take a look at the figure 5

Figure 9

After that create your layout xml. Just simple layout for this tutorial. in this project i labelled as activity_google_login.xml. Here its code :

In your java class, there several steps that you have to know, there are :

Google Sign Options

//google signin option
GoogleSignInOptions googleSignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestEmail().build();

Google Api Client

//set google api

googleApiClient = new GoogleApiClient.Builder(this)
        .enableAutoManage(this,this) /*activity, onConnectionFailedListener*/        
        .addApi(Auth.GOOGLE_SIGN_IN_API, googleSignInOptions)
        .build();

Method getCachange()

The flow of this method is, in case the users have logged in, when they open the apps again, user no need to login again because method getCache() will displaying the information. The point is, this method will work if the users don't logout the apps before.

Method HandleSignInResult()

After login, this method will set the display name and profile picture on you application. Glide will uses as the Image Loader from URL.

That's the main step that you have to know. Maybe the code above is still unclear for you, so the follows is the full code of java classes :


Download the project by clicking the following button :


Related Posts


EmoticonEmoticon