How to add Google Sign in to android app

To add Google Sign in to the app first Go to firebase console and create a new Project

      Well, nowadays each and every app has a Google Sign in. Even if you do not use a back end server for the app, you can add Google sign in to the app. Adding Google Sign in to the app has two big advantages. Your app remains secured and your app doesn't need to collect data from the user. You must be wondering how your app remains secure. Let me explain it to you. If your app doesn't allow the user to use the app until the user has signed in through google then your app gets increased security from google. When user signs in through Google not only Google allows the app to access information but Google also checks the key using which the application is signed and checks it with the key in the Google respiratory and if it doesn't match then it doesn't let the user sign in on Google. So without wasting further time, let's start with our Google sign-in integration.

If you find it difficult to view this site in mobile then open this site in desktop mode

How to add Google Sign in to the android app


How to add Google Sign in to android app

Go to Firebase Console and create a new project. Enter any name for the project, tick all the boxes and then click on accept.

How to add Google Sign in to android app

Click on Android icon to add your app with your project.

How to add Google Sign in to android app

Enter the details of the app like app name, package name, and SHA1 signature.

How to add Google Sign in to android app

You will find your SHA1 signature in signingReport of Gradle in Android Studio.

Once you click on Register, the second step is to download the google-services.json file and paste in the directory of your app.

[post_ads]

How to add Google Sign in to android app
Add these two lines in the dependencies of your App level build.gradle file.

[implementation 'com.google.android.gms:play-services-auth:11.6.0'implementation 'com.github.bumptech.glide:glide:3.7.0']

In the last line add

[apply plugin: 'com.google.gms.google-services']
How to add Google Sign in to android app

Now in your Project level build.gradle file add this line.

[classpath 'com.google.gms:google-services:3.1.0']

For Google Sign in method, you will need two activities. One with sign in button and second after sign in. Let's refer to the first activity as first.java and the XML file of first activity as first.xml and similarly second becomes second.java and second.xml.

[post_ads_2]

Copy paste this sign in button in your first.xml.

[<com.google.android.gms.common.SignInButton android:id="@+id/sign_in_button"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="142dp" android:layout_marginTop="129dp" android:layout_marginEnd="142dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />]

Now paste this code in the OnCreate of first.java

[GoogleSignInOptions gso = new GoogleSignInOptions
.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
googleApiClient=new GoogleApiClient.Builder(this)
.enableAutoManage(this,this)
.addApi(Auth.GOOGLE_SIGN_IN_API,gso)
.build();
signInButton=(SignInButton)findViewById(R.id.sign_in_button);
signInButton.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
Intent intent = Auth.GoogleSignInApi.getSignInIntent(googleApiClient);
startActivityForResult(intent,RC_SIGN_IN);
}
});]




Import all the necessary statements with Alt+Enter and then paste this code in the class Main activity of first.java.

[@Overridepublic void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Overrideprotected void onStart() {
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
if (account!=null){
gotoProfile();
}
super.onStart();
}  @Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if(requestCode==RC_SIGN_IN){GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);handleSignInResult(result);}  private void handleSignInResult(GoogleSignInResult result){ if(result.isSuccess()){
gotoProfile();
}else{
Toast.makeText(getApplicationContext(), "Sign in cancel",
 Toast .LENGTH_LONG).show();
}
}
private void gotoProfile(){ Intent intent=new Intent(MainActivity.this, Second.class);
startActivity(intent);
}]

Implement GoogleApiClient.OnConnectionFailedListener in the first activity. You will have to declare these variables.

[SignInButton signInButton;
private GoogleApiClient googleApiClient;
private static final int RC_SIGN_IN = 1;]

Now paste this in your second.xml

[<LinearLayout android:layout_width="0dp" android:layout_height="0dp" android:layout_marginEnd="8dp" android:orientation="vertical" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"><ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="200dp"/><TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" /><TextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" /><TextView android:id="@+id/textView3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button" /></LinearLayout>]

Now open your second.java and in OnCreate method paste this.

[logoutBtn=(Button)findViewById(R.id.button2);
userName=(TextView)findViewById(R.id.textView);
userEmail=(TextView)findViewById(R.id.textView2);
userId=(TextView)findViewById(R.id.textView3);
profileImage=(ImageView)findViewById(R.id.imageView);
gso =  new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestEmail()
        .build();
googleApiClient=new GoogleApiClient.Builder(this)
        .enableAutoManage(this,this)
        .addApi(Auth.GOOGLE_SIGN_IN_API,gso)
        .build();
logoutBtn.setOnClickListener(new View.OnClickListener() {
    @Override    public void onClick(View view) {
        Auth.GoogleSignInApi.signOut(googleApiClient).setResultCallback(
                new ResultCallback<Status>() {
                    @Override                    public void onResult(Status status) {
                        if (status.isSuccess()){
                            gotoMainActivity();
                        }else{
                            Toast.makeText(getApplicationContext(),"Session not close",Toast.LENGTH_LONG).show();
                        }
                    }
                });
    }
});] 

Now in your Second activity class paste this.

[@Overrideprotected void onStart() { super.onStart();
OptionalPendingResult<GoogleSignInResult> opr= Auth.GoogleSignInApi.silentSignIn(googleApiClient);
if(opr.isDone()){
GoogleSignInResult result=opr.get();
handleSignInResult(result);
}else{
opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
@Override public void onResult(@NonNull GoogleSignInResult googleSignInResult) {
handleSignInResult(googleSignInResult);
}
});
}
}
private void handleSignInResult(GoogleSignInResult result){
if(result.isSuccess()){
GoogleSignInAccount account=result.getSignInAccount();
userName.setText(account.getDisplayName());
userEmail.setText(account.getEmail());
userId.setText(account.getId());
try{
Glide.with(this).load(account.getPhotoUrl()).into(profileImage);
}catch (NullPointerException e){
Toast.makeText(getApplicationContext(),"image not found",Toast.LENGTH_LONG).show();
}
}else{
gotoMainActivity();
}
}
private void gotoMainActivity(){
Intent intent=new Intent(this,first.class);
startActivity(intent);
}
@Overridepublic void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}]

Implement GoogleApiClient.OnConnectionFailedListener in the second activity.
Now declare the variables.

[Button logoutBtn;
TextView userName,userEmail,userId;
ImageView profileImage;
private GoogleApiClient googleApiClient;
private GoogleSignInOptions gso;]

Well, compile and run your app and if you have perfectly done everything your app should run without any errors.

How to add Google Sign in to android app

COMMENTS

BLOGGER
Name

Affiliate marketing tutorial,2,Android,60,Android Development,22,Android tutorials,41,Blogging tactics,8,Deals,1,DSA,4,Elite Youtube Tutorial,4,Firebase,1,Flutter,4,Fuchsia,1,Hosting Guides,3,Leetcode,4,Miscellaneous,10,Mobile Gaming,17,NTN,94,Problogging,7,PUBG,16,Reviews,1,SEO Tutorial,5,Start a blog,9,Start a channel,6,Top 5,4,Travel Blogging,4,Windows,12,Windows Security,4,Windows tutorials,7,Wordpress Tutorials,4,Youtube Tutorials,11,
ltr
item
A Passionate Developers Creation: How to add Google Sign in to android app
How to add Google Sign in to android app
To add Google Sign in to the app first Go to firebase console and create a new Project
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYwsdSb7ytnIVD_1nSubmCcNxFsdrUC_5tSdx2vrFupE4NYHDyFxaWO3vIwREw9iris51xj-Icb3tvWMUb69zuglM4UEcIXF7M6jo2G3ZTz479Z3-UMAKhry0Xk1h54NJMuWUmIkLVuAc/s320/Annotation+2019-06-29+223751.jpg
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYwsdSb7ytnIVD_1nSubmCcNxFsdrUC_5tSdx2vrFupE4NYHDyFxaWO3vIwREw9iris51xj-Icb3tvWMUb69zuglM4UEcIXF7M6jo2G3ZTz479Z3-UMAKhry0Xk1h54NJMuWUmIkLVuAc/s72-c/Annotation+2019-06-29+223751.jpg
A Passionate Developers Creation
https://www.apdevc.com/2019/06/how-to-add-google-sign-in-to-android-app.html
https://www.apdevc.com/
https://www.apdevc.com/
https://www.apdevc.com/2019/06/how-to-add-google-sign-in-to-android-app.html
true
3690156770086819421
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content