Code for adding google sign in

1. Create a new project in android studio and name it as “GoogleSignIn”. Then, create an activity SignInActivity and its layout “activity_signin.

activity_signin.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: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="google.googlesignin.SignInActivity">

<TextView
  android:id="@+id/tv_username"
  android:layout_marginTop="50dp"

 android:layout_centerHorizontal="true"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  />
  <TextView
  android:id="@+id/tv_Email"
  android:layout_marginTop="100dp"

 android:layout_centerHorizontal="true"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  />

<com.google.android.gms.common.SignInButton
  android:layout_centerInParent="true"
  android:id="@+id/sign_in_button"
  android:layout_width="120dp"
  android:layout_height="wrap_content" />

<Button
  android:id="@+id/btn_logout"
  android:layout_centerHorizontal="true"
  android:layout_alignParentBottom="true"
  android:text="Sign out"
  android:layout_marginBottom="50dp"
  android:layout_width="120dp"
  android:layout_height="50dp" />

</RelativeLayout>

SignInActivity.java

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
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.ResultCallback;
import com.google.android.gms.common.api.Status;
import org.w3c.dom.Text;
public class SignInActivity extends Activity implements View.OnClickListener, GoogleApiClient.OnConnectionFailedListener {
  TextView tv_username, tv_Email;
  Button signOutbutton;
  SignInButton signInbutton;
  GoogleApiClient mGoogleApiClient;
  private static final int RC_SIGN_IN = 9001;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.signinpage);

  tv_username= (TextView) findViewById(R.id.tv_username);
  tv_Email = (TextView) findViewById(R.id.tv_Email);
  //Register both button and add click listener
  signInbutton = (SignInButton) findViewById(R.id.sign_in_button);
  signOutbutton = (Button) findViewById(R.id.btn_logout);
  signInbutton.setOnClickListener(this);
  signOutbutton.setOnClickListener(this);
  GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
  .requestEmail()
  .build();

mGoogleApiClient = new GoogleApiClient.Builder(this)

.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
  .build();

}
  protected void onStart(){
  super.onStart();
  signOutbutton.setVisibility(View.GONE);
  }

@Override
  public void onClick(View v) {

switch (v.getId()){
  case R.id.sign_in_button:

signIn();

break;
  case R.id.btn_logout:

signOut();

break;
  }

}

@Override
  public void onConnectionFailed(ConnectionResult connectionResult) {
  // Toast.makeText(SignInActivity.this, "Connection Failed", Toast.LENGTH_SHORT).show();
  }

private void signIn() {
  mGoogleApiClient.connect();
  Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
  startActivityForResult(signInIntent, RC_SIGN_IN);

signOutbutton.setVisibility(View.VISIBLE);
  signInbutton.setVisibility(View.GONE);
  }

private void signOut() {
  Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
  new ResultCallback<Status>() {
  @Override
  public void onResult(Status status) {
  tv_username.setText("");
  tv_Email.setText("");

}
  });

signOutbutton.setVisibility(View.GONE);
  signInbutton.setVisibility(View.VISIBLE);
  }

@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) {

if (result.isSuccess()) {
  // Signed in successfully, show authenticated UI.
  GoogleSignInAccount acct = result.getSignInAccount();
  /*Intent i = new Intent(SignInActivity.this, ThirdActivity.class);
  startActivity(i);*/
  tv_username.setText(getString(R.string.signed_in_fmt, acct.getDisplayName()));
  tv_Email.setText(acct.getEmail());
  } else {
  // Signed out, show unauthenticated UI.
  // updateUI(false);
  }
  }
  }

2. Now, add the plugin and the google play services ‘com.google.android.gms:play-services:8.3.0’ library in your app level gradle file.

apply plugin: 'com.android.application'
 apply plugin: 'com.google.gms.google-services'

dependencies {
  compile fileTree(dir: 'libs', include: ['*.jar'])
  compile 'com.android.support:appcompat-v7:24.0.0-alpha2'
  compile 'com.google.android.gms:play-services:8.3.0'

}

3. Add the classpath ‘com.google.gms:google-services:2.0.0-alpha3’ in your project level gradle file(GoogleSignIn gradle file in this case).

dependencies {
  classpath 'com.android.tools.build:gradle:1.0.0'
  classpath 'com.google.gms:google-services:2.0.0-alpha3'
  // NOTE: Do not place your application dependencies here; they belong
  // in the individual module build.gradle files
  }

4. Download the google-services.json file by following these steps and place the file inside your /app directory.

5. Also, add the following permissions in your AndroidManifest.xml

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

 <uses-permission android:name="android.permission.USE_CREDENTIALS" /> <!-- To retrieve the account name (email) as part of sign-in: -->
  <uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
  <uses-permission android:name="android.permission.READ_PROFILE" />
  <uses-permission android:name="android.permission.READ_CONTACTS" />

6. Include the meta data tag inside your application tag in your AndroidManifest.xml.

<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=".SignInActivity" >
  <intent-filter>
  <action android:name="android.intent.action.MAIN" />

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


admin Author

Leave a Reply

Your email address will not be published. Required fields are marked *