PocketMagic

PocketMagic

Where Technology meets magic


Android
47 Posts
BlackBerry
4 Posts
Electronics
69 Posts
Hardware
123 Posts
High Voltage
49 Posts
Image processing
2 Posts
iPhone
4 Posts
Linux
2 Posts
Nuclear
20 Posts
Optics
11 Posts
Photography
7 Posts
Photoshop
3 Posts
Research
19 Posts
Reviews
18 Posts
Robotics
8 Posts
Security
7 Posts
Software
76 Posts
Symbian
2 Posts
Tubes
20 Posts
Windows
3 Posts
Windows Mobile
11 Posts

Top Articles!       See PocketMagic on Facebook


Building a robot – Part 2 | 8851 Views | Rate 67.05
Building a robot – Part 2
uRADMonitor - Online Radiation monitoring station | 16094 Views | Rate 66.78
uRADMonitor - Online Radiation monitoring station
Bluetooth and iOS - Use Bluetooth in your iPhone apps | 22039 Views | Rate 65.2
Bluetooth and iOS - Use Bluetooth in your iPhone apps
NMEA GPS Library for AVR Microcontrollers | 5814 Views | Rate 51
NMEA GPS Library for AVR Microcontrollers
Programmatically Injecting Events on Android - Part 2 | 6528 Views | Rate 46.63
Programmatically Injecting Events on Android - Part 2
Simple Switched power Supplies | 19038 Views | Rate 45.99
Simple Switched power Supplies
Capacitor Discharge Microspot Welder / Cutter | 13186 Views | Rate 38.9
Capacitor Discharge Microspot Welder / Cutter
Coil Winding machine counter with Atmega8 and Reed relay | 964 Views | Rate 38.56
Coil Winding machine counter with Atmega8 and Reed relay

 
  

Android WebBrowser using WebView


By Radu Motisan Posted on March 18th, 2011 , 3287 Views (Rate 3.99)



  

Based on the info in my previous article I'd like to present the steps to create a simple Android Internet Browser application, with a header, a WebView central area and a footer - all dynamical, without using XMLs:

