Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML Splash Screen -> Display Webview

Thanks for trying to solve my problem!

Currently my app basically loads a web page using the Android Web view. The downside is that sometimes (with a bad connection) the web page takes over 10 seconds to load. Is there a way I can create some sort of splash XML "loading" screen while the web page is loading?

Take a look at my code...

package com.ect;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.CookieSyncManager;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class SomeActivity extends Activity {

    /** Called when the activity is first created. */

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

        //THIS IS WHAT I WANT: setContentView(R.layout.main);

        CookieSyncManager.createInstance(getBaseContext());

        // Let's display the progress in the activity title bar, like the
        // browser app does.


        getWindow().requestFeature(Window.FEATURE_PROGRESS);

        WebView webview = new WebView(this);
        setContentView(webview);


        webview.getSettings().setJavaScriptEnabled(true);

        final Activity activity = this;
        webview.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {
             // Activities and WebViews measure progress with different scales.
             // The progress meter will automatically disappear when we reach 100%
             activity.setProgress(progress * 1000);
        }
      });

webview.setWebViewClient(new WebViewClient() {

public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        //Users will be notified in case there's an error (i.e. no internet connection)
        Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}

public void onPageFinished(WebView view, String url) {
    CookieSyncManager.getInstance().sync();
}
});

CookieSyncManager.getInstance().startSync();
CookieSyncManager.getInstance().sync();

     //This will load the webpage that we want to see
      webview.loadUrl("http://www.web.com/");

   }
}

That is my code. If you look closely I made a comment that says, "THIS IS WHAT I WANT: ..."

That comment would call the main.xml file to be displayed, but if I use that comment the main.xml file will be displayed, but the webview never appears...

How can I display the main.xml file and then (once the the web view loads) display the webpage?

like image 546
Nick Chubb Avatar asked Dec 21 '11 16:12

Nick Chubb


2 Answers

You need to create a WebViewClient object and override the onPageFinished() method. Then use webView.setWebViewClient() to set the WebViewClient object you created above to the WebView. Now you can run some code in onPageFinished() such as setContentView() to change the content view from the loading screen to the web view when it has finished loading.

This tutorial might help you a bit, although it isn't exactly what you need. http://www.giantflyingsaucer.com/blog/?p=1331

like image 128
Thomas Clayson Avatar answered Nov 02 '22 23:11

Thomas Clayson


You must use the event onPageFinished().

Here is the official reference Clickme

Your main activity must setContentView(R.layout.main); and the function onPageFinished() must setContentView(webview);

like image 23
vgonisanz Avatar answered Nov 02 '22 23:11

vgonisanz