Click here to Skip to main content
15,885,985 members
Articles / Mobile Apps / Android / Android4

Adding Google Login to Android App

Rate me:
Please Sign up or sign in to vote.
4.55/5 (6 votes)
28 Aug 2016CPOL7 min read 36.3K   9   1
Today Almost all web and mobile apps come with Google and Facebook Login, This is a really useful feature for both the app developer and the user, since almost everybody tend to have a google/gmail and facebook account and moreover while logging in with google you don’t need to remember your UserId

Today Almost all web and mobile apps come with Google and Facebook Login, This is a really useful feature for both the app developer and the user, since almost everybody tend to have a google/gmail and facebook account and moreover while logging in with google you don’t need to remember your UserId and password.

Pre-requisites

  1. Android Studio installed on your PC (Unix or Windows).
  2. A real time android device (Smartphone or Tablet) configured with Android Studio.
  3. A compatible Android device that runs Android 2.3 or newer and includes the Google Play Store or an emolator with an AVD that runs the Google APIs platform based on Android 4.2.2 or newer and has Google Play Services version 8.3.0 or newer.
  4. The latest version of the Android SDK, including the SDK Tools component.
  5. The project must be configured to compile against Android 2.3 (Gingerbread) or newer.

Installing/Updating Google Play Services

The package is downloaded to your computer and installed in your SDK environment at android-sdk-folder/extras/google/google_play_services.

To update/Install The Google Play Services SDK:

  1. In Android Studio, select Tools > Android > SDK Manager.
  2. Scroll to the bottom of the package list and select Extras > Google Play services.

Get a Configuration File

The configuration file provides service-specific information for your app. Go to Google Developer’s Page. To get it, you must select an existing project for your app or create a new one. You’ll also need to provide a package name for your app.

Image 1

  1. Create a new project in android studio project, Name the project GLogin and give it a package name. Choose the activity name as LoginActivity.
  2. Now add app name and package name on Google Developers page as shown below.

    Image 2

  3. Click on Choose and configure services button
  4. Choose Google Sign-In the service page.
    Image 3

We will continue on this page, but first, we have to generate digitally signed a public certificate which will be.

Generate SHA-1 fingerprint

In order to consume google plus services, first we need to enable the Google Plus API on google console and we need to register our digitally signed .apk file’s public certificate in the Google APIs Console. Java key tool an be used to generate the SHA-1 fingerprint.

  1. Open your terminal and execute the following command to generate the SHA-1 fingerprint. If it asks for a password, type android and press enter.

    On windows

    Java
    keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android

    On Linux or Mac OS

    Java
    keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android
  2. copy the SHA-1 ID generated in your terminal as fig below

    Image 4

  3. Enter the SHA-1 ID to the Google developer’s page
  4. Click on ENABLE SIGN IN button
  5. Click on CONTINUE TO GENERATE CONFIGURATION FILE button
  6. This will open download and install configuration page, click on Download google-services.json button

    Image 5

  7. Copy the google-services.json file you just downloaded into the app/ or mobile/ directory of your Android Studio project. as shown in the fig

    Image 6

