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.
Step 3. Now go to APIs & service menu clicks the OAuth consent screen and add the required information.
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.
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.
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>
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()); } }
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