Google Sign In in Android Studio example | How To Integrate Google Login In android App
In this article we will see how to integrate google login in android application and we will also see the alternative of startactivityforresult as you may know google had deprecated startactivityforresult method so we will also cover it.
This Is the part 4 of our youtube tutorial series in android development In last part we covered how to login with email and password using firebase authentication so in this video tutorial we are gonna see how to Integrate Google Login in just 7 Steps.
Step 1.
So to Integrate Google Login In your android project first of all you will need to add this library in your app level build.gradle file and sync the project
implementation 'com.google.android.gms:play-services-auth:20.0.1'
Step 2.
In this step we will add a SignInButton In our xml file and this signinbutton class is available in above library so we just have to use it.
sign in button xml code.
<com.google.android.gms.common.SignInButton
android:id="@+id/sign_in_with_google"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Step 3.
In this step we will initialize all views so we have only one view in our activity_main.xml file so we will initialize it this way.
SignInButton signInButton = findViewById(R.id.sign_in_with_google);
Step 4.
In this step we will create instance of GoogleSignInClient & GoogleSignInOptions class
private GoogleSignInClient mGoogleSignInClient;
/// write this code in onCreate Activity Method
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
Step 5.
In this Step we will discuss how start an activity for result. as i already discuess above startActivityForResult(intent,REQUEST_CODE); method has been deprecated so we will what is the alternative of this method
so to start and activity for result we will first create a internal class GetGoogleLoginDetails that will extend with ActivityResultContract.
public class GetGoogleLoginDetails extends ActivityResultContract<Integer, Intent> {
@NonNull
@Override
public Intent createIntent(@NonNull Context context, @NonNull Integer requestCode) {
// in this overrirde method simply return required intent
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
return signInIntent;
}
@Override
public Intent parseResult(int resultCode, @Nullable Intent result) {
// in this overrirde method you can filter results
if (resultCode != Activity.RESULT_OK || result == null) {
return null;
}
return result;
}
}
Step 6.
In this step we will create ActivityResultLauncher make sure to add ActivityResultLauncher in your oncreate method of an
activity or a fragment class. ActivityResultLauncher will give results inside onActivityResult Method and we will get intent result as a parameter of this metod
we will use this intent to get GoogleSignInAccount Details as you can see in code below.
ActivityResultLauncher activity_result_launcher = registerForActivityResult(new GetGoogleLoginDetails(), new ActivityResultCallback() {
@Override
public void onActivityResult(Intent result) {
Task task = GoogleSignIn.getSignedInAccountFromIntent(result);
try {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
Log.d(TAG, "firebaseAuthWithGoogle:" + account.getId());
Toast.makeText(MainActivity.this, "Data Recieved " + account.getDisplayName(), Toast.LENGTH_SHORT).show();
firebaseAuthWithGoogle(account.getIdToken());
} catch (ApiException e) {
// Google Sign In failed, update UI appropriately
Log.w(TAG, "Google sign in failed", e);
}
}
});
Step 7.
Add click listener on sign in button and call ActivityResultLauncher lanch method inside this listener.
signInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
activity_result_launcher.launch(12);
}
});
As you can see in step 6 we called firebaseAuthWithGoogle(account.getIdToken()); method but we have not created it yet
so in this step we will se how this method will work.
In firebaseAuthWithGoogle method simply use idToken and create AuthCredential and then we use credential to login user to firebase
if you are not using firebase auth to manage users account then you don't need to write below code you can simply get signed in user details
in side onactivity result method by calling account.getEmail() and then store this email on your server .
private void firebaseAuthWithGoogle(String idToken) {
AuthCredential credential = GoogleAuthProvider.getCredential(idToken, null);
FirebaseAuth.getInstance().signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithCredential:success");
FirebaseUser user = mAuth.getCurrentUser();
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCredential:failure", task.getException());
// updateUI(null);
}
}
});
}
What's Your Reaction?






