thieumao

Show WebView Android

Dec 29th, 2014
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 KB | None | 0 0
  1. package com.androidexample.webview;
  2.  
  3. import android.app.Activity;
  4. import android.app.ProgressDialog;
  5. import android.os.Bundle;
  6. import android.webkit.WebView;
  7. import android.webkit.WebViewClient;
  8.  
  9. public class ShowWebView extends Activity {
  10.  
  11.     //private Button button;
  12.     private WebView webView;
  13.     public void onCreate(Bundle savedInstanceState) {
  14.        
  15.         super.onCreate(savedInstanceState);
  16.         setContentView(R.layout.show_web_view);
  17.        
  18.         //Get webview
  19.         webView = (WebView) findViewById(R.id.webView1);
  20.        
  21.         startWebView("http://www.androidexample.com/media/webview/login.html");
  22.        
  23.     }
  24.    
  25.     private void startWebView(String url) {
  26.        
  27.         //Create new webview Client to show progress dialog
  28.         //When opening a url or click on link
  29.        
  30.         webView.setWebViewClient(new WebViewClient() {      
  31.             ProgressDialog progressDialog;
  32.          
  33.             //If you will not use this method url links are opeen in new brower not in webview
  34.             public boolean shouldOverrideUrlLoading(WebView view, String url) {              
  35.                 view.loadUrl(url);
  36.                 return true;
  37.             }
  38.        
  39.             //Show loader on url load
  40.             public void onLoadResource (WebView view, String url) {
  41.                 if (progressDialog == null) {
  42.                     // in standard case YourActivity.this
  43.                     progressDialog = new ProgressDialog(ShowWebView.this);
  44.                     progressDialog.setMessage("Loading...");
  45.                     progressDialog.show();
  46.                 }
  47.             }
  48.             public void onPageFinished(WebView view, String url) {
  49.                 try{
  50.                 if (progressDialog.isShowing()) {
  51.                     progressDialog.dismiss();
  52.                     progressDialog = null;
  53.                 }
  54.                 }catch(Exception exception){
  55.                     exception.printStackTrace();
  56.                 }
  57.             }
  58.            
  59.         });
  60.          
  61.          // Javascript inabled on webview  
  62.         webView.getSettings().setJavaScriptEnabled(true);
  63.        
  64.         // Other webview options
  65.         /*
  66.         webView.getSettings().setLoadWithOverviewMode(true);
  67.         webView.getSettings().setUseWideViewPort(true);
  68.         webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
  69.         webView.setScrollbarFadingEnabled(false);
  70.         webView.getSettings().setBuiltInZoomControls(true);
  71.         */
  72.        
  73.         /*
  74.          String summary = "<html><body>You scored <b>192</b> points.</body></html>";
  75.          webview.loadData(summary, "text/html", null);
  76.          */
  77.        
  78.         //Load url in webview
  79.         webView.loadUrl(url);
  80.          
  81.          
  82.     }
  83.    
  84.     // Open previous opened link from history on webview when back button pressed
  85.    
  86.     @Override
  87.     // Detect when the back button is pressed
  88.     public void onBackPressed() {
  89.         if(webView.canGoBack()) {
  90.             webView.goBack();
  91.         } else {
  92.             // Let the system handle the back button
  93.             super.onBackPressed();
  94.         }
  95.     }
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment