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

前端 未结 3 493
名媛妹妹
名媛妹妹 2020-11-30 22:01

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 empt

相关标签:
3条回答
  • 2020-11-30 22:36

    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"
            />
    
    0 讨论(0)
  • 2020-11-30 22:44

    I have one activity. 1 xml file and 1 java class. Inside xml file i have:

    1. WebView
    2. ImageView, logo of my application,
    3. ProgressBar and
    4. TextView, app version .

    Code of main.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:a="http://schemas.android.com/apk/res/android"
        a:layout_width="fill_parent"
        a:layout_height="fill_parent"
        a:background="#aaaaaa"
        a:orientation="vertical" >
    
    <WebView
        a:id="@+id/webView1"
        a:layout_width="fill_parent"
        a:layout_height="fill_parent" />
    
    <ImageView
        a:id="@+id/imageView1"
        a:layout_width="wrap_content"
        a:layout_height="wrap_content"
        a:layout_alignParentTop="true"
        a:layout_centerHorizontal="true"
        a:layout_marginTop="46dp"
        a:src="@drawable/logo" />
    
    <ProgressBar
        a:id="@+id/progressBar1"
        a:layout_width="wrap_content"
        a:layout_height="wrap_content"
        a:layout_below="@+id/imageView1"
        a:layout_centerHorizontal="true" />
    
    <TextView
        a:id="@+id/textView1"
        a:layout_width="wrap_content"
        a:layout_height="wrap_content"
        a:layout_alignParentBottom="true"
        a:layout_alignParentRight="true"
        a:layout_marginBottom="13dp"
        a:layout_marginRight="13dp"
        a:text="version 1.0"
        a:textAppearance="?android:attr/textAppearanceSmall"
        a:textColor="#444444" />
    
    </RelativeLayout>
    

    Code of NovcanikActivity.java:

    package zm.Nocanik;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.view.View;
    import android.webkit.DownloadListener;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import android.widget.ImageView;
    import android.widget.ProgressBar;
    import android.widget.TextView;
    
    
    public class NovcanikActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
        WebView webview = (WebView) findViewById(R.id.webView1);
    
        WebSettings websettings = webview.getSettings();
    
        websettings.setJavaScriptEnabled(true);
        websettings.setSaveFormData(false);
        websettings.setSavePassword(false);
    
        webview.loadUrl("http://m.novcanik.net/?appvers=1.0");
        webview.setHorizontalScrollBarEnabled(false);
        webview.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
        webview.setBackgroundColor(128);
    
        webview.setWebViewClient(new NovcanikWebViewClient());
    
        webview.setDownloadListener(new DownloadListener() {
            public void onDownloadStart(String url, String userAgent,
                    String contentDisposition, String mimetype,
                    long contentLength) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                startActivity(intent);
    
            }
        });
    
    
    
    
    }
    
    public void visible(){
    
        WebView webview = (WebView) findViewById(R.id.webView1);
    
        ImageView logo = (ImageView) findViewById(R.id.imageView1);
    
        ProgressBar bar = (ProgressBar) findViewById(R.id.progressBar1);
    
        TextView version = (TextView) findViewById(R.id.textView1);
    
        webview.setVisibility(10);
    
        logo.setVisibility(0);
    
        bar.setVisibility(0);
    
        version.setVisibility(0);
    
    }
    
    public void unvisible(){
    
        WebView webview = (WebView) findViewById(R.id.webView1);
    
        ImageView logo = (ImageView) findViewById(R.id.imageView1);
    
        ProgressBar bar = (ProgressBar) findViewById(R.id.progressBar1);
    
        TextView version = (TextView) findViewById(R.id.textView1);
    
        webview.setVisibility(0);
    
        logo.setVisibility(10);
    
        bar.setVisibility(10);
    
        version.setVisibility(10);
    
    }
    
    
    private class NovcanikWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView webview, String url){
            webview.loadUrl(url);
            return true;
        }
    
    @Override
    public void onReceivedError(WebView view, int errorCode,
            String description, String failingUrl) {
        // TODO Auto-generated method stub
        view.loadUrl("file:///android_asset/noconnection.html");
    }
    
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
    
        visible();
    
    }
    
    @Override
    public void onPageFinished(WebView view, String url) {
    
        unvisible();
    
    }
    
    }
    
    }
    

    Sorry for no description. If there would be need for description, i will describe in detail the entire code.

    0 讨论(0)
  • 2020-11-30 22:50

    Just use mWebView.setBackgroundColor(getResources().getColor(R.color.colorPrimary)); for your mWebView

    0 讨论(0)
提交回复
热议问题