Integrate ‘Google Plus Sign In’ to Double or Triple Your Android App Registration

0
Shares
Integrate ‘Google Plus Sign In’ to Double or Triple Your Android App Registration
3.3 (66.67%) 3 votes

Today, almost all apps such as Tinder, Quora, and Snapchat allows to login with Google Plus. Now this is a useful feature to add because it helps to boost registration rate of any Android app.

And since almost everyone tends to have Facebook and Google accounts, they are more likely to use that account to register themselves in other apps as well, if given the option. This means it could help you to double your app registrations which would directly result in a success of your Android app.

Therefore today, in this Android tutorial, we’re going to learn the integration process of Google Plus Sign-In with the help of Google Plus API.

Implementation

Before we start developing the demo, you need to make sure that you have the latest version of Google Play Service in your SDK. If that’s not the case, then go to SDK manager from your Android Studio and update it.

Also, according to the latest policy of Google, you’ll need Google-services.json file to use Google APIs in your project’s app folder.

To get this file, you’ll first need to get an SHA-1 key. There are two ways to get it.

  1. If you have a Keystore then use Keytool command to get an SHA-1 key. Open your terminal and execute the Keytool command to generate the SHA-1 fingerprint. If it asks for password then enter Android and press enter.
  2. If only single user SHA-1 Key is needed then you can get it through following steps.
  • Open Android Studio
  • Open your project
  • Click on Gradle (from right side panel, you can find Gradle Bar)
  • Click on Refresh (Click on Refresh from Gradle bar, you’ll see List Gradle script of your project.)
  • Click on your project (your project name from List.)
  • Click on Tasks.
  • Click on Android.
  • Double click on Signingreport (you will get SHA-1 and MD5 in Run bar.)

Once you get the Google-services.json file through the SHA-1 key, it’s time to create a project in Google Developer Console and paste this SHA-1 key into that project and get the Google-services.json file.

To get this Go to Android Quick Start Guide and click on Get A Configuration File button. It will redirect you to a page where you can choose a name for your package and project.

Now, create an app and give your current app package name. Here, we’re going to name it as “com.glogindemo”.

SSGPlusLogin1

Paste the SHA-1 fingerprint and click on Enable Google Sign-In. Then, click on Generate Configuration File to download it.

SSGPlusLogin2

 

SSGPlusLogin3

After downloading a file, create a new project under the file menu and give name and package name to it. Here, we’re going to name it as “G_LoginDemo”.

SSGPlusLogin4

 

Note: make sure to give the same package name which you gave in Google Console.

Now choose Blank Activity and click on next.

SSGPlusLogin5

Open project level build.gradle and add ‘com.google.gms:google-services:3.0.0’ class path to dependencies.

dependencies {
classpath 'com.android.tools.build:gradle:2.1.3'
classpath 'com.google.gms:google-services:3.0.0'
}

Open app level build.gradle and add ‘compile com.google.android.gms:play-services-auth:9.2.1’ to dependencies. It requires to add the bottom of the file, add apply plugin: ‘com.google.gms.google-services’

dependencies {

compile 'com.google.android.gms:play-services-auth:9.2.1'

// To load the G+ profile image I've used Glide Library. Ignore if you don't want to load an image.
compile 'com.github.bumptech.glide:glide:3.7.0'
}

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

Create XML file for creating login button for G+ and after login to display logged in user name and profile picture.

activity_gplus_login.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="@dimen/activity_horizontal_margin">
<LinearLayout
android:id="@+id/llProfile"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:orientation="horizontal"
android:visibility="gone"
android:weightSum="3">
<ImageView
android:id="@+id/imgProfilePic"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="2"
android:orientation="vertical">
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:textSize="20dp" />
<TextView
android:id="@+id/tvEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:textSize="18dp" />
</LinearLayout>
</LinearLayout>
<com.google.android.gms.common.SignInButton
android:id="@+id/btnSignIn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp" />
<Button
android:id="@+id/btnSignOut"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="@string/btn_logout_from_google"
android:visibility="gone" />
</LinearLayout>

Now, create a java file to perform G+ login and display information after login.

GplusLoginDemo.java

– Get all view using findviewbyId
– Manage Click event of a sign in and sign out button.
– implement GoogleApiClient.OnConnectionFailedListener listener which will override onConnectionFailed() method.
– Now, first, declare object if GoogleAPiClient

private GoogleApiClient mGoogleApiClient;
- in onCreate(Bundle savedInstanceState) initialize mGoogleApiClient object.

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();

Create two methods for sign in and sign out methods which will be called on the click of sign in and sign out button respectively.

private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
private void signOut() {
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
updateUIAfterLoginLogout(false);
}
});
}

In OnStart() method of Activity perform checking process of already signed in user

OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
if (opr.isDone()) {
// If the user's cached credentials are valid, the OptionalPendingResult will be "done"
// and the GoogleSignInResult will be available instantly.
Log.d(TAG, "Got cached sign-in");
GoogleSignInResult result = opr.get();
handleSignInResult(result);
} else {
// If the user has not previously signed in on this device or the sign-in has expired,
// this asynchronous branch will attempt to sign in the user silently. Cross-device
// single sign-on will occur in this branch.
showProgressDialog();
opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
@Override
public void onResult(GoogleSignInResult googleSignInResult) {
hideProgressDialog();
handleSignInResult(googleSignInResult);
}
});
}

If you get login success then get user information and display it.

if (result.isSuccess()) {
// Signed in successfully, show authenticated user detail
GoogleSignInAccount acct = result.getSignInAccount();
Log.e(TAG, "User name: " + acct.getDisplayName());
String personName = acct.getDisplayName();
String personPhotoUrl = acct.getPhotoUrl().toString();
String email = acct.getEmail();
Log.e(TAG, "Name: " + personName + ", Email: " + email
+ ", Image: " + personPhotoUrl);
tvName.setText(personName);
tvEmail.setText(email);
Glide.with(getApplicationContext()).load(personPhotoUrl)
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imgProfilePic);
updateUIAfterLoginLogout(true);
} else {
// Signed out, show unauthenticated user detail
updateUIAfterLoginLogout(false);
}

You can get result in onActivityResult()

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}

Done! By doing this, you can easily add Google plus sign in option in any of your Android app.

Any questions?

Contact us now. We will get back to you as soon as possible.

With this feature, our clients have almost double or triple their app downloads. Therefore, if you have an idea for Android app and in need of an Android app development company, we’re here for you.

We’ve developed more than 2000+ mobile applications successfully. Take a look at our Android Portfolio.

You can download Android Google plus source code from Github.

 
0
Shares
 

LET'S TALK VALIDATE YOUR IDEA!