Skip to content Skip to sidebar Skip to footer

WebView Loading Page Android

My app begins by loading a webpage as a WebView on onCreate. Should I extend this into a new thread since it may hang for a bit? Or at least a way to show that the page is still lo

Solution 1:

the webview handles threading itself, so you don't need to worry about that.

you can register callbacks for when the page starts and finishes loading. it's up to you to put up a progress bar, or whatever you want. see WebChromeClient.onProgressChanged() for details. here's a good post that gives some details.

you can add something to your manifest to tell the system you don't care about orientation changes. add the following to your activity definition,

android:configChanges="orientation"

the other option is to lock your app into one orientation or the other,

android:screenOrientation="portait"

Solution 2:

Ad 1. I have not (and should not) do that in separate thread.

WebView progress bar - Android WebView progress bar

Ad 2. you can block orientation changing by adding android:screenOrientation="portait" or "landscape" to declaration of your Activity in manifest.


Solution 3:

You should use Progress Dialog. Show dialog till load the web page to the WebView.

public final class MyWebview extends Activity {

 private static final int DIALOG2_KEY = 1;
 private ProgressDialog pd = null;
 private static final String  AmitBlog="YOUR URL";
 private WebView webView;
 @Override
 protected void onCreate(Bundle icicle) {
  super.onCreate(icicle);
  setContentView(R.layout.main);
  pd = new ProgressDialog(this);
  webView = (WebView) findViewById(R.id.webview);
  webView.setWebViewClient(new HelpClient());
  webView.getSettings().setBuiltInZoomControls(true);

  /** Showing Dialog Here */
  showDialog(DIALOG2_KEY);
 }

 @Override
 protected void onResume() {
                super.onResume();
  LoadView();
 }

 private void LoadView()
 {
  webView.loadUrl(AmitBlog);
 }

 /** Its very important while navigation hardware back button if we navigate to another link .Its like a Stack pop of all the pages you visited in WebView */
 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
  if (keyCode == KeyEvent.KEYCODE_BACK) {
   if (webView.canGoBack()) {
    webView.goBack();
    return true;
   }
  }
  return super.onKeyDown(keyCode, event);
 }

/** WebViewClient is used to open other web link to the same Activity. */

  private final class HelpClient extends WebViewClient {
  @Override
  public void onPageFinished(WebView view, String url) {
   setTitle(view.getTitle());

   /** Dismissing Dialog Here after page load*/
   dismissDialog(DIALOG2_KEY);
  }

  @Override
  public boolean shouldOverrideUrlLoading(WebView view, String url) {
   if (url.startsWith("file")) {
    return false;
   } else{
    view.loadUrl(url);
    return true;
   }
  }
 }

 @Override
 protected Dialog onCreateDialog(int id) {
  switch (id)
  {
  case DIALOG2_KEY:
  {
   pd.setMessage(getResources().getString(R.string.Loading));
   pd.setIndeterminate(true);
   pd.setCancelable(false);
   return pd;
  }
  }
  return null;
 }
}

Post a Comment for "WebView Loading Page Android"