Android Google Plus Login-Sign UP Integration example tutorial – Google+ Login Part-2 Complete

How to add Google+ Login-Logout Using Google developers API key inside android application with complete source code.

In my last Google+ Login part-1 tutorial we have discussed about 2 most important things first is Get SHA1 fingerprint from Eclipse and Android Studio and After that Generate Google+ Authentication API key from Google developers. So now in this tutorial we are going to do the next most important steps to insert Google+ Login inside android application project. So here is the complete step by step tutorial for Android Google Plus Login-Sign UP Integration example tutorial – Google+ Login Part-2.

android-project-download-code-button

List of most important steps which is necessary to follow :

  1. Get SHA1 fingerprint form Eclipse or Android studio in Windows.
  2. Generate Google+ Login Developers Key from Google Developers.
  3. Import Google play services library inside Eclipse.
  4. Import Google Play lib into Our current project.
  5. Start Coding.

Next step is to import Google Lib into our Existing project in which we are going to add Google+ Login :

1. Create New project in Eclipse.

2. Open properties of your project by simply right click on your project name Or the short cut key to open properties is ALT + Enter.

3. Now select Android -> Add .

library-add

4. Now Select google-play-services_lib .

google-lib

Here you go now Google library is successfully inserted inside your Android project. Next step is START CODING.

Android Google Plus Login-Sign UP Integration example tutorial – Google+ Login Part-2.

Please add these permissions inside your AndroidManifest.xml file .

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

Please add Meta Data inside AndroidManifest.xml file .

 <meta-data
 android:name="com.google.android.gms.version"
 android:value="@integer/google_play_services_version" />

Code for MainActivity,java file.

 package com.googlepluslogin_android_examples.com;
import java.io.InputStream;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.plus.Plus;
import com.google.android.gms.plus.model.people.Person;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

 
public class MainActivity extends Activity implements ConnectionCallbacks, OnConnectionFailedListener {

 private static final int RC_SIGN_IN = 0;
 private GoogleApiClient mGoogleApiClient;
 private SignInButton GoogleSignInButton;
 private ImageView profileImageView;
 private TextView GoogleUserName, GoogleUserEmail ;
 private RelativeLayout GoogleUserProfileLayout, GoogleUserLoginLayout ;
 private boolean mIntentInProgress;
 private boolean signedInUser;
 private ConnectionResult mConnectionResult;
 private Button LogoutButton;
 Person ProfileNamePerson;
 String GoogleProfileName, GoogleProfilePicURL, GoogleUserEmailAddress;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 GoogleSignInButton = (SignInButton) findViewById(R.id.GsignIn);
 
 profileImageView = (ImageView) findViewById(R.id.profileImage);
 
 GoogleUserName = (TextView) findViewById(R.id.username);
 
 GoogleUserEmail = (TextView) findViewById(R.id.Email);
 
 LogoutButton = (Button)findViewById(R.id.logout);
 
 GoogleUserProfileLayout = (RelativeLayout) findViewById(R.id.relativelayout);
 
 GoogleUserLoginLayout = (RelativeLayout) findViewById(R.id.relativelayout2);
 
 mGoogleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(Plus.API, Plus.PlusOptions.builder().build()).addScope(Plus.SCOPE_PLUS_LOGIN).build();
 