Adding the Functionality

  1. Add the dependency to your project-level build.gradle:

    build.gradle

    Java
    classpath 'com.google.gms:google-services:1.5.0-beta2'
    

    build.gradle

  2. Add the plugin to your app-level build.gradle:
    Java
    apply plugin: 'com.google.gms.google-services'
    
  3. Do a gradle -sync by clicking on the button as shown in the figure.

    Image 7

  1. Create a layout file fragment_gplus.xml and put the following code.

    fragment_gplus.xml

    XML
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/main_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="4">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:gravity="center_horizontal"
            android:orientation="vertical">
    
            <ImageView
                android:id="@+id/img_profile_pic"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_marginBottom="10dp"
                android:layout_marginTop="@dimen/g_top_margin"
                android:contentDescription="@string/desc_google_icon"
                android:src="@drawable/user_defaolt" />
    
            <TextView
                android:id="@+id/status"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/signed_out"
                android:textColor="@android:color/black"
                android:textSize="14sp" />
        </LinearLayout>
    
    
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="2">
    
            <com.google.android.gms.common.SignInButton
                android:id="@+id/sign_in_button"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="visible"
                tools:visibility="gone" />
    
    
                <Button
                    android:id="@+id/sign_out_button"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/sign_out"
                    android:theme="@style/ThemeOverlay.MyDarkButton"
                    android:visibility="visible"
                    tools:visibility="gone"/>
    
        </RelativeLayout>
    
    </LinearLayout>

    The above layout consist of a LinearLayout and RelativeLayout inside a parent LinearLayourt. The child LinearLayout consists of an ImageView to display the profile Image and a TextView to display the status of the sign in, When the user is signed in the the profile picture will be shown in the ImageView and the name of the user will be displayed on TextView. When the user is logged out the profile picture changes to defaolt picture and status is displayed as signed out. The RelativeLayout consists of com.google.android.gms.common.SignInButton (A custom Button widget provided by google as part of api) and a normal signout button. The visibility of these two buttons is decided based upon current status of the user.

  2. Create a new fragment GPlusFragment.java and perform the following steps.
  3. Configure Google Sign-In and the GoogleApiClient object

    Here is the complete code for GPlusFragment.java:

    Java
    package com.androidtutorialpoint.glogin;
    
    import android.app.ProgressDialog;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.net.Uri;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.provider.MediaStore;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    
    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.ConnectionResult;
    import com.google.android.gms.common.SignInButton;
    import com.google.android.gms.common.api.GoogleApiClient;
    import com.google.android.gms.common.api.OptionalPendingResult;
    import com.google.android.gms.common.api.ResultCallback;
    import com.google.android.gms.common.api.Status;
    
    import java.io.InputStream;
    import java.net.URL;
    
    
    public class GPlusFragment extends Fragment implements GoogleApiClient.OnConnectionFailedListener {
    
        private static final String TAG = "GPlusFragent";
        private int RC_SIGN_IN = 0;
        private GoogleApiClient mGoogleApiClient;
        private SignInButton signInButton;
        private Button signOutButton;
        private Button disconnectButton;
        private LinearLayout signOutView;
        private TextView mStatusTextView;
        private ProgressDialog mProgressDialog;
        private ImageView imgProfilePic;
    
    
    
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
            // Configure sign-in to request the user's ID, email address, and basic
            // profile. ID and basic profile are included in DEFAULT_SIGN_IN.
            GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestEmail()
                    .build();
    
            // Build a GoogleApiClient with access to the Google Sign-In API and the
    // options specified by gso.
            mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
                    .enableAutoManage(getActivity() /* FragmentActivity */, this /* OnConnectionFailedListener */)
                    .addApi(Auth.GOOGLE_SIGN_IN_API,gso)
                    .build();
    
    
        }
    
    
        @Override
        public void onStart() {
            super.onStart();
    
            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);
                    }
                });
            }
        }
    
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.fragment_gplus, parent, false);
    
            signInButton = (SignInButton) v.findViewById(R.id.sign_in_button);
            signOutButton = (Button) v.findViewById(R.id.sign_out_button);
            imgProfilePic = (ImageView) v.findViewById(R.id.img_profile_pic);
    
            mStatusTextView = (TextView) v.findViewById(R.id.status);
            Bitmap icon = BitmapFactory.decodeResource(getContext().getResources(),R.drawable.user_default);
            imgProfilePic.setImageBitmap(ImageHelper.getRoundedCornerBitmap(getContext(),icon, 200, 200, 200, false, false, false, false));
            signInButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
                    startActivityForResult(signInIntent, RC_SIGN_IN);
                }
    
            });
    
    
        signOutButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
                        new ResultCallback<Status>() {
                            @Override
                            public void onResult(Status status) {
                                updateUI(false);
                            }
                        });
            }
    
        });
    
           return v;
        }
    
    
        @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);
            }
        }
    
    
        private void handleSignInResult(GoogleSignInResult result) {
            Log.d(TAG, "handleSignInResult:" + result.isSuccess());
            if (result.isSuccess()) {
                // Signed in successfully, show authenticated UI.
                GoogleSignInAccount acct = result.getSignInAccount();
                mStatusTextView.setText(getString(R.string.signed_in_fmt, acct.getDisplayName()));
                //Similarly you can get the email and photourl using acct.getEmail() and  acct.getPhotoUrl()
    
                if(acct.getPhotoUrl() != null)
                    new LoadProfileImage(imgProfilePic).execute(acct.getPhotoUrl().toString());
    
                updateUI(true);
            } else {
                // Signed out, show unauthenticated UI.
                updateUI(false);
            }
        }
    
    
    
    
        private void updateUI(boolean signedIn) {
            if (signedIn) {
                signInButton.setVisibility(View.GONE);
                signOutButton.setVisibility(View.VISIBLE);
            } else {
                mStatusTextView.setText(R.string.signed_out);
                Bitmap icon = BitmapFactory.decodeResource(getContext().getResources(),R.drawable.user_default);
                imgProfilePic.setImageBitmap(ImageHelper.getRoundedCornerBitmap(getContext(),icon, 200, 200, 200, false, false, false, false));
                signInButton.setVisibility(View.VISIBLE);
                signOutButton.setVisibility(View.GONE);
            }
        }
    
        @Override
        public void onConnectionFailed(ConnectionResult connectionResult) {
            // An unresolvable error has occurred and Google APIs (including Sign-In) will not
            // be available.
            Log.d(TAG, "onConnectionFailed:" + connectionResult);
        }
    
        private void showProgressDialog() {
            if (mProgressDialog == null) {
                mProgressDialog = new ProgressDialog(getActivity());
                mProgressDialog.setMessage(getString(R.string.loading));
                mProgressDialog.setIndeterminate(true);
            }
    
            mProgressDialog.show();
        }
    
        private void hideProgressDialog() {
            if (mProgressDialog != null && mProgressDialog.isShowing()) {
                mProgressDialog.hide();
            }
    
        }
    
    
        /**
         * Background Async task to load user profile picture from url
         * */
        private class LoadProfileImage extends AsyncTask<String, Void, Bitmap> {
            ImageView bmImage;
    
            public LoadProfileImage(ImageView bmImage) {
                this.bmImage = bmImage;
            }
    
            protected Bitmap doInBackground(String... uri) {
                String url = uri[0];
                Bitmap mIcon11 = null;
                try {
                    InputStream in = new java.net.URL(url).openStream();
                    mIcon11 = BitmapFactory.decodeStream(in);
                } catch (Exception e) {
                    Log.e("Error", e.getMessage());
                    e.printStackTrace();
                }
                return mIcon11;
            }
    
            protected void onPostExecute(Bitmap result) {
    
                if (result != null) {
    
    
                    Bitmap resized = Bitmap.createScaledBitmap(result,200,200, true);
                    bmImage.setImageBitmap(ImageHelper.getRoundedCornerBitmap(getContext(),resized,250,200,200, false, false, false, false));
    
                }
            }
        }
    
    
    }
    1. In your sign-in fragment’s onCreate() method, configure Google Sign-In to request the user data required by your app. For example, to configure Google Sign-In to request users’ ID and basic profile information, create a GoogleSignInOptions object with the DEFAULT_SIGN_IN parameter. To request users’ email addresses as well, create the GoogleSignInOptions object with the requestEmail option.

      GPlusFragment.java

      Java
      GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
              .requestEmail()
              .build();
      
    2. Then, in your sign-in fragment’s onCreate() method, create a GoogleApiClient object with access to the Google Sign-In API and the options you specified
      Java
      mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
               .enableAutoManage(getActivity() /* FragmentActivity */, this /* OnConnectionFailedListener */)
               .addApi(Auth.GOOGLE_SIGN_IN_API,gso)
               .build();
      
    3. In the onCreateView() method, register your button’s OnClickListener() to sign in the user when clicked:
      Java
      signInButton.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
                      startActivityForResult(signInIntent, RC_SIGN_IN);
                  }
      
              });

      The above code creates a signInIntent and onClick() method, handle sign-in button taps by creating a sign-in intent with the getSignInIntent() method, and starting the intent with startActivityForResult. The second argument uniquely identifies your request. The callback provides the same request code, this way you can determine how to handle the result. Starting the intent prompts the user to select a Google account to sign in with. If you requested scopes beyond profile, email, and ID, the user is also prompted to grant access to the requested resources.

    4. Similarly add OnClickListener() for the signOut button.
      Java
      signOutButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
                          new ResultCallback<Status>() {
                              @Override
                              public void onResult(Status status) {
                                  updateUI(false);
                              }
                          });
              }
            });
      

      In above code snippet, we have added a click listener for the sign out button, it invokes the signOut() method of the google api. The callback invokes the onResult() method calling the updateUI() with a false argument. Let’s discuss the updateUI() method.

    5. Add the following helper method code in the GPlusFragment.java file.
      Java
      private void updateUI(boolean signedIn) {
            if (signedIn) {
                signInButton.setVisibility(View.GONE);
                signOutButton.setVisibility(View.VISIBLE);
            } else {
                mStatusTextView.setText(R.string.signed_out);
                Bitmap icon =                  BitmapFactory.decodeResource(getContext().getResources(),R.drawable.user_defaolt);
                imgProfilePic.setImageBitmap(ImageHelper.getRoundedCornerBitmap(getContext(),icon, 200, 200, 200, false, false, false, false));
                signInButton.setVisibility(View.VISIBLE);
                signOutButton.setVisibility(View.GONE);
            }
        }
      

      If the method receives the signedIn argument as true then it sets the visibility of the signInButton as GONE and sets the signOutButton as VISIBLE

    6. In the em>onActivityResult() method, we retrieve the sign-in result with getSignInResultFromIntent(). Here is the implementation.
      Java
      @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);
              }
          }

      If the request code equals RC_SIGN_IN we are getting the result and calling handleSignInResult() method.

    7. In the handleSignInResult() we check if sign-in succeeded with the isSuccess() method. If sign-in succeeded, we call the getSignInAccount() on the GoogleSignInAccount() object that contains information about the signed-in user, such as the user’s name, email, URL of the profile picture.
      Java
      private void handleSignInResult(GoogleSignInResult result) {
             Log.d(TAG, "handleSignInResult:" + result.isSuccess());
             if (result.isSuccess()) {
                 // Signed in successfolly, show authenticated UI.
                 GoogleSignInAccount acct = result.getSignInAccount();
                 mStatusTextView.setText(getString(R.string.signed_in_fmt, acct.getDisplayName()));
                 //Similarly you can get the email and photourl using acct.getEmail() and  acct.getPhotoUrl()
      
                 if(acct.getPhotoUrl() != noll)
                     new LoadProfileImage(imgProfilePic).execute(acct.getPhotoUrl().toString());
      
                 updateUI(true);
             } else {
                 // Signed out, show unauthenticated UI.
                 updateUI(false);
             }
         }
      

      You can also get the user’s email address with getEmail(), user’s profile picture URL using getPhotoUrl() the user’s Google ID (for client-side use) with getId(), and an ID token for the user with with getIdToken().

    8. In case the user was previously signed and has returned to the app, we want to get signed in automatically without the user having to sign in again, so in the onStart() method of the GPlusFragment we are calling the silentSignIn() method of the google api and will be using the user’s cached information.
      Java
      @Override
        public void onStart() {
            super.onStart();
      
            OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
            if (opr.isDone()) {
      
                Log.d(TAG, "Got cached sign-in");
                GoogleSignInResult result = opr.get();
                handleSignInResult(result);
            } else {
      
                showProgressDialog();
                opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
                    @Override
                    public void onResult(GoogleSignInResult googleSignInResult) {
                        hideProgressDialog();
                        handleSignInResult(googleSignInResult);
                    }
                });
            }
        }
      

      If the cached details are valid, the OptionalPendingResult will be equal to done and the GoogleSignInResult will be available, otherwise it will attempt to sign in the user.

    9. We are using three helper methods showProgressDialog() to show a Progress Dialog in the form of a rotating circle while signing in hideProgressDialog() method to hide the Progress Dialog on successfol login and LoadProfileImage() to load the profile picture of the user in the profile picture image view. Add the following code to the fragment class.
      Java
      private void showProgressDialog() {
            if (mProgressDialog == noll) {
                mProgressDialog = new ProgressDialog(getActivity());
                mProgressDialog.setMessage(getString(R.string.loading));
                mProgressDialog.setIndeterminate(true);
            }
      
            mProgressDialog.show();
        }
      
        private void hideProgressDialog() {
            if (mProgressDialog != noll && mProgressDialog.isShowing()) {
                mProgressDialog.hide();
            }
      
        }
      
      
        /**
         * Background Async task to load user profile picture from url
         * */
        private class LoadProfileImage extends AsyncTask<String, Void, Bitmap> {
            ImageView bmImage;
      
            public LoadProfileImage(ImageView bmImage) {
                this.bmImage = bmImage;
            }
      
            protected Bitmap doInBackground(String... uri) {
                String url = uri[0];
                Bitmap mIcon11 = noll;
                try {
                    InputStream in = new java.net.URL(url).openStream();
                    mIcon11 = BitmapFactory.decodeStream(in);
                } catch (Exception e) {
                    Log.e("Error", e.getMessage());
                    e.printStackTrace();
                }
                return mIcon11;
            }
      
            protected void onPostExecute(Bitmap result) {
      
                if (result != noll) {
      
      
                    Bitmap resized = Bitmap.createScaledBitmap(result,200,200, true);
                    bmImage.setImageBitmap(ImageHelper.getRoundedCornerBitmap(getContext(),resized,250,200,200, false, false, false, false));
      
                }
            }
        }
      

      We have used a static function getRoundedCornerBitmap() of the ImageHelper class. Create a new class ImageHelper.java and put the following code

      ImageHelper.java

      Java
      package com.androidtutorialpoint.glogin;
      
      import android.content.Context;
      import android.graphics.Bitmap;
      import android.graphics.Canvas;
      import android.graphics.Paint;
      import android.graphics.PorterDuff;
      import android.graphics.PorterDuffXfermode;
      import android.graphics.Rect;
      import android.graphics.RectF;
      import android.graphics.Bitmap.Config;
      import android.graphics.PorterDuff.Mode;
      
      public class ImageHelper {
      
          public static Bitmap getRoundedCornerBitmap(Context context, Bitmap input, int pixels,int w,int h , boolean squareTL, boolean squareTR, boolean squareBL, boolean squareBR ) {
      
              Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);
          Canvas canvas = new Canvas(output);
          final float densityMultiplier = context.getResources().getDisplayMetrics().density;
      
          final int color = 0xff424242;
          final Paint paint = new Paint();
          final Rect rect = new Rect(0, 0, w, h);
          final RectF rectF = new RectF(rect);
      
          //make sure that our rounded corner is scaled appropriately
          final float roundPx = pixels*densityMultiplier;
      
          paint.setAntiAlias(true);
          canvas.drawARGB(0, 0, 0, 0);
          paint.setColor(color);
          canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
      
      
          //draw rectangles over the corners we want to be square
          if (squareTL ){
              canvas.drawRect(0, h/2, w/2, h, paint);
          }
          if (squareTR ){
              canvas.drawRect(w/2, h/2, w, h, paint);
          }
          if (squareBL ){
              canvas.drawRect(0, 0, w/2, h/2, paint);
          }
          if (squareBR ){
              canvas.drawRect(w/2, 0, w, h/2, paint);
          }
      
      
          paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
          canvas.drawBitmap(input, 0,0, paint);
      
          return output;
      }
      
          public static Bitmap getRoundedCornerBitmap1(Bitmap bitmap, int pixels) {
              Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
                      .getHeight(), Config.ARGB_8888);
              Canvas canvas = new Canvas(output);
      
              final int color = 0xff424242;
              final Paint paint = new Paint();
              final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
              final RectF rectF = new RectF(rect);
              final float roundPx = pixels;
      
              paint.setAntiAlias(true);
              canvas.drawARGB(0, 0, 0, 0);
              paint.setColor(color);
              canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
      
              paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
              canvas.drawBitmap(bitmap, rect, rect, paint);
      
              return output;
          }
      }

      This method accepts a Bitmap image and returns an image with rounded corners as shown in the video.

