Android WebView Simple to use demo

Source: Internet
Author: User

Activity configuration file: Activity_main.xml

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical" >

<linearlayout
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:orientation= "Horizontal" >

<edittext
Android:id= "@+id/et_address"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:layout_weight= "0.5"
android:text= "Http://www.baidu.com"/>

<button
Android:id= "@+id/search"
Android:layout_width= "101DP"
android:layout_height= "Match_parent"
android:onclick= "click"
android:text= "Search"/>
</LinearLayout>

<webview
Android:id= "@+id/mywebview"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:layout_weight= "0.08"/>

</LinearLayout>

Mainactivity.java

 PackageCom.example.mybrowser;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.Window;Importandroid.webkit.WebSettings;ImportAndroid.webkit.WebView;Importandroid.webkit.WebViewClient;ImportAndroid.widget.EditText; Public classMainactivityextendsActivity {Private Static FinalString URL = "http://www.baidu.com"; PrivateEditText et_address; PrivateWebView Mywebview; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Requestwindowfeature (Window.feature_no_title);                Setcontentview (R.layout.activity_main); Et_address=(EditText) Findviewbyid (r.id.et_address); Mywebview=(WebView) Findviewbyid (R.id.mywebview); WebSettings mysettings=mywebview.getsettings (); Mysettings.setsupportzoom (true); Mysettings.setbuiltinzoomcontrols (true); Mywebview.setwebviewclient (Newwebviewclient ()); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present. //getmenuinflater (). Inflate (R.menu.main, menu);Menu.addsubmenu (0,0,0, "Refresh"); Menu.addsubmenu (0,0,1, "forward"); Menu.addsubmenu (0,0,2, "back."); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//TODO auto-generated Method Stub                Switch(Item.getorder ()) { Case0: Mywebview.reload ();  Break;  Case1:            if(Mywebview.cangoback ()) {mywebview.goback (); }            Else            {                            }             Break;  Case2:                if(Mywebview.cangoforward ()) {Mywebview.goforward (); }             Break; }        return true; }         Public voidClick (View v) {String URL=et_address. GetText (). toString (). Trim (); if(url = =NULL||Url.isempty ()) {URL=URL;    } mywebview.loadurl (URL); }}

Remember to include access to the network in the manifest configuration file:

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

As follows:

Android WebView Simple to use demo

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.