Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);       }    } 
like image 211
Migua Avatar asked Mar 06 '12 18:03

Migua


2 Answers

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"         /> 
like image 68
davehale23 Avatar answered Sep 19 '22 06:09

davehale23


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.

like image 44
Živorad Milekić Avatar answered Sep 21 '22 06:09

Živorad Milekić