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:
m_web.getSettings().setJavaScriptEnabled(true);
m_web.setWebViewClient(new MyWebViewClient());
Note: The WebView Client class is quite simple:
private class MyWebViewClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Log.d(LOG_TAG, "onPageStarted");
m_tv.setText("Loading page...");
//stop button is enabled only when pages are loading
m_bButStop.setEnabled(true);
}
@Override
public void onPageFinished(WebView view, String url) {
Log.d(LOG_TAG, "onPageFinished");
m_tv.setText("Ready ");
//stop button is disabled when pages are already loaded
m_bButStop.setEnabled(false);
// This call inject JavaScript into the page which just finished loading.
m_web.loadUrl("javascript:window.HTMLOUT.showHTML(''+document.getElementsByTagName('html')[0].innerHTML+'');");
// adjust prev/next buttons, only if history is available
m_bButBack.setEnabled(m_web.canGoBack());
m_bButFwd.setEnabled(m_web.canGoForward());
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
Details:
To run the Javascript we need to call :
// set java script used to get the HTML code
m_web.addJavascriptInterface(new JavaScriptInterface(), "HTMLOUT");
// Used with Webview, to get the HTML code
class JavaScriptInterface{
public void showHTML(String html) {
m_nHTMLSize = 0;
if (html !=null) {
//int i = html.lastIndexOf(""); //search for something in the text
m_nHTMLSize = html.length();
Log.d(LOG_TAG, "HTML content is: "+html+"\nSize:"+m_nHTMLSize+" bytes");
}
}
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
For further details, see the code, it is easy and self explanatory:
Android Internet Browser Webview Sample
Very helpful article.
Thanks for the post.
any idea how to get this to play video and or audio?
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.
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
@Azhar Inamdar : To fix that you have to add:
webview.getSettings().setDomStorageEnabled(true);
hi evryone i want to fetch particular data from wikiipedia can any one help me please????
super
Nice tutorial ….thanks for sharing ..:)
You’re welcome!
Please don’t call loadUrl from shouldOverrideUrlLoading. shouldOverrideUrlLoading is called for subframes with non-https schemes. If you go to a a page like http://jsbin.com/gupug/1/quiet your code will end up calling view.loadUrl(‘tel:1234’) and you will end up showing an error page, since the webview doesn’t know how to load a tel: URL.
The right thing to do is to return false from shouldOverrideUrlLoading.