Splash screen while loading a url in a webview in android app

Migua picture Migua · Mar 6, 2012 · Viewed 49.4k times · Source

I've got an app, that has 2 activity, the first one launch the second to load a url into a webview.

It works, but while the url is loading , the webview appear empty... then i want to make a splash screen or something like this, to show it while the url is loading, I did that in a new activity, but i don't know what can i do to close the third activity when the url is loaded... Please can anybody help me?

This is my code...Thank you!

public class Visor extends  Activity {

    WebView mWebView;
    int Result;

    @Override
    public void onCreate (Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.visor);
        Bundle extras=getIntent().getExtras();
        String s= extras.getString("url");

        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setPluginsEnabled(true);
        mWebView.getSettings().setAllowFileAccess(true);

        mWebView.loadUrl(s);
        mWebView.setWebViewClient(new VisorClient());
        mWebView.getSettings().setBuiltInZoomControls(true);

        }

    private class VisorClient extends WebViewClient {

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                lanzarIntro();
            }
            @Override
            public void onPageFinished(WebView view, String url) {
                mWebView.loadUrl(url);
            }
     }

    public void lanzarIntro(){
        Intent i=new Intent (this, Intro.class);

        startActivity(i);


    }



}

Answer

davehale23 picture davehale23 · Mar 6, 2012

I do it by initially showing an ImageView and then once the WebView has loaded, swapping their visibility like this

        WebView wv = (WebView) findViewById(R.id.webView1);
        wv.getSettings().setJavaScriptEnabled(true);
        wv.setWebViewClient(new WebViewClient() {

            ...

            @Override
            public void onPageFinished(WebView view, String url) {
                //hide loading image
                findViewById(R.id.imageLoading1).setVisibility(View.GONE);
                //show webview
                findViewById(R.id.webView1).setVisibility(View.VISIBLE);
            }


        });     
        wv.loadUrl("http://yoururlhere.com");

And my xml layout looks like this

    <ImageView android:id="@+id/imageLoading1"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:visibility="visible"
        android:src="@drawable/vert_loading"
        />
    <WebView android:id="@+id/webView1"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:visibility="gone"
        />