Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to achieve google plus login functionality on own button in Android

Hello I am trying to learn Google Sign In functionality in android. I did this and working fine according as expected.

Here it is showing default button of Google+ to sign, here is the layout and screenshot.

<com.google.android.gms.common.SignInButton
    android:id="@+id/loginGoogleButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    />

enter image description here

Question

I don't want to have this style on my button. I want a simple button like android have

<Button
    android:id="@+id/loginFaceboookButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:background="@drawable/button_background"
    android:text="@string/login_with_facebook"
    android:textColor="@android:color/white" />

How that button can achieve Google Sign In functionality or any way to make com.google.android.gms.common.SignInButton to simple default button as in android.

Thanks in advance.

like image 615
N Sharma Avatar asked Nov 29 '14 12:11

N Sharma


People also ask

How do I add a Google login button?

To create a Google Sign-In button with custom settings, add an element to contain the sign-in button to your sign-in page, write a function that calls signin2. render() with your style and scope settings, and include the https://apis.google.com/js/platform.js script with the query string onload=YOUR_RENDER_FUNCTION .

How do I use Google button?

Talk to the Google Assistant: Say "Hey Google" or long press the button. Stop the Google Assistant: Press the button. Get notifications: Say "Hey Google, read my notifications" or press the button on the accessory. Answer or end a call: Press the button to answer a call.


3 Answers

On https://developers.google.com/+/web/signin/customize, this is written:

Customizing the sign-in button

You can customize the sign-in button so that it better fits your site's design. You must follow the branding guidelines and use the appropriate colors and icons in your custom button. You must also ensure that your button appears with the same prominence as other third-party login options. The branding guidelines also provide icon assets that you can use to design your custom button.

It links to the branding guidelines which state

These guidelines provide you with the design specification for various Google+ buttons and badges. You can use these assets on your app (website or mobile app) without pre-approval provided you follow these basic guidelines. Use of Google brands in ways not expressly covered by this document is not allowed without prior written consent from Google (see the Guidelines for Third Party Use of Google Brand Features for more information). Button use must also be in line with our Buttons Policy.

This basically means you are not even allowed to use the Google+ code with your own button, unless you get "written consent", which I imagine will be hard to acquire.

(You are allowed to use a "custom" button IF if your button conforms to the guidelines, which it does not).

I don't want to have this style on my button.

You pretty much have to.
You can try to work around it, but your app will not survive in the Play Store long, and your developer account might face consequences (this is my own assumption, though)).

like image 95
Tim Avatar answered Sep 27 '22 17:09

Tim


Legal matters aside, the SignInButton doesn't actually perform any special function, it's just a visual widget. From the docs:

The Google sign-in button to authenticate the user. Note that this class only handles the visual aspects of the button. In order to trigger an action, register a listener using setOnClickListener(OnClickListener).

That is, if you have a Google+ sample running (i.e. using GoogleApiClient, the related callbacks, any of the APIs from com.google.android.gms.plus, &c) then you can replace the SignInButton with any other regular button and it will work just the same.


About the sample code: sorry, from the wording of your question I assumed you already had this part running.

You can find the tutorial for G+ integration here, in particular this is the part about sign-in. Also, a sample project is distributed as part of the SDK (<android-sdk>\extras\google\google_play_services\samples\plus) with everything ready to go (although you need to enable the API in the Google Developers Console, as described in the link, for it to work properly).

You can then just swap the SignInButton for a regular button.

like image 26
matiash Avatar answered Sep 27 '22 16:09

matiash


implements ConnectionCallbacks, OnConnectionFailedListener than setup your normal button

btnGoogle.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if (internetChecker.isConnectingToInternet() == false) {
                        Toast.makeText(getApplicationContext(),getString(R.string.interet_connectivity),Toast.LENGTH_SHORT).show();
                    } else {
                        signInWithGplus();
                    }
} }); /** * Sign-in into google * */ private void signInWithGplus() { if (!mGoogleApiClient.isConnecting()) { mSignInClicked = true; resolveSignInError(); } } protected void onStart() { super.onStart(); mGoogleApiClient.connect(); } protected void onStop() { super.onStop(); if (mGoogleApiClient.isConnected()) { mGoogleApiClient.disconnect(); } } /** * Method to resolve any signin errors * */ private void resolveSignInError() { try { if (mConnectionResult.hasResolution()) { try { mIntentInProgress = true; mConnectionResult.startResolutionForResult(this, RC_SIGN_IN); } catch (SendIntentException e) { mIntentInProgress = false; mGoogleApiClient.connect(); } } } catch (Exception e) { // TODO: handle exception Log.d("error",e.toString()); return; } }

@Override public void onConnectionFailed(ConnectionResult result) { if (!result.hasResolution()) { Log.d("result.toString()", result.toString()); GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this,0).show(); return; } if (!mIntentInProgress) { // Store the ConnectionResult for later usage mConnectionResult = result; if (mSignInClicked) { // The user has already clicked 'sign-in' so we attempt to // resolve all // errors until the user is signed in, or they cancel. resolveSignInError(); } }

} @Override public void onConnected(Bundle arg0) { Log.d("onConnected", "onConnected::"+session.getIsRequestLogout()); if (!session.getIsRequestLogout()) { mSignInClicked = false; // Get user's information getGoogleProfileInformation(); // Update the UI after signin

}
else
{
    signOutFromGplus();
}

} /** * Fetching user's information name, email, profile pic * */ private void getGoogleProfileInformation() { try { if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) { Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient); GoogleId=currentPerson.getId(); UserName = currentPerson.getDisplayName(); UserEmail = Plus.AccountApi.getAccountName(mGoogleApiClient); /String personName = currentPerson.getDisplayName(); String personPhotoUrl = currentPerson.getImage().getUrl(); String personGooglePlusProfile = currentPerson.getUrl(); String email = Plus.AccountApi.getAccountName(mGoogleApiClient); Log.e("login", "Name: " + personName + ", plusProfile: " + personGooglePlusProfile + ", email: " + email + ", Image: " + personPhotoUrl);/

} else {
Toast.makeText(getApplicationContext(),getString(R.string.google_profile_error), Toast.LENGTH_LONG).show();
            }
} catch (Exception e) {
e.printStackTrace();
        }

}

@Override public void onConnectionSuspended(int arg0) { mGoogleApiClient.connect(); Toast.makeText(getApplicationContext(), getString(R.string.google_suspended_account), Toast.LENGTH_LONG).show(); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == RC_SIGN_IN) { if (requestCode != RESULT_OK) { mSignInClicked = false; } mIntentInProgress = false; if (!mGoogleApiClient.isConnecting()) { mGoogleApiClient.connect(); } }

}

And one more thing follow this answer Android Google+ Signin "An internal error has occurred"

like image 38
Bhavin Chauhan Avatar answered Sep 27 '22 18:09

Bhavin Chauhan