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
Go to Firebase Console and create a new project. Enter any name for the project, tick all the boxes and then click on accept.
Click on Android icon to add your app with your project.
Enter the details of the app like app name, package name, and SHA1 signature.
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]
Add these two lines in the dependencies of your App level build.gradle file.
In the last line add
Now in your Project level build.gradle file add this line.
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.
Now paste this code in the OnCreate of first.java
Import all the necessary statements with Alt+Enter and then paste this code in the class Main activity of first.java.
Implement GoogleApiClient.OnConnectionFailedListener in the first activity. You will have to declare these variables.
Now open your second.java and in OnCreate method paste this.
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
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]
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']
[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);
}
});]
[@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>]
[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;]
COMMENTS