How to set WebView in android?
HelloWorld example of WebView
Read more: WebView on Android developers
**main.xml (res/layout)**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/urlEdit"
android:layout_width="300dip"
android:layout_height="wrap_content"
android:inputType="textWebEditText"
android:imeOptions="actionDone" />
<Button
android:id="@+id/goButton"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_toRightOf="@id/urlEdit"
android:background="@drawable/redbutton"
android:text="Go..."
android:textColor="#fcfcfc" />
</RelativeLayout>
<WebView
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
WebView Activtiy
public class WebViewExampleActivity extends Activity {
public WebView webView;
public Button goButton;
public EditText urlEdit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webView = (WebView)findViewById(R.id.webView);
goButton = (Button)findViewById(R.id.goButton);
urlEdit = (EditText)findViewById(R.id.urlEdit);
webView.setWebViewClient(new DemoWebViewClient());
goButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
openUrl();
}
});
}
public void openUrl() {
String url;
url = urlEdit.getText().toString();
webView.loadUrl(url);
}
public class DemoWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
Here they are
xml
<WebView
android:id="@+id/web_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0" />
class
WebView webview = (WebView) findViewById(R.id.web_view);
webview.getSettings().setSupportZoom(true);
webview.getSettings().setBuiltInZoomControls(true);
webview.loadUrl("file:///android_asset/yourHtmlFile.html");
精彩评论