Integration of social login is one of the most common tasks in app development , whether it is a mobile app or a website. Social login makes it easier for the user to login and try the app . Thus in this post we’ll learn to implement google login for android .
The steps involved are :
-
- 1. Create a new android project
- 2. Create and configure Google Api console project and get the configuration file
- 3. Set up the android studio project to integrate google login
- 4. Create a GoogleSignInOptions object (inside onCreate of MainActivity) to specify the information we need from google api.
- 5. Create a GoogleApiClient object with the the GoogleSignInOptions object created above, this will be used to access Google Sign-In api.
- 6. Add sign-in button to our MainActivity’s layout file (activity_main.xml) and set action to it
7. Handle the sing-in result inside onActivityResult()
package com.droidmonk.googlelogin; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Toast; import com.google.android.gms.auth.api.Auth; import com.google.android.gms.auth.api.signin.GoogleSignInAccount; import com.google.android.gms.auth.api.signin.GoogleSignInOptions; import com.google.android.gms.auth.api.signin.GoogleSignInResult; import com.google.android.gms.common.SignInButton; import com.google.android.gms.common.api.GoogleApiClient; public class MainActivity extends AppCompatActivity { private static final int REQ_CODE_GOOGLE_SIGN_IN = 1; private GoogleApiClient mGoogleApiClient; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); GoogleSignInOptions gso=new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestEmail() .build(); mGoogleApiClient=new GoogleApiClient.Builder(this) .addApi(Auth.GOOGLE_SIGN_IN_API,gso) .build(); SignInButton btn= (SignInButton) findViewById(R.id.google_login); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); startActivityForResult(intent,REQ_CODE_GOOGLE_SIGN_IN); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode==REQ_CODE_GOOGLE_SIGN_IN ) { /*get sign in result*/ GoogleSignInResult result=Auth.GoogleSignInApi.getSignInResultFromIntent(data); /*check if logged in*/ if(result.isSuccess()) { /*Successfully logged in */ GoogleSignInAccount account=result.getSignInAccount(); Toast.makeText(this,"Login success:"+account.getDisplayName(),Toast.LENGTH_LONG).show(); } else { /*Failed to log in*/ Toast.makeText(this,"Login failure",Toast.LENGTH_LONG).show(); } } } }
<?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:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.droidmonk.googlelogin.MainActivity"> <com.google.android.gms.common.SignInButton android:id="@+id/google_login" android:layout_width="match_parent" android:layout_height="wrap_content" /> </RelativeLayout>
apply plugin: 'com.android.application' android { compileSdkVersion 25 buildToolsVersion "25.0.2" defaultConfig { applicationId "com.droidmonk.googlelogin" minSdkVersion 15 targetSdkVersion 25 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile 'com.android.support:appcompat-v7:25.3.1' compile 'com.google.android.gms:play-services-auth:10.2.0' } apply plugin: 'com.google.gms.google-services'
buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.3.3' classpath 'com.google.gms:google-services:3.0.0' } } allprojects { repositories { jcenter() } } task clean(type: Delete) { delete rootProject.buildDir }