All you need to do to integrate Linkify into your application is to simply inject the script tag below into the HTML header of a web page.
<script type="text/javascript"
src="http://www-static.linkify.mobi/api/linkify.js?key=YOUR_API_KEY">
</script>
We will show an example of how to integrate Linkify into your Android WebView application. We will inject the JavaScript code to a web page (we will use Yahoo News! as an example).
To add a WebView into your application, simply include the <WebView> element in your activity layout that is placed in res/layout/activity_main.xml. The code below will add a WebView on your screen.
<?xml version="1.0" encoding="utf-8"?>
<WebView
xmls:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_width="fill_parent"
/>
Since WebView requires Internet access, you will need to enable Internet access by adding the following codes in your manifest file, which is placed in AndroidManifest.xml.
<manifest ... >
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
Here is an example of a Java Activity showing how to load the WebView and integrate Linkify. Before loading any web page, make an instance of WebView and enable the JavaScript. Then, set up the WebView client to integrate Linkify by injecting the JavaScript URL. Finally, load Yahoo! News.
Notice that in the example below, we have loaded Linkify right after the application loaded. You may edit it to start Linkify by pressing some kind of button or any kind of actions. We recommend copy & pasting the setWebViewClient method from the Linkify page since there exists an environment variable, YOUR_API_KEY inside the code below.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WebView webView = (WebView)findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavascriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:(function(){if(!window.linkified){var d=document,s=d.createElement('script');s.type='text/javascript';s.src='http://www-static.linkify.mobi/api/linkify.js?key=YOUR_API_KEY';d.getElementsByTagName('head')[0].appendChild(s);window.linkified=true;}})()");
}
});
webView.loadUrl("http://news.yahoo.com");
}