javaandroid-studioandroid-webviewandroid-thread

Finish splash screen on website loading


I need to display splash screen until web page is loaded in webview.

I use following code. Is it possible to do?

public class SplashScreen extends Activity {

    protected Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_splash_screen);

        intent = new Intent(getApplicationContext(), MainActivity.class);

        // MainActivity.class  contains WebView

        Thread myThread = new Thread() {
            @Override
            public void run() {

                try {
                    sleep(5000);
                    startActivity(intent);
                    finish();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        };

        myThread.start();
    }
}

And

public class MainActivity extends Activity {

    private WebView view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_main);

        String url = "http://google.com";
        view = (WebView)this.findViewById(R.id.webView1);

        view.clearCache(true);
        WebSettings s = view.getSettings();
        s.setJavaScriptEnabled(true);

        s.setCacheMode(WebSettings.LOAD_DEFAULT);
        s.setDomStorageEnabled(true);

        view.loadUrl(url);
    }

Solution

  • Don't make a separate activity for splash screen, in MainActivity.java itself create a splash screen layout and webview layout and set visibility of webview to GONE.

    On open of MainActivity initialize webView and set custom WebViewClient. Override onPageFinished() in your custom webViewClient and in this method make webview visible and splash screen layout to Gone.

    Same here : Loading a WebView URL before splashscreen finishes