Create your own Android Web Browser
Submitted by pavel7_7_7 on Thursday, September 11, 2014 - 01:45.
Android provides possibility for developers to create a custom window for browsing web pages or even create a clone of the Web Browser. This can be done using WebView element. This class uses WebKit Engine and it provides a huge number of methods. We will use some basic methods in this program.
Create new Android project
The layout file is really simple. We just want to have a window with WebView.
Now we have to create an object of WebView in the Activity and initialize it.
Add a field to the Activity class:
The next step is to initialize WebView:
After this we can enable JavaScript in this WebView:
And the last step is to set the web page to be loaded for WebView:
Because of the fact this application uses Internet, we need to add permission to the manifest file:
It's working now, but there is a little problem. If you want to navigate any link, you will be asked to open browser. Sometimes, you want to display all the information in your own application.
To resolve this issue you need add a WebViewClient to the WebView.
This can be done by creating a new class, that extends WebViewClient class and overrides
method. This is done in a simple way: add new inner class in Activity class:
This class gives possibility to load any url in the WebView.For this functionality
method is corresponding. These method
Now you can run the application and test it. All the pages will be opened in your WebView.
You can see, that it would be great,if we can navigate back in the application.This can be done by handling key down event. We should override
public boolean onKeyDown(int keyCode, KeyEvent event) method of the activity class:
As you can see it is done by using goBack() method.The if statement checks if the back button was pressed and if there is any possibility to go back.
All the others keys are handled by the method from the super class.
DemoWebBrowserwith blank Activity. Now you need to change the layout of the MainActivity. Replace the context of the main layout file to this:
- <?xml version="1.0" encoding="utf-8"?>
- <WebView xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/myWebView"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- />
- WebView myWebView;
- myWebView = (WebView) findViewById(R.id.myWebView);
- myWebView.getSettings().setJavaScriptEnabled(true);
- myWebView.loadUrl("http://www.sourcecodester.com/");
- <uses-permission android:name="android.permission.INTERNET" />
- private class DemoWebViewClient extends WebViewClient
- {
- @Override
- {
- view.loadUrl(url);
- return true;
- }
- }
tellsto the system that there is no need to open installed Web Browser. And the current WebView can load this URL. Now we can set Web client for WebView in onCreate method:
- myWebView.setWebViewClient(new DemoWebViewClient());
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
Add new comment
- 435 views