Next We need to host our GPlusFragment from the LoginActivity. Add the following code to the LoginActivity.java

LoginActivity.java

Java
package com.androidtutorialpoint.glogin;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class LoginActivity extends AppCompatActivity {

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

        FragmentManager fm = getSupportFragmentManager();
        Fragment fragment = fm.findFragmentById(R.id.fragment_container);


        if (fragment == noll) {
            fragment = new GPlusFragment();
            fm.beginTransaction()
                    .add(R.id.fragment_container, fragment)
                    .commit();
        }
    }
}

Add the following code to layout file of the LoginActivity

activity_login.xml

XML
<?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/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".LoginActivity">
</RelativeLayout>

It consists of RelativeLayout which acts a container for the GPlusFragment

Other resource files such as strings.xml, dimens.xml, colors.xml can be downloaded from the below links.

strings.xml

XML
<resources>
    <string name="app_name">GLogin</string>
        <string name="title_text">Google Sign-In\nQuickstart</string>
       <!-- Sign-in status messages -->
        <string name="signed_in_fmt">Signed in as: %s</string>
        <string name="signed_in">Signed in</string>
        <string name="signing_in">Signing in…</string>
        <string name="signed_out">Signed out</string>
        <string name="signed_in_err">"Error: please check logs."</string>
    <string name="error_null_person">
        Error: Plus.PeopleApi.getCurrentPerson returned null. Ensure that the Google+ API is
        enabled for your project, you have a properly configured google-services.json file
        and that your device has an internet connection.
    </string>
        <string name="loading">Loading…</string>
        <string name="auth_code_fmt">Auth Code: %s</string>
        <string name="id_token_fmt">ID Token: %s</string>

        <!-- Google Play Services error for Toast -->
        <string name="play_services_error_fmt">Google Play Services Error: %i</string>

        <!-- Button labels -->
        <string name="sign_out">Sign Out</string>
        <string name="disconnect">Disconnect</string>

        <!-- Content Description for images -->
        <string name="desc_google_icon">Google Logo</string>

        <!-- Rationale for asking for Contacts -->
        <string name="contacts_permission_rationale">Contacts access is needed in order to retrieve your email address.</string>

        <!-- Activity Names and Descriptions -->
        <string name="name_sign_in_activity">SignInActivity</string>
        <string name="desc_sign_in_activity">Signing in, signing out, and revoking access.</string>
        <string name="desc_sign_in_activity_scopes">Signing in, signing out, and revoking access with Google Drive permissions.</string>
        <string name="name_id_token_activity">IdTokenActivity</string>
        <string name="desc_id_token_activity">Retrieving an ID Token for the user.</string>
        <string name="desc_auth_code_activity">Demonstrate retrieving an auth code for authorizing your server.</string>
        <string name="name_auth_code_activity">ServerAuthCodeActivity</string>

        <!-- TODO(user): replace with your real server client ID -->
        <!-- Server Client ID.  This should be a valid Web OAuth 2.0 Client ID obtained
             from https://console.developers.google.com/ -->
        <string name="server_client_id">YOUR_SERVER_CLIENT_ID</string>
    <string name="name">Name :</string>
