
If you want to signup with google account first of all need to create project in console. below we discuss everything.
Creating a Project at Google Developers Console
Log in to Google Plus account and go to this link and click on create project.
Now fill the details and click on create.
Now we need a configuration file for our android app. So go to this link. Select your app we just created on developer console. And write the package name of your android studio project we created. Finally choose country and click on continue.
In the next screen you will asked for SHA-1. To get SHA-1 Certificate. Open command prompt and navigate to jdk/bin folder. Write the following command and hit enter
Note: Make sure you have changed c:\users\belal\ with c:\users\your_user_name in the above command
1 |
keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android |
Copy the SHA1 and paste it to the Enable Google Services Page.
Now click on Enable Google Signin. And click on Continue to Generate Configuration Files. Now click on download google-services.json to get your configuration file. Now come to android studio.
Google Login Android Project
Copy the json configuration file (you just downloaded) and paste it inside your google login android studio project (Inside app/ directory).
Open the Top Level build.gradle file and add the following line inside dependencies.
1 |
classpath 'com.google.gms:google-services:4.3.3' |
Inside build.gradle file add the following dependencies and sync your google login android project.
1 2 |
implementation 'com.google.android.gms:play-services-auth:18.0.0' implementation 'com.squareup.picasso:picasso:2.71828' |
The second line is for adding volley. I will use volley to load user’s profile image in this tutorial.
Now come to activity_main.xml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <com.google.android.gms.common.SignInButton android:id="@+id/sign_in_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" /> <ImageView android:id="@+id/profileImage" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:text="Name" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/textViewName" android:textStyle="bold" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:text="email" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/textViewEmail" android:textStyle="bold" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> |
As you can see I have created a google plus login button with two TextView (to show name and email) and a NetworkImageView (to show profile pic).
Now come to MainActivity.java and write the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
package com.androindian.gmailloginapp; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.ImageView; 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.squareup.picasso.Picasso; public class MainActivity extends AppCompatActivity implements View.OnClickListener, GoogleApiClient.OnConnectionFailedListener { TextView name,Email; ImageView imageView; SignInButton signInButton; private GoogleSignInOptions gso; private GoogleApiClient mGoogleApiClient; private int RC_SIGN_IN = 100; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); name = findViewById(R.id.textViewName); Email = findViewById(R.id.textViewEmail); imageView = findViewById(R.id.profileImage); gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestEmail() .build(); signInButton = (SignInButton) findViewById(R.id.sign_in_button); signInButton.setSize(SignInButton.SIZE_STANDARD); signInButton.setScopes(gso.getScopeArray()); mGoogleApiClient = new GoogleApiClient.Builder(this) .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */) .addApi(Auth.GOOGLE_SIGN_IN_API, gso) .build(); signInButton.setOnClickListener(this); } @Override public void onClick(View v) { signIn(); } private void signIn() { Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); //Starting intent for result startActivityForResult(signInIntent, RC_SIGN_IN); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); //If signin if (requestCode == RC_SIGN_IN) { GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); //Calling a new function to handle signin handleSignInResult(result); } } private void handleSignInResult(GoogleSignInResult result) { if (result.isSuccess()) { //Getting google account GoogleSignInAccount acct = result.getSignInAccount(); //Displaying name and email name.setText(acct.getDisplayName()); Email.setText(acct.getEmail()); Picasso.get().load(acct.getPhotoUrl().toString()).into(imageView); } else { //If login fails Toast.makeText(this, "Login Failed", Toast.LENGTH_LONG).show(); } } @Override public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.androindian.gmailloginapp"> <uses-permission android:name="android.permission.INTERNET"></uses-permission> <application android:allowBackup="true" android:usesCleartextTraffic="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
Now add internet permission to your manifest and run your google login android application.