-1

Hello i am trying to build a app using google sdk, i want to display the profile name in other activity where i have EditText. For example :- GoogleActivity.java to UserInformation.java

i have successfully created google login so now i want to fetch profile name from GoogleActivity.java and want to display in UserInformation.java Here is my code for USerInformation.java

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_signin);
        signInToolbar = (Toolbar) findViewById(R.id.toolbar_signin);
        setSupportActionBar(signInToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        signInToolbar.setNavigationIcon(R.drawable.back_arrow);

        OtpVerify = (TextView) findViewById(R.id.OtpVerification);
        editTextUsername = (EditText) findViewById(R.id.editTextName);
        editTextEmail = (EditText) findViewById(R.id.editTextEmail);

        signInToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        getSupportActionBar().setTitle(Html.fromHtml("<font color='#FFFFFF'>Sign Up</font>"));
        OtpVerify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OtpVerification();
            }
        });


    }

    private void OtpVerification() {

        LayoutInflater inflater = getLayoutInflater();
        View OtpDialongScreen = inflater.inflate(R.layout.otpverification, null);
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setView(OtpDialongScreen);
        alert.setCancelable(false);
        final AlertDialog dialog = alert.create();
        dialog.show();

    }


}

can anyone help me to display the profile name in EditText

Nongthonbam Tonthoi
  • 12,667
  • 7
  • 37
  • 64

1 Answers1

0

You can get the user iformation from Google API using the following code. Or for more details you can refer : https://developers.google.com/identity/sign-in/android/people#before_you_begin

GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
GoogleSignInAccount acct = result.getSignInAccount();
String personName = acct.getDisplayName();
String personEmail = acct.getEmail();
String personId = acct.getId();
Uri personPhoto = acct.getPhotoUrl();

Now, the user's name is in the variable personName. After successful login, the application will not enters in login page so its better to save the user information in SharedPreference.Create a class AppUtils.java

public class AppUtils {

private final static String SHARED_PREFS="AppUtils";
private SharedPreferences prefs;
private SharedPreferences.Editor edit;

public AppUtils(Context context) {
    prefs = context.getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE);
}

public void setUserName(String name) {

    edit = prefs.edit();
    edit.putString("name", name);
    edit.commit();
}

public String getUserName() {
    return prefs.getString("name", "");
}

}

So in your GoogleActivity.java class after fetching the username set it to a shared preference object by calling the function setUserName().

AppUtils utils=new AppUtils(UserInformation.this);
utils.setUserName(personName);

And whenever you wants call the function getUserName(). In your case :

AppUtils utils=new AppUtils(UserInformation.this);

editTextUsername = (EditText) findViewById(R.id.editTextName);
editTextEmail = (EditText) findViewById(R.id.editTextEmail);

editTextUsername.setText(utils.getUserName());
Phoenix
  • 238
  • 2
  • 12
  • `UserInformation.this` can u guide what is this – Firdoesh Khan May 30 '16 at 18:53
  • It is the context of that activity. Context is context of current state of the application/object. It provides services like resolving resources, obtaining access to databases and preferences, and so on. An android app has activities. It’s like a handle to the environment your application is currently running in. The activity object inherits the Context object. For more information refer the link : http://www.simplecodestuffs.com/what-is-context-in-android/ – Phoenix May 31 '16 at 04:44