Splash screen on a Android WebView while loads first time until javascript onload fires

All we need is an easy explanation of the problem, so here it is.

The desired behavior when opening an app is:

  • Show splash screen and load URL in parallel
  • When a javascript interface fired when onload just remove the splash screen

Mainactivity.java

myWebView.addJavascriptInterface(new JavaScriptInterface(this, cookieManager),"Android");

JavaScriptInterface.java

@JavascriptInterface
  public void hideOrRemoveSplashScreen() {
  objetcSplashScreen.doRemoveSplashScreen();    
  //...
}

HTML page (only for pages loaded with app, should detect with User Agent)

$(function() {
  try{Android.hideOrRemoveSplashScreen()}catch(e){};
});

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/pullfresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android">
    <WebView
        android:id="@+id/msw_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"></WebView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

I don’t know how to load in parallel a simple .png as splash screen with the rest of the app, and then, how to remove.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Seems this is working, not sure is the right way:

  1. Modify activity_main.xml in order to put a container with the image and webview
    <?xml version="1.0" encoding="utf-8"?>
    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/pullfresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android">
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
      <ImageView
        android:id="@+id/splashimg"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/darkblue"
        android:src="@drawable/splash" />
      <WebView
        android:id="@+id/msw_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"></WebView>
       </LinearLayout>
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

Then add objects in MainActivity.java

container = findViewById(R.id.container);
splash = findViewById(R.id.splashimg);

And finaly remove image onload inside Oncreate

myWebView.setWebChromeClient(new MyChrome() {
[...]
    @Override
    public void onProgressChanged(WebView view, int newProgress) {
       if (newProgress == 100){
          container.removeView(splash);
       }
       super.onProgressChanged(view, newProgress);
    }
}

On load first page fires the removeView and some white body flashes until page is fully loaded, but can be fixed setting <body style='background-color'>

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply