Hey It’s

Ashutosh from #TeamRadicallabs

and if you are looking for how to integrate login with google or add Google Login in your Android project then this Content will really helps you a lot…

Yes It Is Possible, And For This We Will Create It With the Help Of The Google’s APIs.

  1. If We Going To Imagine It That How It Will Looks Like Then We Can See Many Apps That Are Currently Using This For Authentication Purpose.
  1. Theory Section
    1. First Of All We Will Create A Google App At Google’s Developers For Communicating And Authenticating Of Your Android App.
    2. And Then We Will Configure Our Project With Google Services And Apis.
    3. After This We Will Create A User Interface With Google’s Login Button For Performing Actions.
    4. Then We Will Simply Code For Login Features And Get Data From Google Servers.

 

  1. Configuration Sectio

    n

    1. So We Have To A Create An Android Project With Our Android Studio/Eclipse Ide.
    2. After App Creation, Choose To Download The Configureservices.Json File Which Includes All Necessary About Your Application. So Paste It In Our Project’s App Directory.
    3. Make Sure That We Have Already Installed Google Repository In Our Project If Not Just Download And Install It For Your Tool>Sdk Manager>Sdk Tools…
    4. Now Starts With Androidmainfest.Xml File And Include The Internet Permission To Your Application.

 

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

 

  1. Now Add Some Dependencies In Our Project For Including Google’s Api Code.
    1. Add This Dependency To Project Level.

 

classpath ‘com.google.gms.google-services:3.0.0’
  1. Add This Dependency To App Level(After Apply Plugin Com.Android.Application).

 

apply plugin : ‘com.google.gms.google-sevices’

 

  • Add This Dependency To App Level.

 

compile ‘com.google.android.gms:play-services-auth:9.8.0’

 

  1. And Now Here We Are Done With Configuration Section (Dependency And All), And Its Basically To Being(/Working) Google’s Api In Your Project And It’s Obviously Stores In Google’s Repository As We Have Installed It.

 

  1. User Interface Section
    1. And If We Open Our activity_main.Xml File Then Here Is Some Code That Are Transferred By The Android Studio/Eclipse.
    2. So Now Just Add Google’s Login Button, Simple Button For Logout , Text View For Displaying User Name And Email Address And A Image View For Showing User’s Profile Pic To This Layout File. And Get All These Components Into Your MainActivity.Java(By FindViewById(R.Id. Sign_In_Button)).

<com.google.android.gms.common.signinbutton
android:id="@+id/sign_in_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dip"
/>

 

  1. And Now Here We Are Done With User Interface Section, And Its Basically For Displaying The Interface To The User Within Our Application.

 

  1. Coding Section
    1. Now If We Open Our MainActivity.Java File Then Here Is Some Code That Are Transferred By The Android Studio/Eclipse.
    2. So Before Starting The Coding Section Let’s Focus On These Two Terms.
      1. GoogleSignIn:- Create Sign In Request
      2. GoogleApiClient:- Fire Sign In Request
    3. Now Create A Sign In Request By GoogleSignInOptions Class With Default Parameter.
GoogleSignInOptions googleSignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.<em>DEFAULT_SIGN_IN</em>) requestEmail().build();

 

  1. Now Fire This Sign In Request By GoogleApiClient Class With Default Parameter.
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(MainActivity.this)
.addApi(Auth.<em>GOOGLE_SIGN_IN_API</em>, googleSignInOptions)
.build();

 

  1. And Now Simply Connect Our GoogleApiClient Object To Connection.
googleApiClient.connect();

 

  1. And Now Simply Create An Intent To Get Intent Prompt From Google.
Intent signInIntent = Auth.<em>GoogleSignInApi</em>.getSignInIntent(googleApiClient);
startActivityForResult(signInIntent, <em>RC_SIGN_IN</em>);
  1. And Now Simply Request From SignInResult With GetSignInResult From Intent. And All These Things Will Happen In Intent’s Callback Method In onActivityResult();
    1. And Now Here We Will Authenticate That Is Our Login Process Is Successful Or Not And It’s Done By GoogleSignInResult Object With It’s Method.
      1. isSuccess():- If It Gives Ture Then Only, Our Authentication Is Successful.
      2. Otherwise Our Authentication Is Fails.
    2. And If Our Authentication Is Successful Then Only We Can Get User’s Data From GoogleSignInAccount Object

By Calling GoogleSignInResult Object At getsignInAccount() Method;

  1. Now We Can Get User Data Like ‘Email’,’Name’,’Profile Pic’ And Much More By Calling GoogleSignInAccount Object At It’s Appropriate Methods.

 

&lt;@override
public void onActivityResult(Int requestCode, Int resultCode, Intent data) {
super.onActivityResult(requestCode, Resultc resultCode ode, data);
// Result Returned From Launching The Intent From Googlesigninapi.Getsigninintent(...);
if (requestCode == <em>RC_SIGN_IN</em>) {
GoogleSignInResult result = Auth.<em>GoogleSignInApi</em>.getSignInResultFromIntent(data);
if (result.isSuccess()) {
Log.e("Status ", "Successfully Logged In...");
GoogleSignInAccount googleSignInAccount = result.getSignInAccount();
Uri personPhoto = googleSignInAccount.getPhotoUrl();
Glide.<em>with</em>(MainActivity.this).load(personPhoto).Into(imageView);
textview.setText(GoogleSignInAccount.getEmail() + "\n" + GoogleSignInAccount.getDisplayName());
}
}

And Now Here We Are Done With Coding Section And All, And Its Basically Implementing Login Feature Within Our Application…

Now Your Desired Application Is Ready To Use…

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>