Imzzy Imzzy - 21 days ago 7
Android Question

how to go to next screen after google sign in

I am using google sign in api to login it is working all fine but i am stuck at how to go to the next activity or screen after successful login

MainActivity.java

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.GoogleApiClient;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, GoogleApiClient.OnConnectionFailedListener {

//Signin button
private SignInButton signInButton;

//Signing Options
private GoogleSignInOptions gso;

//google api client
private GoogleApiClient mGoogleApiClient;

//Signin constant to check the activity result
private int RC_SIGN_IN = 100;

//TextViews
private TextView textViewName;
private TextView textViewEmail;
private NetworkImageView profilePhoto;

//Image Loader
private ImageLoader imageLoader;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//Initializing Views
textViewName = (TextView) findViewById(R.id.textViewName);
textViewEmail = (TextView) findViewById(R.id.textViewEmail);
profilePhoto = (NetworkImageView) findViewById(R.id.profileImage);

//Initializing google signin option
gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();

//Initializing signinbutton
signInButton = (SignInButton) findViewById(R.id.sign_in_button);
signInButton.setSize(SignInButton.SIZE_WIDE);
signInButton.setScopes(gso.getScopeArray());

//Initializing google api client
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();


//Setting onclick listener to signing button
signInButton.setOnClickListener(this);
}


//This function will option signing intent
private void signIn() {
//Creating an intent
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);

//Starting intent for result
startActivityForResult(signInIntent, RC_SIGN_IN);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//If signin
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
//Calling a new function to handle signin
handleSignInResult(result);
}
}


//After the signing we are calling this function
private void handleSignInResult(GoogleSignInResult result) {
//If the login succeed
if (result.isSuccess()) {
//Getting google account
GoogleSignInAccount acct = result.getSignInAccount();

//Displaying name and email
textViewName.setText(acct.getDisplayName());
textViewEmail.setText(acct.getEmail());

//Initializing image loader
imageLoader = CustomVolleyRequest.getInstance(this.getApplicationContext())
.getImageLoader();

imageLoader.get(acct.getPhotoUrl().toString(),
ImageLoader.getImageListener(profilePhoto,
R.mipmap.ic_launcher,
R.mipmap.ic_launcher));

//Loading image
profilePhoto.setImageUrl(acct.getPhotoUrl().toString(), imageLoader);

} else {
//If login fails
Toast.makeText(this, "Login Failed", Toast.LENGTH_LONG).show();
}
}

@Override
public void onClick(View v) {
if (v == signInButton) {
//Calling signin
signIn();
}
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}


}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

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

<com.android.volley.toolbox.NetworkImageView
android:id="@+id/profileImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:text="Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/textViewName"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />


<TextView
android:text="email"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />


<TextView
android:id="@+id/textViewEmail"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>


i just wanted to know where i can add the next activity on click og google sign in after sucessfull login it will show name email id and image of that particular person how to go to next screen after 5 seconds of login after displaying the image email id and name

Answer

In handleSignInResult function, you can call another activity if you got sucess result.

You can use handler for 5 seconds delay. Write below code after Loading image line.

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() 
    {
         Intent mainIntent = new Intent(MainActivity.this, OtherActivityName.class);
         startActivity(mainIntent);
     }
 }, 5000);