 GoogleSignInButton.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 if (!mGoogleApiClient.isConnecting()) {
 signedInUser = true;
 SignInCheck();
 }
 
 }
 });
 
 LogoutButton.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 if (mGoogleApiClient.isConnected()) {
 Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
 mGoogleApiClient.disconnect();
 mGoogleApiClient.connect();
 OnProfilePicUpdateCall(false);
 }
 
 }
 });
 
 }

 private void SignInCheck() {
 if (mConnectionResult.hasResolution()) {
 try {
 mIntentInProgress = true;
 mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
 } catch (SendIntentException e) {
 mIntentInProgress = false;
 mGoogleApiClient.connect();
 }
 }
 }

 @Override
 protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
 switch (requestCode) {
 case RC_SIGN_IN:
 if (responseCode == RESULT_OK) {
 signedInUser = false;

 }
 mIntentInProgress = false;
 if (!mGoogleApiClient.isConnecting()) {
 mGoogleApiClient.connect();
 }
 break;
 }
 }

 @Override
 public void onConnected(Bundle arg0) {
 signedInUser = false;
 Toast.makeText(this, "Connected", Toast.LENGTH_LONG).show();
 GooglePlusProfileInfo();
 }

 @Override
 public void onConnectionFailed(ConnectionResult result) {
 if (!result.hasResolution()) {
 GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this, 0).show();
 return;
 }

 if (!mIntentInProgress) {
 // store mConnectionResult
 mConnectionResult = result;

 if (signedInUser) {
 SignInCheck();
 }
 }
 }
 
 
 private void OnProfilePicUpdateCall(boolean value) {
 
 if (value) {
 
 GoogleUserProfileLayout.setVisibility(View.VISIBLE);
 GoogleUserLoginLayout.setVisibility(View.GONE);

 } else {
 
 GoogleUserLoginLayout.setVisibility(View.VISIBLE);
 GoogleUserProfileLayout.setVisibility(View.GONE);
 }
 }

 private void GooglePlusProfileInfo() {
 try {
 if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
 
 ProfileNamePerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
 
 GoogleProfileName = ProfileNamePerson.getDisplayName();
 
 GoogleUserEmailAddress = Plus.AccountApi.getAccountName(mGoogleApiClient);
 
 GoogleProfilePicURL = ProfileNamePerson.getImage().getUrl();

 GoogleUserName.setText(GoogleProfileName);
 
 GoogleUserEmail.setText(GoogleUserEmailAddress);

 new ProfileImageLoadingClas(profileImageView).execute(GoogleProfilePicURL);
 
 OnProfilePicUpdateCall(true);
 }
 } catch (Exception e) {
 e.printStackTrace();
 }
 }

 @Override
 public void onConnectionSuspended(int cause) {
 
 mGoogleApiClient.connect();
 
 OnProfilePicUpdateCall(false);
 }
 
 private class ProfileImageLoadingClas extends AsyncTask<String, Void, Bitmap> {
 ImageView bitmapImage;
 
 public ProfileImageLoadingClas(ImageView bitmapImg) {
 
 this.bitmapImage = bitmapImg;
 }
 
 protected Bitmap doInBackground(String... urls) {
 String ImageURL = urls[0];
 Bitmap imageIcon = null;
 try {
 InputStream in = new java.net.URL(ImageURL).openStream();
 imageIcon = BitmapFactory.decodeStream(in);
 } catch (Exception e) {
 Log.e("Error", e.getMessage());
 e.printStackTrace();
 }
 return imageIcon;
 }
 
 protected void onPostExecute(Bitmap result) {
 bitmapImage.setImageBitmap(result);
 }
 }
 
 protected void onStart() {
 super.onStart();
 mGoogleApiClient.connect();
 }

 protected void onStop() {
 super.onStop();
 if (mGoogleApiClient.isConnected()) {
 mGoogleApiClient.disconnect();
 }
 }
 
}

Code for activity_main.xml layout file.

 <RelativeLayout 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: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.cameraapp_android_examples.com.MainActivity" >

 <RelativeLayout
 android:id="@+id/relativelayout"
 android:layout_width="fill_parent"
 android:layout_height="300dp"
 android:padding="20dp"
 android:visibility="gone"
 >

 <ImageView
 android:id="@+id/profileImage"
 android:layout_width="100dp"
 android:layout_height="100dp"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true" />

 <TextView
 android:id="@+id/username"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/profileImage"
 android:layout_centerHorizontal="true"
 android:text="UserName"
 android:textSize="17dp"
 android:gravity="center" />

 <TextView
 android:id="@+id/Email"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_below="@+id/username"
 android:layout_marginTop="19dp"
 android:text="User Email"
 android:textSize="17dp"
 android:gravity="center" />

 <Button
 android:id="@+id/logout"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/Email"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="32dp"
 android:text="Logout" />
 
 </RelativeLayout>
 
 <RelativeLayout
 android:id="@+id/relativelayout2"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:padding="20dp" 
 >

 <com.google.android.gms.common.SignInButton
 android:id="@+id/GsignIn"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:textSize="18dp" >
 </com.google.android.gms.common.SignInButton>
 
 </RelativeLayout>

</RelativeLayout>

Code for AndroidManifest.xml file.

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.googlepluslogin_android_examples.com"
 android:versionCode="1"
 android:versionName="1.0" >

 <uses-sdk
 android:minSdkVersion="11"
 android:targetSdkVersion="21" />
 
 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 <uses-permission android:name="android.permission.GET_ACCOUNTS" />
 <uses-permission android:name="android.permission.USE_CREDENTIALS" />

 <application
 android:allowBackup="true"
 android:icon="@drawable/ic_launcher"
 android:label="@string/app_name"
 android:theme="@style/AppTheme" >
 
 <meta-data
 android:name="com.google.android.gms.version"
 android:value="@integer/google_play_services_version" />
 
 <activity
 android:name=".MainActivity"
 android:label="@string/app_name" >
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />

 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
 
 
 </activity>
 </application>

</manifest>

Screenshot :

google-account-login

Click here to download Android Google Plus Login-Sign UP Integration example tutorial – Google+ Login Part-2 project with source code.