How Android embeds a browser in view

Source: Internet
Author: User

One of the things I'm going to do is to embed a browser in the middle of a page, and at first I don't know where to start because it used to be textveiw or editveiw, and they can't be used to display the contents of a Web page.

The first thought is: Is there a view to display the page, so I am in Eclipse XML Editor input <web, haha, sure enough, there is a hint is webveiw, then can you use it? So to search the Internet for its information, sure enough, the following will begin.

First build the WebView in XML, and use Findviewbyid () in the code to take it out. My Code is:

WebView WebView = (WebView) This.findviewbyid ("@/id/webview");

Immediately after the use of the content from the Internet, look at it slowly.

How to create WebView:

1, add permissions: Androidmanifest.xml must use the license "Android.permission.INTERNET", otherwise the Web page is not available error.
2. Generate a WebView component in the activity: WebView WebView = new WebView (this);
3, set WebView basic information:
If you have JavaScript on the page you are visiting, WebView must set up support JavaScript.
Webview.getsettings (). Setjavascriptenabled (True);
Touch Focus Works
Requestfocus ();//If not set, you cannot eject the soft keyboard and do not respond to other events when you click on the text input box of the Web page.
Cancel scroll bar
This.setscrollbarstyle (Scrollbars_outside_overlay);
4. Set Wevview to display the page:
Internet use: Webview.loadurl ("http://www.google.com");
For local files: Webview.loadurl ("file:///android_asset/XX.html"); Local files are stored in the: Assets file
5. If you want to click on the link handled by yourself, instead of the new Android system browser should link.
Add an Event Listener object (webviewclient) to WebView

And rewrite some of these methods
Shouldoverrideurlloading: The response to a hyperlink button in a Web page.
Webviewclient calls this method when a connection is pressed and passes a parameter: The URL that is pressed
Onloadresource
Onpagestart
Onpagefinish
Onreceiveerror
Onreceivedhttpauthrequest

6, if the WebView point link to see a lot of pages later, if you do not do any processing, click the System "back" button, the entire browser will call finish () and end itself, if you want to browse the page fallback instead of exiting the browser, The back event needs to be processed and consumed in the current activity.
Overrides the onkeydown (int keycoder,keyevent event) method of the Activity class.
public boolean onKeyDown (int keycoder,keyevent event) {
if (Webview.cangoback () && Keycoder = = keyevent.keycode_back) {
Webview.goback (); GoBack () indicates that the previous page of the WebView is returned


return true;
}
return false;
}

WebView Related Issues Note:

Android WebView is very powerful, is actually a browser, you can embed it in the location you want, I have two questions here, is how to know the page loading progress and load the page,
Click on the link in the page or in the current WebView jump, do not want to jump to the browser side, the solution is as follows:

This method can handle webview at load time and when loading is complete
Webview.setwebchromeclient (New Webchromeclient () {
@Override
public void onprogresschanged (WebView view, int newprogress) {
if (newprogress==100) {//Here is the title of the activity, or you can do some other things according to your own needs.
Title.settext ("Load Complete");
}else{
Title.settext ("Loading in ...");

}
}
});


Webview.setwebviewclient (New Webviewclient () {
@Override
public boolean shouldoverrideurlloading (WebView view, String URL) {//Override this method to indicate that clicking a link in a webpage or jumping in the current WebView does not jump to the browser
View.loadurl (URL);
return true;
}

@Override
public void Onreceivedsslerror (WebView view, Sslerrorhandler handler, android.net.http.SslError error) {// Override this method to have webview handle HTTPS requests
Handler.proceed ();
}

});

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.