Wednesday, July 28, 2010

Create your own Browser using WebView

Using a WebView activity, you can display webpages over the Internet or display webpages or any other content from your device's drive. You might use this webview as a Webkit in your applications to display web contents, which can even have integrated Javascripts.


WebView activity will use the Internet, so you must add the Internet usage permission in your AndroidManifest.xml file.


 <uses-permission android:name = "android.permission.INTERNET"/>
In order to create a WebView, firstly you must create your layout including a WebView content. You can put a WebView into your layout xml file and in set its width and height as "fill_parent" to reach a fullscreen view.


Then in your main Activity, define a WebView and associate it with your layout.

 mWebView = (WebView) findViewById(R.id.web_view);
For a WebView you have a few choices about displaying contents. You can open a URL, you can create your HTML code inside your Activity and display it, you can open HTML files from your device's drive and also you can display other contents from your device's drive.


 // Loading a URL
 mWebView.loadUrl("http://www.androtips.com");

 // Loading HTML code created in the Activity
 String data = "<html><body><h2>Hello WebView</h2></body></html>";
 String mimeType = "text/html";
 String encoding = "utf-8";
 mWebView.loadData(data , mimeType , encoding);
 // Loading an HTML file or any content from sd card
 String filePath = "file:///data/data/com.webkitdemo/test.html";
 mWebView.loadUrl(filePath);

Now you have a basic WebView to display your contents. The problem with this default WebView is, when you click to a link in the webpage, Android will attempt to open its default Browser. Of course we want to open any links from our WebView open in our own WebView. To do this, we must add this small inner-class to our Activity.

 private class mWebViewClient extends WebViewClient {
    
     /** Opens new links in the same WebView instead of opening it in Browser Application.
* 
      */
     @Override
        public boolean shouldOverrideUrlLoading(WebView view , String url) {
view.loadUrl(url);
            return true;
        }
    }

Another good feature to add to our activity is setting the behavior of the Back button of device. So that we can go back from a link we went from our WebView.

 @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

Because we have small display sizes on mobile devices, we might need to add zoom controls to our WebView to enhance readability. In order to add zoom controls, you must add this controls into your onCreate method.

 @Override
    public void onCreate(Bundle savedInstanceState) {
        ...
        ...
// Zoom controls
        FrameLayout mContentView = (FrameLayout) getWindow().getDecorView().findViewById(android.R.id.content);
        final View zoom = this.mWebView.getZoomControls();
        mContentView.addView(zoom, ZOOM_PARAMS);
        zoom.setVisibility(View.GONE);
...
...
}

Your WebView is ready to use now. You can display any content in this WebView and integrate it into your Applications.


Enjoy!

9 comments:

  1. I’ve been searching for some decent stuff on the subject and haven't had any luck up until this point, You just got a new biggest fan!.. download UC mini

    ReplyDelete
    Replies
    1. Big data is a term that describes the large volume of data – both structured and unstructured – that inundates a business on a day-to-day basis. big data projects for students But it’s not the amount of data that’s important.Project Center in Chennai

      Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Corporate TRaining Spring Framework the authors explore the idea of using Java in Big Data platforms.

      Spring Training in Chennai

      The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

      Delete
  2. Interesting post. I Have Been wondering about this issue, so thanks for posting. Pretty cool post.It 's really very nice and Useful post.Thanks Search Bar Firefox 57 Quantum addon

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. It's really worth reading the blog post whcih has useful content. I have recently done CRS Info Solutions which is highly helpful to upgrade my career. What is the Salesforce course training fee in Chennai? Which is the best option from Salesforce Certification Training in Chennai? Who offers job oriented Salesforce training in Chennai? Find the best institute for project based Salesforce training in Chennai.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Full-pay 9-6 machines are most typical in Nevada, however they are often present in Illinois, Indiana, Mississippi, Missouri -- indeed, nearly anywhere could be} competitors for players. Even in Nevada, some casinos offer the 8-5 pay desk, generally even alternating 9-6 and 8-5 pay tables on the similar financial institution of machines. Be sure to learn the pay desk earlier than sitting all the way down 우리카지노 to} play. If you are be} in an space with several of} casinos inside walking distance, do not settle for a pay desk that's under the usual for the area.

    ReplyDelete
  7. The subtractive process of CNC machining provides multiple of} advantages for both prototyping and low-volume manufacturing parts. Work with a trusted U.S.-based producer and remove the risk of sending parts overseas. You can even call or e mail us at any time and we'll help with ordering parts, design feedback, materials suggestions, and answer any questions. CNC Waterjet – Water cutters use a high-pressure stream of water to cut via the steel, offering a excessive stage of accuracy and an excellent good} floor finish since there aren't any heat-effected edges. I bought this CNC mill for mildew making with aluminum and copper. You shouldn't beat Vacuum Storage Bags the worth for what this machine is able to doing for hobbyists.

    ReplyDelete