Some details:

  • In AndroidManifest.xml, you will need to include the INTERNET permissions:
    1. <uses-permission android:name="android.permission.INTERNET" />
  • The interface: you can learn how to use the nice RelativeLayout control and create the interface, as per my previous article on this topic. The user controls are the top address field and the GO button, the BACK, FORWARD, RELOAD and STOP at the bottom.
  • The WebView control: will handle all the HTML rendering and other complex function related to internet browsers. The webview is created between the Header and the Footer, and the content is finger-friendly (finger scrollable). By default the zoom level has been configured to fit the pages in the screen's width.
  • For the WebView control I've enabled JavaScript support and added a WebViewClient class to intercept the various events like page loading events (start/ready):
    1.  
    2. m_web.getSettings().setJavaScriptEnabled(true);
    3. m_web.setWebViewClient(new MyWebViewClient());
    4.  

    Note: The WebView Client class is quite simple:

    1.  
    2. private class MyWebViewClient extends WebViewClient {
    3. @Override
    4. public void onPageStarted(WebView view, String url, Bitmap favicon) {
    5. Log.d(LOG_TAG, "onPageStarted");
    6. m_tv.setText("Loading page...");
    7. //stop button is enabled only when pages are loading
    8. m_bButStop.setEnabled(true);
    9. }
    10. @Override
    11. public void onPageFinished(WebView view, String url) {
    12. Log.d(LOG_TAG, "onPageFinished");
    13. m_tv.setText("Ready ");
    14. //stop button is disabled when pages are already loaded
    15. m_bButStop.setEnabled(false);
    16. // This call inject JavaScript into the page which just finished loading.
    17. m_web.loadUrl("javascript:window.HTMLOUT.showHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
    18. // adjust prev/next buttons, only if history is available
    19. m_bButBack.setEnabled(m_web.canGoBack());
    20. m_bButFwd.setEnabled(m_web.canGoForward());
    21. }
    22.  
    23. @Override
    24. public boolean shouldOverrideUrlLoading(WebView view, String url) {
    25. view.loadUrl(url);
    26. return true;
    27. }
    28.  
    29. }
    30.  

    Details:

  • When the page starts loading (onPageStarted) we set the proper content in the status textview, and we enable the STOP button. This button is enabled when a page is loading, and is disabled when the page is completely loaded, because it doesn't make sense to stop something that has already finished loading, does it?
  • When the page is loaded (onPageFinished) we show "Ready" in the status textview, we disable the STOP button, and we inject a JavaScript so we would have access to the HTML code of the page we've loaded.
    To run the Javascript we need to call :
    1.  
    2. // set java script used to get the HTML code
    3. m_web.addJavascriptInterface(new JavaScriptInterface(), "HTMLOUT");
    4.  
  • Where the JavaScriptInterface is a simple HTML reader based on the "showHTML" function (that we call after a page is loaded):
    1.  
    2. // Used with Webview, to get the HTML code
    3. class JavaScriptInterface{
    4. public void showHTML(String html) {
    5. m_nHTMLSize = 0;
    6. if (html !=null) {
    7. //int i = html.lastIndexOf(""); //search for something in the text
    8. m_nHTMLSize = html.length();
    9. Log.d(LOG_TAG, "HTML content is: "+html+"\nSize:"+m_nHTMLSize+" bytes");
    10.  
    11. }
    12. }
    13. }
    14.  
  • The Third of the page-related events, is shouldOverrideUrlLoading:
    1.  
    2. @Override
    3. public boolean shouldOverrideUrlLoading(WebView view, String url) {
    4. view.loadUrl(url);
    5. return true;
    6. }
    7.  
  • This is a very important piece of code, we use this to allow our Android Internet Browser to follow newly pressed links and handle them inside our own Browser.
  • For further details, see the code, it is easy and self explanatory:
    Android Internet Browser Webview Sample


      

    More on PocketMagic:

    Atmega8 and enc28J60 for ethernet support | 8830 Views | Rate 27.25
    Atmega8 and enc28J60 for ethernet support
    How to make metallic sodium | 1415 Views | Rate 26.7
    How to make metallic sodium
    Tube: USN-5J29 | 1079 Views | Rate 25.69
    Tube: USN-5J29
    How to set the AVR Fusebits | 2532 Views | Rate 25.32
    How to set the AVR Fusebits
    Tube: GP-5 (ГП-5) | 627 Views | Rate 25.08
    Tube: GP-5 (ГП-5)
    Dual H-Bridge for controlling two motors | 2094 Views | Rate 24.93
    Dual H-Bridge for controlling two motors

    10 Responses to “Android WebBrowser using WebView”

    1. 1
      Santhosh:

      Very helpful article.

      Thanks for the post.

    2. 2
      Android TV Box: Meteorit MMB-322 - Seite 117 - Android-Hilfe.de:

      [...] [...]

    3. 3
      Jenjeng:

      any idea how to get this to play video and or audio?

    4. 4
      Azhar Inamdar:

      Dude,
      I tries using your browser, gr8 work!!!!
      But it does not open up this link https://mobile.twitter.com

      Can u pls point me out some way to open the above link thru webview?
      Any help wud be appreciated.

      Thanks and Regards.

    5. 5
      Jake Hart:

      I tried this code and it works great. However, if I load a page that contains text input boxes, when I tap the input the web view flashes black for a second (actually it is flashing transparent, because if I set the background color for the layout underneath the WebView, that is the color I see. And sometimes it doesn’t even redraw except for the input that has focus.

      Every other WebView code I can find has this same problem — does anyone know how to fix it?
      –Thanks

    6. 6
      Todilo:

      @Azhar Inamdar : To fix that you have to add:

      webview.getSettings().setDomStorageEnabled(true);

    7. 7
      The Pirate:

      hi evryone i want to fetch particular data from wikiipedia can any one help me please????

    8. 8
      saravanakumar:

      super

    9. 9
      Anonymous:

      Nice tutorial ….thanks for sharing ..:)

    10. 10
      Radu Motisan:

      You’re welcome!

    Thank you for commenting. Your comment won't show until approved, which can take some time.

    Please copy the string oSnhnV to the field below: