In this post i will show you how to create a simple browser for android.
First I will discuss about the basic terms and then i will explain the code:
WebView :-
In simple word this is just a view that can display web pages. This is a Basic class which will allow you to display some online content. This class can be used to create a simple browser. It uses the Web-Kit rendering engine to display web pages and it also includes some methods which is used to navigate forward and backward through history, clear cache, zoom in and out, perform text searches.
To have access to the internet connection you should give permission in the Manifest file. Then it will allow to access INTERNET and load pages.
To use WebView use JavaScript. By default java Script is disabled, so we need to enable it.
We can retrieve WebSettings with getSettings() and then enable JavaScript with setJavaScriptEnabled().
To bind a new interface between your JavaScript and Android code, call
Controlling Page Navigation:-
When the user click a link from a web page in WebView, the android by default launches a application that handles URLs. By default the web browser opens and loads the destination URL. However we can Override this behavior, thus we can allow the user to navigate backward and forward through the web page history. WebviewClient is used in WebView and using setWebViewClient() we can open the links clicked.
To have more control we have to create our own WebviewClient that overrides shouldOverrideUrlLoading() methord.
There are two xml files in layout, One is used to define the menus and other one defines the basic GUI of the Browser.
menu.xml - for Menus
browser.xml- Basic Design for the Browser
The Main java code Browser.java
Link for code :- https://www.dropbox.com/s/llze0eycsspceo1/Browserapp.rar
First I will discuss about the basic terms and then i will explain the code:
WebView :-
In simple word this is just a view that can display web pages. This is a Basic class which will allow you to display some online content. This class can be used to create a simple browser. It uses the Web-Kit rendering engine to display web pages and it also includes some methods which is used to navigate forward and backward through history, clear cache, zoom in and out, perform text searches.
To have access to the internet connection you should give permission in the Manifest file. Then it will allow to access INTERNET and load pages.
<uses-permission android:name="android.permission.INTERNET"/>
Using JavaScript:-To use WebView use JavaScript. By default java Script is disabled, so we need to enable it.
We can retrieve WebSettings with getSettings() and then enable JavaScript with setJavaScriptEnabled().
To bind a new interface between your JavaScript and Android code, call
addJavascriptInterface
(), passing it
a class instance to bind to your JavaScript and an interface name that your JavaScript can call to
access the class.addJavascriptInterface
() allows javaScript to control the Android Application. This can cause secuirity issue. You shouldn't use addJavascriptInterface
() unless all of the HTML and JavaScript is written by you for your WebView.Controlling Page Navigation:-
When the user click a link from a web page in WebView, the android by default launches a application that handles URLs. By default the web browser opens and loads the destination URL. However we can Override this behavior, thus we can allow the user to navigate backward and forward through the web page history. WebviewClient is used in WebView and using setWebViewClient() we can open the links clicked.
To have more control we have to create our own WebviewClient that overrides shouldOverrideUrlLoading() methord.
There are two xml files in layout, One is used to define the menus and other one defines the basic GUI of the Browser.
menu.xml - for Menus
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/back"
android:title="Back" />
<item
android:id="@+id/frd"
android:title="Forward" />
<item
android:id="@+id/refresh"
android:title="Refresh" />
<item
android:id="@+id/stop"
android:title="Stop" />
</menu>
browser.xml- Basic Design for the Browser
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/url"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="softwaretechiess.blogspot.in"
android:ems="10" />
<Button
android:id="@+id/go"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go" />
</TableRow>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="400dp" />
</LinearLayout>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/status"
android:layout_width="0dp"
android:layout_marginTop="10dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
</TableLayout>
The Main java code Browser.java
1: package com.example.browserapp;
2: import android.annotation.SuppressLint;
3: import android.app.Activity;
4: import android.graphics.Bitmap;
5: import android.os.Bundle;
6: import android.view.Menu;
7: import android.view.MenuInflater;
8: import android.view.MenuItem;
9: import android.view.View;
10: import android.view.View.OnClickListener;
11: import android.webkit.WebView;
12: import android.webkit.WebViewClient;
13: import android.widget.Button;
14: import android.widget.EditText;
15: import android.widget.TextView;
16: @SuppressLint("JavascriptInterface")
17: public class Browser extends Activity implements OnClickListener{
18: String myblogurl = "http://softwaretechiess.blogspot.in/";
19: EditText url;
20: TextView status;
21: WebView web;
22: Button go;
23: String geturl;
24: int m_nHTMLSize = 0;
25: @Override
26: protected void onCreate(Bundle savedInstanceState) {
27: super.onCreate(savedInstanceState);
28: setContentView(R.layout.browser);
29: url = (EditText)findViewById(R.id.url);
30: web = (WebView)findViewById(R.id.webView);
31: status = (TextView)findViewById(R.id.status);
32: go = (Button)findViewById(R.id.go);
33: go.setOnClickListener(this);
34: web.getSettings().setJavaScriptEnabled(true);
35: web.setWebViewClient(new MyWebViewClient());
36: if (myblogurl != null)
37: web.loadUrl(myblogurl);
38: web.addJavascriptInterface(new JavaScriptInterface(), "HTMLOUT");
39: }
40: class JavaScriptInterface{
41: public void showHTML(String html) {
42: m_nHTMLSize = 0;
43: if (html !=null) {
44: m_nHTMLSize = html.length();
45: }
46: }
47: }
48: private class MyWebViewClient extends WebViewClient {
49: @Override
50: public void onPageStarted(WebView view, String url, Bitmap favicon) {
51: status.setText("Loading page...");
52: }
53: @Override
54: public void onPageFinished(WebView view, String url) {
55: status.setText("Ready");
56: web.loadUrl("javascript:window.HTMLOUT.showHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
57: }
58: @Override
59: public boolean shouldOverrideUrlLoading(WebView view, String url) {
60: view.loadUrl(url);
61: return true;
62: }
63: }
64: @Override
65: public boolean onCreateOptionsMenu(Menu menu) {
66: MenuInflater menuInflater = getMenuInflater();
67: menuInflater.inflate(R.layout.menu, menu);
68: return true;
69: }
70: @Override
71: public boolean onOptionsItemSelected(MenuItem item)
72: {
73: switch (item.getItemId())
74: {
75: case R.id.back:
76: web.goBack();
77: return true;
78: case R.id.frd:
79: web.goForward();
80: return true;
81: case R.id.refresh:
82: web.reload();
83: return true;
84: case R.id.stop:
85: web.stopLoading();
86: return true;
87: default:
88: return super.onOptionsItemSelected(item);
89: }
90: }
91: @Override
92: public void onClick(View arg0) {
93: // TODO Auto-generated method stub
94: geturl = url.getText().toString().trim();
95: geturl = geturl.replaceAll(" ", "");
96: if(geturl!=null) web.loadUrl("http://"+geturl);
97: }
98: }
Link for code :- https://www.dropbox.com/s/llze0eycsspceo1/Browserapp.rar
No comments:
Post a Comment