Configure Google Sign-In Button and add in android app's layout that starts the sign-in flow.
We are going to configure google plus login with our app.
Steps :-
1. Add Google play services from SDK manager.
2. Now we need to add google-services.json file in our android project.
3. We are going to connect our app with Google Plus login so we need to create app or project in fire base.
So goto firebase console and create a project or app, we can see the below image.
Add the project name and country name and than create project after that select android and we can see below image.
Now, we need to add package name of our project and add SHA-1 key and register app and next step we can download google-services.json file.
Now you can get SHA-1 from below image.
Code :-
add dependencies in build.gradle file.
dependencies { classpath 'com.android.tools.build:gradle:2.1.0' classpath 'com.google.gms:google-services:3.0.0' } |
dependencies { compile 'com.google.android.gms:play-services-auth:9.2.1' compile 'com.github.bumptech.glide:glide:3.7.0' } apply plugin: 'com.google.gms.google-services' |
Strings.xml
<resources> <string name="app_name">GoogleAccountLogin</string> <string name="btn_logout">Logout</string> </resources>
MainActivity.java
public class MainActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener { private TextView name,email; private ImageView profile; private RelativeLayout avtar_layout; private SignInButton btn_log_in; private static final int RC_SIGN_IN = 007; private GoogleApiClient mGoogleApiClient; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); name = (TextView)findViewById(R.id.name); email = (TextView)findViewById(R.id.email); profile = (ImageView) findViewById(R.id.profile); avtar_layout = (RelativeLayout)findViewById(R.id.avatar_layout); btn_log_in = (SignInButton) findViewById(R.id.btn_log_in); GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestEmail() .build() GoogleApiClient = new GoogleApiClient.Builder(this) .enableAutoManage(this, this) .addApi(Auth.GOOGLE_SIGN_IN_API, gso) .build(); btn_log_in.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); startActivityForResult(signInIntent, RC_SIGN_IN); } }); } @Override public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { Toast.makeText(getApplicationContext(),"Somthing is wrong",Toast.LENGTH_LONG).show(); } private void handleSignInResult(GoogleSignInResult result) { if (result.isSuccess()) { GoogleSignInAccount acct = result.getSignInAccount(); String personName = acct.getDisplayName(_; String id = acct.getId(); String personPhotoUrl = acct.getPhotoUrl() != null ? acct.getPhotoUrl().toString() : null; String useremail = acct.getEmail(); name.setText(personName); email.setText(useremail); Glide.with(getApplicationContext()).load(personPhotoUrl) .thumbnail(0.5f) .crossFade() .diskCacheStrategy(DiskCacheStrategy.ALL) .into(profile); ChangeDesign(true); } else { ChangeDesign(false); } } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == RC_SIGN_IN) { GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); handleSignInResult(result); } } private void ChangeDesign(boolean isSignedIn) { if (isSignedIn) { avtar_layout.setVisibility(View.VISIBLE); email.setVisibility(View.VISIBLE); btn_log_in.setVisibility(View.GONE); } else { Toast.makeText(getApplicationContext(),"Somthing is wrong",Toast.LENGTH_LONG).show(); }}}Output :-
Comments
Post a Comment