WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

WebView Tutorial Android


WebView is a view used to display the web pages in application.


we can use also specify HTML string and can show it inside our application using a WebView. you can also add it in java class
<WebView
android:id="@+id/demoWebView"  
android:layout_width="fill_parent"
android:layout_height="fill_parent" />



Must need internet permission For Webview Activity to access the Internet and load the web pages in a WebView we need to add permission. there are following java class add.
<!--Add this before application tag in AndroidManifest.xml-->
<uses-permission android:name="android.permission.INTERNET" />

Methods WebView In Android

loadUrl() – Load a web page in our WebView
loadUrl(String url)

This function is used to load a web page in a web view of our application.


/*Add in Oncreate() funtion after setContentView()*/
// initiate a web view
WebView simpleWebView=(WebView) findViewById(R.id.simpleWebView);
// specify the url of the web page in loadUrl function
simpleWebView.loadUrl("https://www.google.com");
loadData() – Load Static Html Data on WebView
  loadData(String data, String mimeType, String encoding) 
This method is used to load the static HTML string in a web view. loadData() function takes html string data.
/*Add in Oncreate() funtion after setContentView()*/
// initiate a web view
WebView webView = (WebView) findViewById(R.id.demoWebView);
// static html string data
String customHtml = "<html><body><h1>Hello, World!</h1>" +
        "<h1>Heading 2

Heading 4

Heading 3</h3>" + "<p>This is a sample paragraph of static HTML In Web view</p>" + "</body></html>"; // load static html data on a web view webView.loadData(customHtml, "text/html", "UTF-8");

Load Remote URL on WebView using WebViewClient we need Override the shouldOverrideUrlLoading() method.this method allow us to perform our own action when a particular url is selected
/*Add in Oncreate() funtion after setContentView()*/
// initiate a web view
 simpleWebView = (WebView) findViewById(R.id.demoWebView);

// set web view client
 simpleWebView.setWebViewClient(new MyWebViewClient());

// string url which you have to load into a web view
 String url = "https://www.google.com";
 simpleWebView.getSettings().setJavaScriptEnabled(true);
 simpleWebView.loadUrl(url); // load the url on the web view
 }

 // custom web view client class who extends WebViewClient
 private class MyWebViewClient extends WebViewClient {
 @Override
 public boolean shouldOverrideUrlLoading(WebView view, String url) {
 view.loadUrl(url); // load the url
 return true;
 }

canGoBack() – Move to one page back if a back history exist This method is used to specify whether the web view has a back history item or not
// initiate a web view
WebView simpleWebView=(WebView)findViewById(R.id.demoWebView);
// checks whether a web view has a back history item or not
Boolean canGoBack=demoWebView.canGoBack();