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:

Solution 3:

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

publicfinalclassMyWebviewextendsActivity {

 privatestaticfinalintDIALOG2_KEY=1;
 privateProgressDialogpd=null;
 privatestaticfinal String  AmitBlog="YOUR URL";
 private WebView webView;
 @OverrideprotectedvoidonCreate(Bundle icicle) {
  super.onCreate(icicle);
  setContentView(R.layout.main);
  pd = newProgressDialog(this);
  webView = (WebView) findViewById(R.id.webview);
  webView.setWebViewClient(newHelpClient());
  webView.getSettings().setBuiltInZoomControls(true);

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

 @OverrideprotectedvoidonResume() {
                super.onResume();
  LoadView();
 }

 privatevoidLoadView()
 {
  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 */@OverridepublicbooleanonKeyDown(int keyCode, KeyEvent event) {
  if (keyCode == KeyEvent.KEYCODE_BACK) {
   if (webView.canGoBack()) {
    webView.goBack();
    returntrue;
   }
  }
  returnsuper.onKeyDown(keyCode, event);
 }

/** WebViewClient is used to open other web link to the same Activity. */privatefinalclassHelpClientextendsWebViewClient {
  @OverridepublicvoidonPageFinished(WebView view, String url) {
   setTitle(view.getTitle());

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

  @OverridepublicbooleanshouldOverrideUrlLoading(WebView view, String url) {
   if (url.startsWith("file")) {
    returnfalse;
   } else{
    view.loadUrl(url);
    returntrue;
   }
  }
 }

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

Post a Comment for "Webview Loading Page Android"