Search code examples
javaandroidgoogle-signin

Android - Login page is coming as blank


When I try to load the app, instead of the login page, I see just a blank page with nothing. Can anyone help me why its happening? I don't see any error, logs or anything that can help me dig deep. Code is compiling fine but its just not showing me the page.

Here is all the code for your reference -


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@drawable/logo"
        android:label="@string/app_name"
        android:roundIcon="@drawable/logo"
        android:supportsRtl="true"
        android:theme="@style/Theme.Bakarstation"
        android:usesCleartextTraffic="true"
        tools:targetApi="31">
        <activity
            android:name=".SplashActivity"
            android:exported="false" />
        <!-- <activity -->
        <!-- android:name=".PostsFeedActivity" -->
        <!-- android:exported="false" /> -->
        <!-- Need to remove above setting in prod env -->
        <activity
            android:name=".MainActivity"
            android:exported="false" />
        <activity
            android:name=".LoginActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

public class LoginActivity extends AppCompatActivity {

    private SignInClient oneTapClient;
    private BeginSignInRequest signUpRequest;
    ImageButton googleSignInButton;

    private static final int REQ_ONE_TAP = 2;  // Can be any integer unique to the Activity.
    private final boolean showOneTapUI = true;
    String userName;
    String userDisplayName;

    @Override
    public void onCreate(Bundle savedInstanceState,
                         PersistableBundle persistentState) {

        super.onCreate(savedInstanceState, persistentState);
        Log.i("CUSTOM MESSAGE", "Hello from Login Activity");

        googleSignInButton = findViewById(R.id.google_login_button);

        oneTapClient = Identity.getSignInClient(this);
        signUpRequest = BeginSignInRequest.builder()
                .setGoogleIdTokenRequestOptions(BeginSignInRequest.GoogleIdTokenRequestOptions.builder()
                        .setSupported(true)
                        // Your server's client ID, not your Android client ID.
                        .setServerClientId(getString(R.string.web_client_id))
                        // Show all accounts on the device.
                        .setFilterByAuthorizedAccounts(false)
                        .build())
                .build();

        ActivityResultLauncher<IntentSenderRequest> activityResultLauncher =
                registerForActivityResult(new ActivityResultContracts.StartIntentSenderForResult(), new ActivityResultCallback<ActivityResult>() {
                    @Override
                    public void onActivityResult(ActivityResult result) {
                        if (result.getResultCode() == Activity.RESULT_OK) {
                            try {
                                SignInCredential credential = oneTapClient.getSignInCredentialFromIntent(result.getData());
                                String idToken = credential.getGoogleIdToken();
                                if (idToken != null) {
                                    // Got an ID token from Google. Use it to authenticate
                                    // with your backend.

                                    //We can authenticate with our own server here as this will help establish that the user exists in our DB
                                    userName = credential.getId();   //userName will be google email id
                                    //Make a call to our backend server to make sure that the user exists and pull his id and alias name
                                    userDisplayName = credential.getDisplayName();
                                    boolean userExists = false;
                                    Toast.makeText(LoginActivity.this, "email:" + userName, Toast.LENGTH_SHORT).show();


                                    Log.d("TAG", "Got ID token.");
                                }
                            } catch (ApiException e) {
                                //Change following with logging
                                e.printStackTrace();
                            }
                        }
                    }
                });

        googleSignInButton.setOnClickListener(v -> oneTapClient.beginSignIn(signUpRequest)
                .addOnSuccessListener(LoginActivity.this, result -> {
                    IntentSenderRequest intentSenderRequest = new IntentSenderRequest.Builder(result.getPendingIntent().getIntentSender()).build();
                    activityResultLauncher.launch(intentSenderRequest);
                })
                .addOnFailureListener(LoginActivity.this, e -> {
                    // No Google Accounts found. Just continue presenting the signed-out UI.
                    Log.d("TAG", e.getLocalizedMessage());
                }));
    }
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical"
    android:background="@color/white"
    tools:context=".LoginActivity">
    <ImageButton
        android:id="@+id/google_login_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:contentDescription="@string/login_with_google"
        android:src="@drawable/android_dark_sq_ctn"
        android:tag="google_login_button"
        android:text="@string/sign_in_google" />
</RelativeLayout>

I explained in the problem that the page is not loading and there is no error or logs that I could see.


Solution

  • You have not added this in OnCreate method:

    setContentView(R.layout.activity_login)
    

    this might be the problem