2

I'm developing app and using Google SignIn. In the iOS Api, I can get the first name and last name like this:

let GoogleUser = GIDSignIn.sharedInstance().currentUser
let firstName = googleAccount.profile.givenName
let lastName = googleAccount.profile.familyName

But in the Android API, I found only getDisplayName() that returns the full name of the user in one String. I can't find any method that returns only the first or the last name.

The Google SignIn API for Android site

Thanks :)

Aviv Abramovich
  • 135
  • 2
  • 9

3 Answers3

5

In android also have,

getFamilyName()
getGivenName()

Refer this,

com.google.android.gms.plus.model.people.Person.Name

Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48
  • This solution works (Using the solution here [link](http://stackoverflow.com/a/33817833/4723532) but this solution uses Google Plus API and not the regular Google SignIn API for Android. The iOS version of the Google SignIn API has the option for first name and last name separated, so I want to do it the same in Android without additional API. But thanks anyway :) – Aviv Abramovich May 02 '16 at 10:27
  • Okay . Mark as answer if it helps you – Sathish Kumar J May 02 '16 at 10:30
  • @AvivAbramovich: if you don't want to use Plus API, try https://developers.google.com/identity/sign-in/android/backend-auth#using-a-google-api-client-library or http://android-developers.blogspot.com/2016/01/using-google-sign-in-with-your-server.html to see if they can help or not – BNK May 02 '16 at 14:29
1

//After the signing we are calling this function private void handleSignInResult(GoogleSignInResult result) {

    //If the login succeed
    if (result.isSuccess()) {

        signInButton.setVisibility(View.GONE);
        //Getting google account
        GoogleSignInAccount acct = result.getSignInAccount();

        String name=acct.getDisplayName();

        String email=acct.getEmail();

        Log.d("namegoogle",name);
        Log.d("emailgoogle",email);
        edit_signin_emailid.setText(email);

        String fullname = acct.getDisplayName();
        String[] parts = fullname.split("\\s+");
        Log.d("Length-->",""+parts.length);
        if(parts.length==2) {
            String firstname = parts[0];
            String lastname = parts[1];
            Log.d("First-->", "" + firstname);
            Log.d("Last-->", "" + lastname);
            AppController.setStringPref("firstnamebook", firstname);
            AppController.setStringPref("lastnamebook", lastname);

            Log.d("FirstApp", "" + AppController.getStringPref("firstnamebook"));
            Log.d("LastApp", "" + AppController.getStringPref("lastnamebook"));
        }
        else if(parts.length==3){
            String firstname = parts[0];
            String middlename = parts[1];
            String lastname = parts[2];
            Log.d("First-->", "" + firstname);
            Log.d("Last-->", "" + lastname);
            AppController.setStringPref("firstnamebook", firstname);
            AppController.setStringPref("lastnamebook", lastname);
        }


    }
}
pavithra
  • 105
  • 1
  • 8
-1

Try this

String fullname = "Sathish Kumar Jeyasankar";
String[] parts = fullname.split("\\s+");
String firstname = parts[0]; // Sathish
String middlename = parts[1]; //  Kumar
String lastname = parts[2]; //  Jeyasankar
shinilms
  • 1,494
  • 3
  • 22
  • 35