Android WebView for use with simple browser

Source: Internet
Author: User

In order to browse the Web page, the first we can use the intent to bring the browser, the second can be webview this control to browse.

The first is very simple, look directly at the code:

Uri uri = uri.parse ("http://www.baidu.com/"), Intent launchbrowser = new Intent (Intent.action_view, URI); StartActivity ( Launchbrowser);

The second is a little bit more complicated.

First, we have an XML layout for the WebView control:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "    android:layout_height=" Match_parent    " Tools:context= ". Mainactivity ">    <webview        android:id=" @+id/webview "        android:layout_width=" Match_parent        " android:layout_height= "Match_parent"/></relativelayout>

Load the control in the OnCreate method and set some parameters:

WebView = (WebView) Findviewbyid (R.id.webview); Load Webviewwebview.getsettings (). Setjavascriptenabled (True); Set whether to support Javascriptwebview.loadurl ("http://www.baidu.com/");  Load Address webview.setwebviewclient (new Webviewclient () {      @Override public      boolean shouldoverrideurlloading ( WebView view, String URL) {            view.loadurl (URL);     return true;      }}); Set up Browse

If you want to use WebView to access the network, you also need a permission:

<uses-permission android:name= "Android.permission.INTERNET"/>

In this way, a simple small program to access the web is done.

Of course, the simple is not satisfied, we also need to add a bit of functionality ...

First we have to deal with the fallback key:

@Overridepublic boolean onKeyDown (int keycode, keyevent event) {    if ((keycode = = Keyevent.keycode_back)) {        if ( Webview.cangoback ())           webview.goback ();        else           finish ();        return true;    }    return false;}

The purpose of this is to return to the previous page after each press of the fallback key, if not, it is equivalent to fallback an activity, this itself is an activity, press back after the equivalent of the activity to destroy it, so, to add the above code.

We know that there is a place to enter the address above the browser, and we'll add it to the XML layout first:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Tools:context =". Mainactivity "> <edittext android:id=" @+id/et_address "android:layout_width=" Wrap_content "an         droid:layout_height= "Wrap_content" android:layout_above= "@+id/webview" android:layout_toleftof= "@+id/btn_go" Android:layout_alignparentleft= "true" android:layout_alignparentstart= "true"/> <button and Roid:id= "@+id/btn_go" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Androi        d:text= "GO" android:layout_alignparenttop= "true" android:layout_alignparentright= "true"/> <webview        Android:id= "@+id/webview" android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android:layout_alignparentbottom= "true"       android:layout_below= "@+id/btn_go"/></relativelayout> 

With a EditText and button control, these two controls are handled as follows:

Et_address = (EditText) Findviewbyid (r.id.et_address); Et_address.settext ("http://www.baidu.com/"); btn_go = (Button) Findviewbyid (r.id.btn_go);    Btn_go.setonclicklistener (New View.onclicklistener () {    @Override public    void OnClick (View v) {        String Address = Et_address.gettext (). toString ();        if (!address.startswith ("http://")) {            address = "//" + address;        }//If you do not start with//, you cannot identify, so Judge        Webview.loadurl (address);        Et_address.settext (address);    }});

OK, so far, a normal browser is done, of course webview This control has a lot of properties that can be set, I do not introduce each.


The end~


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android WebView for use with simple browser

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.