Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Enable GMail Login Feature Within Android Application in 4 Easy Steps

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 517
    Comment on it

    You must have seen many applications and websites provides option to login/signup using Gmail, Facebook, twitter etc. And In my previous tutorial, I had explained Integrate Twitter Login for Your Android App in same way you can also enable Gmail login feature within your android app.

     

    So, If you are willing to add gmail login within your app then please go through the steps mentioned below:-

     

    Step 1) You need to add dependency compile line in your build.gradle file

    compile 'com.google.android.gms:play-services-auth:9.8.0'
    

    sync your project after adding this.

     

    Step 2) Now for authentication you need to create a new project in google portal so that gmail login can be used within your application , so for this please go to the link mentioned below

    https://developers.google.com/mobile/add?platform=android&cntapi=signin&cnturl=https:%2F%2Fdevelopers.google.com%2Fidentity%2Fsign-in%2Fandroid%2Fsign-in%3Fconfigured%3Dtrue&cntlbl=Continue%20Adding%20Sign-In

    after creating new app on google domain the google domain will provide a   google-services.json file place this file inside your app folder

     

    Step 3) Now add the gmail login button code in your activity layout(xml) file.

     <com.google.android.gms.common.SignInButton
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:id="@+id/sign_in_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

     

    Step 4) Now add the below code in your Activity and all done

    public class LoginActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener {
    
        
        GoogleApiClient mGoogleApiClient;
        private int RC_SIGN_IN=1001;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);
    
    
            GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestEmail()
                    .build();
    
    
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
                    .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                    .build();
    
            SignInButton signInButton = (SignInButton) findViewById(R.id.sign_in_button);
            signInButton.setSize(SignInButton.SIZE_STANDARD);
            signInButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    signIn();
                }
            });
        }
    
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
    
            // Pass the activity result to the login button.
    //        loginButton.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()) {
                // Signed in successfully, show authenticated UI.
                Toast.makeText(this, "Yee" +
                        " we are in", Toast.LENGTH_SHORT).show();
                GoogleSignInAccount acct = result.getSignInAccount();
    
    
            } else {
                // Signed out, show unauthenticated UI.
    
            }
        }
    
        private void signIn() {
            Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
            startActivityForResult(signInIntent, RC_SIGN_IN);
        }
    
        @Override
        public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    
        }
    }
    

     

    How to Enable GMail Login Feature Within Android Application in 4 Easy Steps

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: