{"id":252,"date":"2014-02-17T20:55:28","date_gmt":"2014-02-17T19:55:28","guid":{"rendered":"http:\/\/michalu.eu\/wordpress\/?p=252"},"modified":"2014-02-18T06:45:47","modified_gmt":"2014-02-18T05:45:47","slug":"tutorial-run-javascript-in-android-webview-to-hide-div","status":"publish","type":"post","link":"https:\/\/michalu.eu\/wordpress\/tutorial-run-javascript-in-android-webview-to-hide-div\/","title":{"rendered":"Tutorial: Run JavaScript in Android WebView to hide div"},"content":{"rendered":"<p>We need to run JavaScript after page load in Android webView. It is very easy, but how to do that? We\u2019ll look at this closer in the tutorial.<\/p>\n<p>What would we like to achieve?<\/p>\n<table width=\"100%\">\n<tbody>\n<tr>\n<td><a href=\"http:\/\/michalu.eu\/wordpress\/wp-content\/uploads\/2014\/02\/before.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-253 aligncenter\" alt=\"before\" src=\"http:\/\/michalu.eu\/wordpress\/wp-content\/uploads\/2014\/02\/before-200x300.png\" width=\"200\" height=\"300\" srcset=\"https:\/\/michalu.eu\/wordpress\/wp-content\/uploads\/2014\/02\/before-200x300.png 200w, https:\/\/michalu.eu\/wordpress\/wp-content\/uploads\/2014\/02\/before.png 320w\" sizes=\"auto, (max-width: 200px) 100vw, 200px\" \/><\/a><\/td>\n<td><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" alt=\"\" src=\"http:\/\/upload.wikimedia.org\/wikipedia\/commons\/1\/12\/Right_arrow.svg\" width=\"94\" height=\"79\" \/><\/td>\n<td><a href=\"http:\/\/michalu.eu\/wordpress\/wp-content\/uploads\/2014\/02\/disabled.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-254 aligncenter\" alt=\"disabled\" src=\"http:\/\/michalu.eu\/wordpress\/wp-content\/uploads\/2014\/02\/disabled-200x300.png\" width=\"200\" height=\"300\" srcset=\"https:\/\/michalu.eu\/wordpress\/wp-content\/uploads\/2014\/02\/disabled-200x300.png 200w, https:\/\/michalu.eu\/wordpress\/wp-content\/uploads\/2014\/02\/disabled.png 320w\" sizes=\"auto, (max-width: 200px) 100vw, 200px\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><!--more-->Static (in this case) HTML resource located in Android assets. I used jQueryMobile to make the webpage more&#8230; &#8216;mobile&#8217;:<\/p>\n<pre class=\"brush: xml; highlight: [12,20,23,26,29,32]; title: ; notranslate\" title=\"\">&lt;!DOCTYPE html&gt;\r\n\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;meta charset=&quot;utf-8&quot;&gt;\r\n&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;\r\n&lt;title&gt;michalu-examples&lt;\/title&gt;\r\n\r\n&lt;link rel=&quot;stylesheet&quot; type=&quot;text\/css&quot; href=&quot;http:\/\/code.jquery.com\/mobile\/latest\/jquery.mobile.min.css&quot; \/&gt;\r\n&lt;script type=&quot;text\/javascript&quot; src=&quot;http:\/\/code.jquery.com\/jquery-1.7.1.min.js&quot;&gt;&lt;\/script&gt;\r\n&lt;script type=&quot;text\/javascript&quot; src=&quot;http:\/\/code.jquery.com\/mobile\/latest\/jquery.mobile.min.js&quot;&gt;&lt;\/script&gt;\r\n&lt;script type=&quot;text\/javascript&quot;&gt;function disableSection(id){$('#'+id+' a').addClass('ui-disabled');}&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n &lt;div data-role=&quot;page&quot; data-control-title=&quot;Home&quot; id=&quot;page1&quot;&gt;\r\n &lt;div data-theme=&quot;a&quot; data-role=&quot;header&quot;&gt;\r\n &lt;h3&gt;michalu.eu | examples&lt;\/h3&gt;\r\n &lt;\/div&gt;\r\n &lt;div data-role=&quot;content&quot;&gt;\r\n &lt;div id=&quot;section1&quot;&gt;\r\n &lt;a href=&quot;#&quot; data-iconpos=&quot;right&quot; data-icon=&quot;arrow-r&quot; data-role=&quot;button&quot;&gt;Section1&lt;\/a&gt;\r\n &lt;\/div&gt;\r\n &lt;div id=&quot;section2&quot;&gt;\r\n &lt;a href=&quot;#&quot; data-iconpos=&quot;right&quot; data-icon=&quot;arrow-r&quot; data-role=&quot;button&quot;&gt;Section2&lt;\/a&gt;\r\n &lt;\/div&gt;\r\n &lt;div id=&quot;section3&quot;&gt;\r\n &lt;a href=&quot;#&quot; data-iconpos=&quot;right&quot; data-icon=&quot;arrow-r&quot; data-role=&quot;button&quot;&gt;Section3&lt;\/a&gt;\r\n &lt;\/div&gt;\r\n &lt;div id=&quot;section4&quot;&gt;\r\n &lt;a href=&quot;#&quot; data-iconpos=&quot;right&quot; data-icon=&quot;arrow-r&quot; data-role=&quot;button&quot;&gt;Section4&lt;\/a&gt;\r\n &lt;\/div&gt;\r\n &lt;div id=&quot;section5&quot;&gt;\r\n &lt;a href=&quot;#&quot; data-iconpos=&quot;right&quot; data-icon=&quot;arrow-r&quot; data-role=&quot;button&quot;&gt;Section5&lt;\/a&gt;\r\n &lt;\/div&gt;\r\n &lt;\/div&gt;\r\n &lt;\/div&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>JavaScript<strong> disableSection(id)<\/strong> listed below disables div at given id:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">function disableSection(id){$('#'+id+' a').addClass('ui-disabled');}<\/pre>\n<p>We need to call it after successfully loading a webpage in our example activity. But before it we should:<\/p>\n<ul>\n<li><strong>enable<\/strong> JavaScript engine (line 23)<\/li>\n<li>override <strong>onPageFinished<\/strong> (line 26)<\/li>\n<li>and run <strong>disableSection<\/strong> on each element from <strong>idsToHide<\/strong> array (line 33)<\/li>\n<li>of course remember about loading resource into your webView(in this case HTML5 from local assets) (line 38)<\/li>\n<\/ul>\n<pre class=\"brush: java; highlight: [23,26,33,38]; title: ; notranslate\" title=\"\">package eu.michalu;\r\n\r\nimport android.app.Activity;\r\nimport android.os.Bundle;\r\nimport android.webkit.WebView;\r\nimport android.webkit.WebViewClient;\r\n\r\npublic class WebViewActivity extends Activity {\r\n WebView mWebView;\r\n \/*\r\n * array of div's ids to disable\/hide\r\n *\/\r\n String&#x5B;] idsToHide = { &quot;section1&quot;, &quot;section3&quot;, &quot;section5&quot; };\r\n\r\n@Override\r\n public void onCreate(Bundle savedInstanceState) {\r\n \/\/ TODO Auto-generated method stub\r\n super.onCreate(savedInstanceState);\r\n setContentView(R.layout.main);\r\n\r\nmWebView = (WebView) findViewById(R.id.activity_webview);\r\n \/\/enable javascript engine\r\n mWebView.getSettings().setJavaScriptEnabled(true);\r\n mWebView.setWebViewClient(new WebViewClient() {\r\n @Override\r\n public void onPageFinished(WebView view, String url) {\r\n \/\/ TODO Auto-generated method stub\r\n super.onPageFinished(view, url);\r\n\r\n \/\/run 'disableSection' for all divs to hide\/disable\r\n for (String s : idsToHide) {\r\n String surveyId = s;\r\n view.loadUrl(&quot;javascript:disableSection('&quot; + surveyId + &quot;');&quot;);\r\n }\r\n }\r\n });\r\n \/\/load webpage from assets\r\n mWebView.loadUrl(&quot;file:\/\/\/android_asset\/list.html&quot;);\r\n }\r\n}<\/pre>\n<p>All code is available on my github\u00a0<a title=\"https:\/\/github.com\/fockeRR\/michalueu-examples\/tree\/master\/webview_js\" href=\"https:\/\/github.com\/fockeRR\/michalueu-examples\/tree\/master\/webview_js\" target=\"_blank\">profile<\/a>\u00a0or just download:<\/p>\n<ul>\n<li><a title=\"http:\/\/michalu.eu\/wordpress\/wp-content\/uploads\/2014\/02\/webview_js.zip\" href=\"http:\/\/michalu.eu\/wordpress\/wp-content\/uploads\/2014\/02\/webview_js.zip\" target=\"_blank\">project<\/a><\/li>\n<li><a title=\"http:\/\/michalu.eu\/wordpress\/wp-content\/uploads\/2014\/02\/webview_js_apk.zip\" href=\"http:\/\/michalu.eu\/wordpress\/wp-content\/uploads\/2014\/02\/webview_js_apk.zip\" target=\"_blank\">apk<\/a><\/li>\n<\/ul>\n<p><a title=\"https:\/\/github.com\/fockeRR\/michalueu-examples\/tree\/master\/flickr-michalueu\" href=\"https:\/\/github.com\/fockeRR\/michalueu-examples\/tree\/master\/webview_js\"><img loading=\"lazy\" decoding=\"async\" alt=\"github_logo\" src=\"http:\/\/michalu.eu\/wordpress\/wp-content\/uploads\/2013\/03\/github_logo-300x93.png\" width=\"300\" height=\"93\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We need to run JavaScript after page load in Android webView. It is very easy, but how to do that? [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[8,27,89,88],"tags":[100,106,90,105],"class_list":["post-252","post","type-post","status-publish","format-standard","hentry","category-android","category-blog","category-javascript","category-webview","tag-android","tag-javascript","tag-onpagefinished","tag-webview"],"_links":{"self":[{"href":"https:\/\/michalu.eu\/wordpress\/wp-json\/wp\/v2\/posts\/252","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/michalu.eu\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michalu.eu\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michalu.eu\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/michalu.eu\/wordpress\/wp-json\/wp\/v2\/comments?post=252"}],"version-history":[{"count":8,"href":"https:\/\/michalu.eu\/wordpress\/wp-json\/wp\/v2\/posts\/252\/revisions"}],"predecessor-version":[{"id":267,"href":"https:\/\/michalu.eu\/wordpress\/wp-json\/wp\/v2\/posts\/252\/revisions\/267"}],"wp:attachment":[{"href":"https:\/\/michalu.eu\/wordpress\/wp-json\/wp\/v2\/media?parent=252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michalu.eu\/wordpress\/wp-json\/wp\/v2\/categories?post=252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michalu.eu\/wordpress\/wp-json\/wp\/v2\/tags?post=252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}