Skip to content
TrinityTuts TrinityTuts

Android, Php, Web Designing best tutorial

Primary Menu
  • Home
  • Useful Tips & Codes
  • Latest Tech
  • Video Tutorials
  • Looking for help?
  • Youtube Thumbnail Extractor
  • Home
  • google
  • SignIn with Google in your Android Application

SignIn with Google in your Android Application

Posted On : December 20, 2019 Published By : Aneh Thakur

Last updated on October 1st, 2020 at 12:27 pm

In this post, I will explain to you how we implement Google Sign In and Sign Up in our Android Application in simple steps. In my last post, I explained how we can configure Google Sign Up in PHP Application.

Prerequisites

Before we start integrating Google Sign Up in our Android you need to have a google account to create a project in Google console and also have good knowledge of web applications to save register user into your database you can read this post to send data to the server from Android Application.

Creating and configuring a project in Google Console

You can create a new project following below simple steps.

Step 1. Go to Google Console and login to your Google Account.

Step 2. Create a new project in the console as shown in the below image.

New Google project in Google console

Step 3. Now go to APIs & service menu clicks the OAuth consent screen and add the required information.

Google Console create api for web application

Step 4. Now we need to create credentials go to the left menu bar and click on the credentials menu as and select OAuth Client ID as shown in the below image.

Step 5. Now go to OAuth Client and fill the required details.

Add Google Sign up in Android

You can generate SHA1 using a terminal, Open terminal and run this command keytool -list -v -alias androiddebugkey -keystore ~/.android/debug.keystore and the password for the development key is android. 

Android Google SIgnup Implementation

Google SignIn integration Android Application

Now once we have our required key we start integrating Google Auth in our Application.

Step 1. Open Android Studio and create a new Android project or open existing project.

Step 2. Now we need to add Google play service to Implement Google Auth in our application open Project build.gradle file and add below code in repositories as shown in the below code.

buildscript {
  repositories {
    google()
    jcenter()
}

Step 3. Now open app build.gradle file and load Google play service dependency

dependencies {
    /** Other dependency **/
    implementation 'com.google.android.gms:play-services-auth:17.0.0'
}

Add above dependency and Sync your project to install and play dependencies in the project.

Step 4. Now open your activity_main.xml file and add Google Sign in button on the screen.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

    <com.google.android.gms.common.SignInButton
    android:id="@+id/sign_in_button"
    android:layout_width="wrap_content"
    android:layout_gravity="center|center_vertical"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    />
</FrameLayout>

Login with Google Auth Android

Step 5. Now open your MainActivity.java file and add clicklistner to button and handle the response from the server.

Initialize some global variable which we need for Google signup

SignInButton signup;
GoogleSignInClient mGoogleSignInClient;
int google_login= 100;

Now inside onCreate method find the button and initialize GoogleSignInClient as shown below code. Note you need to add your Google Client ID here inside requestIdToken().

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

    signup = (SignInButton)findViewById(R.id.signup);
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken("YOUR_GOOGLE_CLIENT_ID")
            .requestEmail()
            .build();
    mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

    signup.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent signInIntent = mGoogleSignInClient.getSignInIntent();
            startActivityForResult(signInIntent, google_login);
        }
    });
}

Now when user click on Sign In button and select account we get user information inside onActivityResult and when we get user information successfully we can display that on-screen as shown in below code

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    Log.e("called", "called");
    if (requestCode == google_login) {
        Task task = GoogleSignIn.getSignedInAccountFromIntent(data);
        handleSignInResult(task);
    }
}

private void handleSignInResult(Task completedTask) {
    try {
        GoogleSignInAccount account = completedTask.getResult(ApiException.class);
        // Disaplay data on your screen
        Log.e("email", account.getEmail());
        Log.e("name", account.getDisplayName());
        Log.e("id", account.getId());

    } catch (ApiException e) {
        // The ApiException status code indicates the detailed failure reason.
        // Please refer to the GoogleSignInStatusCodes class reference for more information.
        Log.e("signInResult", ":failed code=" + e.getStatusCode());
    }
}

Signup with Google in your Android Application

 

https://github.com/anehkumar/AndroidGoogleSignIn

Now with the help of the above code, you get some information from Google Auth now you need to send this data to the server and save it.  Hope this post helps you understand how to implement Google Auth in your Android Application. 🙂

 

You can also read:- SignIn with Google in PHP web application

Share this:

  • LinkedIn
  • Pocket
  • Email
  • Telegram
  • Print
  • Twitter
  • Tumblr
  • Reddit
  • Pinterest
  • More
  • Facebook
Posted in: Android, googleTagged : Google Android SignIn,Google Android SignIn Application,Login With Google SignIn in Android Application

Post navigation

Redirect www site traffic to non-www website in NGINX
Display WordPress last update time in blog post

Advertisement




Follow Us On Facebook

Follow Us On Facebook

Usefull Tips

Get Started with TypeScript: A Beginner’s Guide with Code Samples

  • Fixing 413 request entity too large PHP NGINX server
    Source: tips Published on 2020-11-26
  • Steps to Install NodeJs LTS on Ubuntu OS
    Source: tips Published on 2020-11-19
  • Get Android Advertisement ID (AAID) programmatically
    Source: tips Published on 2020-10-19

Advertisement —-




Quick Links

  • Useful Tips & Codes
  • Youtube Channel
  • Privacy Policy
  • Latest Tech Tutorial
  • Help
  • Wholesale sarees online

ABOUT

Trinity tuts is one of the best place for beginners to learn android, php, google and web design tutorial and tips. - Maintained by Aneh Thakur

Subscribe to Blog via Email

Join 4,785 other subscribers
Copyright All right reserved
TrinityTuts TrinityTuts

Android, Php, Web Designing best tutorial

Theme: Infinity Mag by ThemeinWP