Header Ads

Sign in with Google plus in Android Studio within 2 minues

 Sign in with Google plus in Android studio
 Sign in with Google plus in Android studio
This is a necessary component of any app having Login functionality. There is a lot of tutorial over internet about this topic but trust me this post is going to be best and shortest tutorial for Google plus Integration in android.So Now time to implement this tutorial Let's start with coding like every post I will make module of steps for complete tutorial and then we will follow all the steps.


  • Create new Project in your Android studio.
  • Create new project on Google Developer console and enable Google sign In Api. 
  • Download googel-services.json file and configure it in your project.
  • Add Playservices dependency in build.gradle.
  • Add Internet Permission in manifest file.
  • Add Google SignIn Button in your activity_main.xml file  also add a Textview and a logout button.
  • Register both button and TextView in MainActivity.java.
  • Build GoogleSignInOptions in MainActivity and also add GoogleApiClient in MainActivity.
  • Call GoogleSignIn Api on click of SignIn button and update ui in onActivityResult method.
  • Add SignOut button functionality.


1.Create new project in your Android studio. Go to File>>New>>New project and select an empty Activity from Template and leave every thing as default.



2.Create new project on Google Developer console and enable Google sign In Api. This is very simple step just click on Google Developer console  and click on Get a Configuration File after clicking on this a new page will open like below image.


Fill App name and package name here and click on continue after this on new page add your system SHA1 key and Enable Google SignIn like below image.


After this scroll page to bottom and clcik on continue and on next page download googel-services.json like below image.



and finally our first and lengthy step is completed.

3.This step is very easy and short in this step we will copy downloaded googel-services.json file from your download folder and paste inside your app directory of project.

Go to YourProject>>app>> and paste your downloaded file here like below image.

4.This is very simple step open build.gradle file of project module and add playservices dependency file under dependencies like below image.



Now everything is set except one thing update ui in signOut method and setText to blank and you are done.

Here is my complete MainActivity.java file.

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
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.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, GoogleApiClient.OnConnectionFailedListener {
TextView tv_username;
GoogleApiClient mGoogleApiClient;
private static final int RC_SIGN_IN = 9001;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_username= (TextView) findViewById(R.id.tv_username);
//Register both button and add click listener
findViewById(R.id.sign_in_button).setOnClickListener(this);
findViewById(R.id.btn_logout).setOnClickListener(this);
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
}
@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(@NonNull ConnectionResult connectionResult) {
}
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
private void signOut() {
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
tv_username.setText("");
}
});
}
@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();
tv_username.setText(getString(R.string.signed_in_fmt, acct.getDisplayName()));
} else {
// Signed out, show unauthenticated UI.
// updateUI(false);
}
}
}
view rawMainActivity.java hosted with ❤ by GitHub
Still facing any problem comment below.

from : http://www.androidwarriors.com/

No comments:

Powered by Blogger.