Use WebView, webchromeclient and webviewclient to load Web pages in Android

Source: Internet
Author: User

In Android apps, sometimes it's better to load a Web page with a progress bar, and Android provides good support.

Which webview a series of uses, such as

Webview.getsettings (). Setjavascriptenabled (True); Settings can use JavaScript
Webview.setscrollbarstyle (Webview.scrollbars_outside_overlay); Set scroll bar style
Webview.sethorizontalscrollbarenabled (FALSE);
Webview.getsettings (). Setsupportzoom (True);
Webview.getsettings (). Setbuiltinzoomcontrols (True); Settings can support zooming
Webview.setinitialscale (25); is 25%, the minimum zoom level, where you can set the scale based on your needs.
Webview.sethorizontalscrollbaroverlay (TRUE);
Here is an example of the program, the first post:

Main program:

public class Webpageloader extends Activity

{

Final activity activity = this;

@Override

public void OnCreate (Bundle savedinstancestate)

{

Super.oncreate (savedinstancestate);

This.getwindow (). Requestfeature (window.feature_progress);

Setcontentview (R.layout.main);

WebView WebView = (WebView) Findviewbyid (R.id.webview);


Webview.getsettings (). Setjavascriptenabled (True);

Webview.getsettings (). Setsupportzoom (True);
Click the link in the current browser response, not the new Android system browser should link in the ring

Webview.setwebchromeclient (New Webchromeclient ()

{

public void onprogresschanged (WebView view, int progress)

{

Activity.settitle ("Loading ...");

Activity.setprogress (Progress * 100);

if (progress = = 100) {

Activity.settitle (R.string.app_name);

}

}

);

Webview.setwebviewclient (New Webviewclient () {



public void Onreceivederror (WebView view, int errorCode, string description, String failingurl)

{//Handle the error



}



public boolean shouldoverrideurlloading (WebView view, String URL)

{

View.loadurl (URL);

return true;

}

});

Webview.loadurl ("http://www.sohu.com");

}

}


The use of the progress bar is to write the Onprogresschanged event in the inner class after the new setwebchromeclient.


In the design of webview, not everything should be WebView class dry, some chores are assigned to other people, so webview concentrate on doing their own analysis, rendering work on the line. Webviewclient is to help WebView handle various notifications, request events, specifically including:

Webchromeclient is an auxiliary webview processing JavaScript dialog box, website icon, website title, loading progress etc.


Determine if the URL of the page being loaded is correct:
if (Urlutil.isnetworkurl (URL) ==true)

Use WebView, webchromeclient and webviewclient to load Web pages in Android

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.