<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>lexanderA &#187; WebChromeClient</title>
	<atom:link href="http://lexandera.com/tag/webchromeclient/feed/" rel="self" type="application/rss+xml" />
	<link>http://lexandera.com</link>
	<description>A blog about the web, mobile web, semantic web and mobile semantic web.</description>
	<lastBuildDate>Sun, 06 Jun 2010 18:27:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Adding alert() support to a WebView</title>
		<link>http://lexandera.com/2009/01/adding-alert-support-to-a-webview/</link>
		<comments>http://lexandera.com/2009/01/adding-alert-support-to-a-webview/#comments</comments>
		<pubDate>Tue, 27 Jan 2009 20:54:09 +0000</pubDate>
		<dc:creator>Aleksander Kmetec</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[WebView examples]]></category>
		<category><![CDATA[WebChromeClient]]></category>
		<category><![CDATA[WebView]]></category>

		<guid isPermaLink="false">http://lexandera.com/?p=191</guid>
		<description><![CDATA[As promised earlier, here is an example of how to add support for alert() function to a WebView in your Android application:


final WebView browser = (WebView)findViewById(R.id.browser);
/* JavaScript must be enabled if you want it to work, obviously */
browser.getSettings().setJavaScriptEnabled(true);

final Context myApp = this;

/* WebChromeClient must be set BEFORE calling loadUrl! */
browser.setWebChromeClient(new WebChromeClient() {
    [...]]]></description>
			<content:encoded><![CDATA[<p>As promised earlier, here is an example of how to add support for alert() function to a WebView in your <a class="zem_slink" href="http://code.google.com/android/" title="Android" rel="homepage">Android</a> application:</p>
<pre name="code" class="java">

final WebView browser = (WebView)findViewById(R.id.browser);
/* JavaScript must be enabled if you want it to work, obviously */
browser.getSettings().setJavaScriptEnabled(true);

final Context myApp = this;

/* WebChromeClient must be set BEFORE calling loadUrl! */
browser.setWebChromeClient(new WebChromeClient() {
    @Override
    public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result)
    {
        new AlertDialog.Builder(myApp)
            .setTitle(&quot;javaScript dialog&quot;)
            .setMessage(message)
            .setPositiveButton(android.R.string.ok,
                    new AlertDialog.OnClickListener()
                    {
                        public void onClick(DialogInterface dialog, int which)
                        {
                            result.confirm();
                        }
                    })
            .setCancelable(false)
            .create()
            .show();

        return true;
    };
});

/* load a web page which uses the alert() function */
browser.loadUrl(&quot;http://lexandera.com/files/jsexamples/alert.html&quot;);
</pre>
<p>Code for adding support for confirm() and prompt() is almost identical and can be found in <a href="http://code.google.com/p/mosembro/source/browse/trunk/src/com/lexandera/mosembro/Mosembro.java?spec=svn12&amp;r=12#137">Mosembro&#8217;s source code</a>. </p>
<div style="margin-top: 10px; height: 15px;" class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/62e34ae6-3382-45cb-b4f5-f08a367c4a80/" title="Zemified by Zemanta"><img style="border: medium none ; float: right;" class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=62e34ae6-3382-45cb-b4f5-f08a367c4a80" alt="Reblog this post [with Zemanta]"></a></div>
]]></content:encoded>
			<wfw:commentRss>http://lexandera.com/2009/01/adding-alert-support-to-a-webview/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Android WebChromeClient</title>
		<link>http://lexandera.com/2009/01/android-webchromeclient/</link>
		<comments>http://lexandera.com/2009/01/android-webchromeclient/#comments</comments>
		<pubDate>Tue, 20 Jan 2009 03:30:16 +0000</pubDate>
		<dc:creator>Aleksander Kmetec</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[WebView examples]]></category>
		<category><![CDATA[browser]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[WebChromeClient]]></category>
		<category><![CDATA[WebView]]></category>

		<guid isPermaLink="false">http://lexandera.com/?p=179</guid>
		<description><![CDATA[My blog stats are telling me that many visitors are arriving here looking for examples on how to implement WebChromeClient on Android. I don&#8217;t (yet) have a nice article about how to do it, but you can have a look at how I implemented WebChromeClient in Mosembro, if you came here for that reason. Just [...]]]></description>
			<content:encoded><![CDATA[<p>My blog stats are telling me that many visitors are arriving here looking for examples on how to implement <a href="http://code.google.com/android/reference/android/webkit/WebChromeClient.html">WebChromeClient</a> on <a class="zem_slink" title="Android" rel="homepage" href="http://code.google.com/android/">Android</a>. I don&#8217;t (yet) have a nice article about how to do it, but you can <a href="http://code.google.com/p/mosembro/source/browse/trunk/src/com/lexandera/mosembro/Mosembro.java">have a look at how I implemented WebChromeClient in Mosembro</a>, if you came here for that reason. Just use your browser&#8217;s search functionality and look for &#8220;new WebChromeClient&#8221;.</p>
<div style="margin-top: 10px; height: 15px;" class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/b89819f7-2769-4fb9-91f8-ab40a0b3e67b/" title="Zemified by Zemanta"><img style="border: medium none ; float: right;" class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=b89819f7-2769-4fb9-91f8-ab40a0b3e67b" alt="Reblog this post [with Zemanta]"></a></div>
]]></content:encoded>
			<wfw:commentRss>http://lexandera.com/2009/01/android-webchromeclient/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Injecting JavaScript into a WebView</title>
		<link>http://lexandera.com/2009/01/injecting-javascript-into-a-webview/</link>
		<comments>http://lexandera.com/2009/01/injecting-javascript-into-a-webview/#comments</comments>
		<pubDate>Fri, 02 Jan 2009 06:05:21 +0000</pubDate>
		<dc:creator>Aleksander Kmetec</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[WebView examples]]></category>
		<category><![CDATA[WebChromeClient]]></category>
		<category><![CDATA[WebView]]></category>
		<category><![CDATA[WebViewClient]]></category>

		<guid isPermaLink="false">http://lexandera.com/?p=64</guid>
		<description><![CDATA[Back in early &#8216;08, while the Androd developer challenge was in full swing, a fairly common question which went without a satisfactory answer was: &#8220;How can I manipulate the contents of a WebView?&#8221;. I now finally have the answer, thanks to a couple of lines of code I stumbled upon while looking at the source [...]]]></description>
			<content:encoded><![CDATA[<p>Back in early &#8216;08, while the <a href="http://code.google.com/android/adc.html">Androd developer challenge</a> was in full swing, a fairly common question which went without a satisfactory answer was: &#8220;How can I manipulate the contents of a WebView?&#8221;. I now finally have the answer, thanks to a couple of lines of code I stumbled upon while looking at the source of <a href="http://www.jsharkey.org/" title="Jeffrey Sharkey" rel="homepage" class="zem_slink">Jeffrey Sharkey</a>&#8217;s <a href="http://www.jsharkey.org/blog/2008/12/15/oilcan-greasemonkey-on-steroids-for-android/">OilCan</a>.</p>
<p>The solution is actually very simple: wait for the original page to finish loading, then inject your own JavaScript code into it by calling webview.loadUrl(&#8220;javascript:your-code-here&#8221;) when your webvew&#8217;s onPageFinished() event is triggered. Loading an URL beginning with &#8220;javascript:&#8221; is the exact same approach used by <a href="http://en.wikipedia.org/wiki/Bookmarklet" title="Bookmarklet" rel="wikipedia" class="zem_slink">bookmarklets</a>.</p>
<p>Here&#8217;s a simple example which loads a page and then sets its text color to red:</p>
<pre name="code" class="java">

final WebView webview = (WebView)findViewById(R.id.browser);
/* JavaScript must be enabled if you want it to work, obviously */
webview.getSettings().setJavaScriptEnabled(true);

/* WebViewClient must be set BEFORE calling loadUrl! */
webview.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url)
    {
        webview.loadUrl(&quot;javascript:(function() { &quot; +
                &quot;document.getElementsByTagName(&#039;body&#039;)[0].style.color = &#039;red&#039;; &quot; +
                &quot;})()&quot;);
    }
});

webview.loadUrl(&quot;http://code.google.com/android&quot;);
</pre>
<p>That&#8217;s it.</p>
<p>Please note though that <em>alert()</em> and <em>confirm()</em> JavaScript functions will not work unless you also register a <a href="http://code.google.com/android/reference/android/webkit/WebChromeClient.html">WebChromeClient</a> which implements the required methods.</p>
<div style="margin-top: 10px; height: 15px;" class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/75a6582e-c459-4c4e-848b-e48a050ab4c9/" title="Zemified by Zemanta"><img style="border: medium none ; float: right;" class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=75a6582e-c459-4c4e-848b-e48a050ab4c9" alt="Reblog this post [with Zemanta]"></a></div>
]]></content:encoded>
			<wfw:commentRss>http://lexandera.com/2009/01/injecting-javascript-into-a-webview/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