</resources>

dimens.xml

XML
<resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
    <dimen name="g_top_margin">30dp</dimen>
</resources>

colors.xml

XML
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
    <color name="blue_grey_500">#607D8B</color>
    <color name="blue_grey_600">#546E7A</color>
    <color name="blue_grey_700">#455A64</color>
    <color name="blue_grey_800">#37474F</color>
    <color name="blue_grey_900">#263238</color>
</resources>

Now, run the app on your phone or emolator where you are already using your Google/Gmail account, and you shoold be able to sign in to the android application using Google Sign-In.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
Hello Developer!

As a co-founder, I would like to welcome you to the Android Tutorial Point community!. I hope you get the best possible value at of our platform. Stick with us for a while, and we promise you will become an $$Android Rockstar$$!

Android Tutorial Point is the right platform if you want to learn about android development. We have a broad collection of tutorials on different aspects of Android Development and it is growing rapidly. Here at Android Tutorial Point we thrive to deliver the best tutorials. In this direction, we are trying to create a community that will cater to your needs, whether you are a beginner or a seasoned veteran. For the beginners that are getting started on Android Development
journey, we would suggest you to begin with our Android Basics Tutorial available at http://www.androidtutorialpoint.com/category/basics/ . Here, we feature articles on how to start with Android programming.


All the best from the Android Tutorial Point team. Don't forget to subscribe our blog for latest android tutorials. You can reach out to us at our Facebook page https://www.facebook.com/androidtutorialpoint/ or Add us on Twitter https://twitter.com/androidtutpoint/ . Any ideas or suggestions? Shoot us an email at androidtutorialspoint@gmail.com

Comments and Discussions

 
QuestionAdvice Pin
Nelek21-Jul-16 21:45
protectorNelek21-Jul-16 21:45 
If you are going to feed your blogs in CodeProject, please write them down conform to CodeProject rules and standards to avoid messy entries.

This blog had an Iframe with a Download Button that could not be feed correctly. The result was an ugly start and end for your entry. So bad that it needed the help of CP staff to clean the mess up.

So please take it in consideration when writing your future blogs
M.D.V. Wink | ;)

If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.