{"id":984,"date":"2016-02-24T05:59:37","date_gmt":"2016-02-24T05:59:37","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?page_id=984"},"modified":"2019-06-12T12:07:04","modified_gmt":"2019-06-12T12:07:04","slug":"webview","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/ui\/webview","title":{"rendered":"WebView Tutorial With Example In Android Studio"},"content":{"rendered":"<p>In Android, WebView is a view used to display the web pages in application. This class is the basis upon which you can roll your own web browser or simply use\u00a0it\u00a0to display some online content within your Activity. We can also specify HTML string and can show it inside our application using a WebView. Basically, WebView turns application into a web application.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1070\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/WebView-in-Android.jpg\" alt=\"WebView in Android\" width=\"399\" height=\"343\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/WebView-in-Android.jpg 399w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/WebView-in-Android-300x258.jpg 300w\" sizes=\"auto, (max-width: 399px) 100vw, 399px\" \/><\/center>In order to add Web View in your application, you have to add\u00a0<strong>&lt;WebView&gt; <\/strong>element to your XML( layout ) file or you can also add it in java class.<\/p>\n<pre>&lt;WebView\r\nandroid:id=\"@+id\/simpleWebView\"\u00a0\u00a0\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"fill_parent\" \/&gt;<\/pre>\n<hr \/>\n<h4><strong>Internet Permission Required For Webview:<\/strong><\/h4>\n<p><span style=\"color: #ff0000;\"><strong>Important\u00a0Note:<\/strong><\/span> In order for Activity to access the Internet and load the web pages in a WebView, we\u00a0must add the internet permissions to our Android Manifest file (Manifest.xml).<\/p>\n<p>Below code define the internet permission in our manifest file to access the internet in our application.<\/p>\n<pre>&lt;!--Add this before application tag in AndroidManifest.xml--&gt;\r\n&lt;uses-permission android:name=\"android.permission.INTERNET\" \/&gt;\r\n<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1073\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/Intenet-Permission-AndroidManifest-Code-For-WebView.jpg\" alt=\"Intenet Permission AndroidManifest Code For WebView\" width=\"516\" height=\"218\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Intenet-Permission-AndroidManifest-Code-For-WebView.jpg 516w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Intenet-Permission-AndroidManifest-Code-For-WebView-300x127.jpg 300w\" sizes=\"auto, (max-width: 516px) 100vw, 516px\" \/><\/center><\/p>\n<hr \/>\n<h4><strong>Methods of WebView In Android:<\/strong><\/h4>\n<p>Let\u2019s discuss some common methods of a Webview which are used to configure a web view in our application.<\/p>\n<p><span style=\"color: #008000;\"><strong>loadUrl() &#8211; Load a web page in our WebView<\/strong><\/span><\/p>\n<pre><strong>loadUrl(String url)<\/strong><\/pre>\n<p>This function is used to load a web page in a web view of our application. In this method we\u00a0specify the url of the web page that should be loaded in a web view.<\/p>\n<p>Below we load a url:\u00a0<a href=\"\/ui\/\">https:\/\/abhiandroid.com\/ui\/<\/a> in our application.<\/p>\n<pre>\/*Add in Oncreate() funtion after setContentView()*\/\r\n\/\/ initiate a web view\r\nWebView simpleWebView=(WebView) findViewById(R.id.simpleWebView);\r\n\/\/ specify the url of the web page in loadUrl function\r\nsimpleWebView.loadUrl(\"https:\/\/abhiandroid.com\/ui\/\");<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1074\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/loadUrl-in-Webview-Android.jpg\" alt=\"loadUrl in Webview Android\" width=\"294\" height=\"262\" \/><\/center><span style=\"color: #008000;\"><strong>2. loadData() &#8211; Load Static Html Data on WebView<\/strong><\/span><\/p>\n<pre><strong>\u00a0<\/strong><strong>\u00a0<\/strong><strong>loadData(String data, String mimeType, String encoding)<\/strong><strong>\u00a0<\/strong>\r\n<\/pre>\n<p>This method is used to load the static HTML string in a web view. loadData() function takes html string data, mime-type and encoding param\u00a0as three parameters.<\/p>\n<p>Below we load the static Html string data in our application of\u00a0a web view.<\/p>\n<pre>\/*Add in Oncreate() funtion after setContentView()*\/\r\n\/\/ initiate a web view\r\nWebView webView = (WebView) findViewById(R.id.simpleWebView);\r\n\/\/ static html string data\r\nString customHtml = \"&lt;html&gt;&lt;body&gt;&lt;h1&gt;Hello, AbhiAndroid&lt;\/h1&gt;\" +\r\n        \"&lt;h1&gt;Heading 1&lt;\/h1&gt;&lt;h2&gt;Heading 2&lt;\/h2&gt;&lt;h3&gt;Heading 3&lt;\/h3&gt;\" +\r\n        \"&lt;p&gt;This is a sample paragraph of static HTML In Web view&lt;\/p&gt;\" +\r\n        \"&lt;\/body&gt;&lt;\/html&gt;\";\r\n\/\/ load static html data on a web view\r\nwebView.loadData(customHtml, \"text\/html\", \"UTF-8\");<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1075\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/loadData-in-WebView-Android.jpg\" alt=\"loadData in WebView Android\" width=\"326\" height=\"326\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/loadData-in-WebView-Android.jpg 326w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/loadData-in-WebView-Android-150x150.jpg 150w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/loadData-in-WebView-Android-300x300.jpg 300w\" sizes=\"auto, (max-width: 326px) 100vw, 326px\" \/><\/center><span style=\"color: #008000;\"><strong>3. Load Remote URL on\u00a0WebView using WebViewClient:<\/strong><\/span><\/p>\n<p>WebViewClient help us to monitor event in a WebView. You have to Override the\u00a0shouldOverrideUrlLoading() method. This method allow us to\u00a0perform our own action when a particular url is selected. Once you are ready with the WebViewClient, you can set the WebViewClient in your WebView using the\u00a0setWebViewClient()\u00a0method.<\/p>\n<p>Below we load a url by using web view client in a WebView.<\/p>\n<pre>\/*Add in Oncreate() funtion after setContentView()*\/\r\n\/\/ initiate a web view\r\n simpleWebView = (WebView) findViewById(R.id.simpleWebView);\r\n\r\n\/\/ set web view client\r\n simpleWebView.setWebViewClient(new MyWebViewClient());\r\n\r\n\/\/ string url which you have to load into a web view\r\n String url = \"https:\/\/abhiandroid.com\/ui\/\";\r\n simpleWebView.getSettings().setJavaScriptEnabled(true);\r\n simpleWebView.loadUrl(url); \/\/ load the url on the web view\r\n }\r\n\r\n \/\/ custom web view client class who extends WebViewClient\r\n private class MyWebViewClient extends WebViewClient {\r\n @Override\r\n public boolean shouldOverrideUrlLoading(WebView view, String url) {\r\n view.loadUrl(url); \/\/ load the url\r\n return true;\r\n }<\/pre>\n<p><span style=\"color: #008000;\"><strong>4. canGoBack() &#8211; Move to one page back if a back history exist<\/strong><\/span><\/p>\n<p>This method is used to specify whether the web view has a back history item or not. This method returns a Boolean value either\u00a0true or false. If it returns true then goBack() method is used to move one page back.<\/p>\n<p>Below we check whether a web view has back history or not.<\/p>\n<pre>\/\/ initiate a web view\r\nWebView simpleWebView=(WebView)findViewById(R.id.<strong><em>simpleWebView<\/em><\/strong>);\r\n\/\/ checks whether a web view has a back history item or not\r\nBoolean canGoBack=simpleWebView.canGoBack();\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>5. canGoForward() &#8211; Move one page forward if forward history exist<\/strong><\/span><\/p>\n<p>This method is used to specify whether the web view has a forword history item or not. This method returns a Boolean value either\u00a0true or false. If it returns true then goForword() method is used to move one page forword.<\/p>\n<p>Below we check whether a web view has forward history or not.<\/p>\n<pre>\/\/ initiate a web view\r\n WebView simpleWebView=(WebView)findViewById(R.id.simpleWebView);\r\n\/\/ checks whether a web view has a forward history item or not\r\n Boolean canGoForword=simpleWebView.canGoForward() ;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>6. clearHistory() &#8211; clear the WebView history<\/strong><\/span><\/p>\n<p>This method is used to clear the web view forward and backward history.<\/p>\n<p>Below we clear the forword and backword history of a WebView.<\/p>\n<pre>WebView simpleWebView=(WebView)findViewById(R.id.<strong><em>simpleWebView<\/em><\/strong>); \/\/ initiate a web view\r\nsimpleWebView.clearHistory(); \/\/ clear the forward and backward history<\/pre>\n<hr \/>\n<h4><strong>WebView Example In Android Studio:<\/strong><\/h4>\n<p>Here in this WebView example\u00a0we show the use of web view in our application. To do\u00a0that we display two buttons one is for displaying a web page and other is for displaying static HTML data in a web view. Below is the final output, download code and step by step explanation:<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"https:\/\/github.com\/abhisheksaini4\/WebViewExample\" target=\"_blank\" rel=\"nofollow\">Download Code<\/a><a class=\"help\" title=\"Learn How To Download Code And Import In Android Studio\" href=\"\/androidstudio\/download-code-abhiandroid\" target=\"_blank\" rel=\"nofollow\"> ? <\/a><\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1076\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/WebView-Example-in-Android-Studio.jpg\" alt=\"WebView Example in Android Studio\" width=\"207\" height=\"327\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/WebView-Example-in-Android-Studio.jpg 207w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/WebView-Example-in-Android-Studio-190x300.jpg 190w\" sizes=\"auto, (max-width: 207px) 100vw, 207px\" \/><\/center><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span> Create a new project and name it <strong>WebViewExample<\/strong><\/p>\n<pre>Select File -&gt; New -&gt; New Project\u2026 then Fill the forms and click \"Finish\" button.<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span> Open res -&gt; layout -&gt;\u00a0activity_main.xml (or) main.xml\u00a0and add following code :<\/p>\n<p>In this step we open an XML\u00a0file and add the code for displaying two buttons and a Webview in our xml file ( layout ).<\/p>\n<pre>&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    android:orientation=\"vertical\"\r\n    android:paddingBottom=\"@dimen\/activity_vertical_margin\"\r\n    android:paddingLeft=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingRight=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingTop=\"@dimen\/activity_vertical_margin\"\r\n    tools:context=\".MainActivity\"&gt;\r\n\r\n    &lt;LinearLayout\r\n        android:layout_width=\"fill_parent\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:orientation=\"horizontal\"\r\n        android:weightSum=\"2\"&gt;\r\n\r\n        &lt;Button\r\n            android:id=\"@+id\/loadWebPage\"\r\n            android:layout_width=\"0dp\"\r\n            android:layout_height=\"wrap_content\"\r\n            android:layout_marginRight=\"10dp\"\r\n            android:layout_weight=\"1\"\r\n            android:background=\"#444\"\r\n            android:text=\"Load Web Page\"\r\n            android:textColor=\"#fff\"\r\n            android:textSize=\"14sp\"\r\n            android:textStyle=\"bold\" \/&gt;\r\n\r\n        &lt;Button\r\n            android:id=\"@+id\/loadFromStaticHtml\"\r\n            android:layout_width=\"0dp\"\r\n            android:layout_height=\"wrap_content\"\r\n            android:layout_marginLeft=\"10dp\"\r\n            android:layout_weight=\"1\"\r\n            android:background=\"#444\"\r\n            android:text=\"Load Static HTML\"\r\n            android:textColor=\"#fff\"\r\n            android:textSize=\"14sp\"\r\n            android:textStyle=\"bold\" \/&gt;\r\n    &lt;\/LinearLayout&gt;\r\n\r\n    &lt;WebView\r\n        android:id=\"@+id\/simpleWebView\"\r\n        android:layout_width=\"fill_parent\"\r\n        android:layout_height=\"fill_parent\"\r\n        android:layout_marginTop=\"20dp\"\r\n        android:scrollbars=\"none\" \/&gt;\r\n\r\n&lt;\/LinearLayout&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong><\/span> Open\u00a0 \u00a0src -&gt; package -&gt;\u00a0MainActivity.java<\/p>\n<p>In this step we open\u00a0MainActivity\u00a0and\u00a0add the code to\u00a0initiate the web view and two buttons.\u00a0Out of those one button is used for displaying a web page in a webview and other one is used to load a static HTML page in webview.<\/p>\n<pre>package example.gb.webviewexample;\r\n\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.os.Bundle;\r\nimport android.support.v7.widget.ButtonBarLayout;\r\nimport android.view.Menu;\r\nimport android.view.MenuItem;\r\nimport android.view.View;\r\nimport android.webkit.WebView;\r\nimport android.webkit.WebViewClient;\r\nimport android.widget.Button;\r\n\r\npublic class MainActivity extends AppCompatActivity implements View.OnClickListener {\r\n\r\n    WebView simpleWebView;\r\n    Button loadWebPage, loadFromStaticHtml;\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n        \/\/ initiate buttons and a web view\r\n        loadFromStaticHtml = (Button) findViewById(R.id.loadFromStaticHtml);\r\n        loadFromStaticHtml.setOnClickListener(this);\r\n        loadWebPage = (Button) findViewById(R.id.loadWebPage);\r\n        loadWebPage.setOnClickListener(this);\r\n        simpleWebView = (WebView) findViewById(R.id.simpleWebView);\r\n\r\n    }\r\n\r\n    @Override\r\n    public void onClick(View v) {\r\n        switch (v.getId()) {\r\n            case R.id.loadFromStaticHtml:\r\n                \/\/ define static html text\r\n                String customHtml = \"&lt;html&gt;&lt;body&gt;&lt;h1&gt;Hello, AbhiAndroid&lt;\/h1&gt;\" +\r\n                        \"&lt;h1&gt;Heading 1&lt;\/h1&gt;&lt;h2&gt;Heading 2&lt;\/h2&gt;&lt;h3&gt;Heading 3&lt;\/h3&gt;\" +\r\n                        \"&lt;p&gt;This is a sample paragraph of static HTML In Web view&lt;\/p&gt;\" +\r\n                        \"&lt;\/body&gt;&lt;\/html&gt;\";\r\n                simpleWebView.loadData(customHtml, \"text\/html\", \"UTF-8\"); \/\/ load html string data in a web view\r\n                break;\r\n            case R.id.loadWebPage:\r\n                simpleWebView.setWebViewClient(new MyWebViewClient());\r\n                String url = \"https:\/\/abhiandroid.com\/ui\/\";\r\n                simpleWebView.getSettings().setJavaScriptEnabled(true);\r\n                simpleWebView.loadUrl(url); \/\/ load a web page in a web view\r\n                break;\r\n        }\r\n    }\r\n\r\n    private class MyWebViewClient extends WebViewClient {\r\n        @Override\r\n        public boolean shouldOverrideUrlLoading(WebView view, String url) {\r\n            view.loadUrl(url);\r\n            return true;\r\n        }\r\n    }\r\n\r\n    \r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 4:<\/strong><\/span> Open <strong>manifests -&gt; AndroidManifest.xml<\/strong><\/p>\n<p>In this step we open\u00a0Manifest file\u00a0and\u00a0define the internet permission for our app.<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;manifest xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    package=\"example.gb.webviewexample\"&gt;\r\n    &lt;!-- define internet permission for our app --&gt;\r\n    &lt;uses-permission android:name=\"android.permission.INTERNET\" \/&gt;\r\n    &lt;application\r\n        android:allowBackup=\"true\"\r\n        android:icon=\"@mipmap\/ic_launcher\"\r\n        android:label=\"@string\/app_name\"\r\n        android:theme=\"@style\/AppTheme\"&gt;\r\n        &lt;activity\r\n            android:name=\".MainActivity\"\r\n            android:label=\"@string\/app_name\"&gt;\r\n            &lt;intent-filter&gt;\r\n                &lt;action android:name=\"android.intent.action.MAIN\" \/&gt;\r\n\r\n                &lt;category android:name=\"android.intent.category.LAUNCHER\" \/&gt;\r\n            &lt;\/intent-filter&gt;\r\n        &lt;\/activity&gt;\r\n    &lt;\/application&gt;\r\n\r\n&lt;\/manifest&gt;<\/pre>\n<p><strong>\u00a0Output:<\/strong><\/p>\n<p>Now start the AVD in Emulator and run the App.\u00a0Choose either to open webpage or static HTML in WebView by clicking on Button. We open static HTML.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1077\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/WebView-Example-Output-Android.jpg\" alt=\"WebView Example Output Android\" width=\"246\" height=\"354\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/WebView-Example-Output-Android.jpg 246w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/WebView-Example-Output-Android-208x300.jpg 208w\" sizes=\"auto, (max-width: 246px) 100vw, 246px\" \/><\/center>&nbsp;<\/p>\n<hr \/>\n<h4><strong>How to add progressBar in webview &#038; convert website into advance android app<\/strong><\/h4>\n<p><script src=\"https:\/\/fast.wistia.com\/embed\/medias\/gxrjsqwnhr.jsonp\" async><\/script><script src=\"https:\/\/fast.wistia.com\/assets\/external\/E-v1.js\" async><\/script><\/p>\n<div class=\"wistia_responsive_padding\" style=\"padding:62.5% 0 0 0;position:relative;\">\n<div class=\"wistia_responsive_wrapper\" style=\"height:100%;left:0;position:absolute;top:0;width:100%;\">\n<div class=\"wistia_embed wistia_async_gxrjsqwnhr videoFoam=true\" style=\"height:100%;width:100%\">&nbsp;<\/div>\n<\/div>\n<\/div>\n<p><a href=\"\/sourcecode\/webview\/\" target=\"_blank\" rel=\"nofollow noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-271\" src=\"\/createandroidapp\/wp-content\/uploads\/2017\/02\/convert-website-into-android-app-banner.png\" alt=\"Convert-website-into-advance-android-app-banner\" width=\"487\" height=\"331\" \/><\/a><\/p>\n<h4><strong>Convert Website Into App Using WebView:<\/strong><\/h4>\n<p>Now you have learn WebView, so why not try\u00a0converting any website into Android App? Read our advance\u00a0<a href=\"\/createandroidapp\/webview-android-app\">WebView Android App tutorial<\/a>\u00a0to learn how to\u00a0do it easily.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Android, WebView is a view used to display the web pages in application. This class is the basis upon which you can roll your own web browser or simply use\u00a0it\u00a0to display some online content within your Activity. We can also specify HTML string and can show it inside our application using a WebView. Basically, &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/webview\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">WebView Tutorial With Example In Android Studio<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"_acf_changed":false,"footnotes":""},"class_list":["post-984","page","type-page","status-publish","hentry"],"acf":[],"psp_head":"<title>WebView Tutorial With Example In Android Studio \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Learn WebView and method used in Android with example in Android Studio. WebView is a view used to display the web pages in application.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/webview\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/984","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/comments?post=984"}],"version-history":[{"count":3,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/984\/revisions"}],"predecessor-version":[{"id":2814,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/984\/revisions\/2814"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=984"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}