Reputation:
I needed to build an Android app that lets you view a web page inside my app. I needed this not to be in a browser but my app. I found the answer and some options for when page is loaded. I thought I'd try sharing the info I found here, after I tested of course.....
Upvotes: 2
Views: 5979
Reputation:
First need to add INTERNET permission to your manifest.
<uses-permission android:name="android.permission.INTERNET" />
Then, use the WebView class to display a web page. First, create a layout that contains a webview:
<?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">
<WebView android:id="@+id/myWebView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
In your Activity, (probably the onCreate), initialize a WebView object by using the layout you created. An example is below. private WebView webview;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.somelayout);
String url = "http://bigdaddyapp.com";
webview = (WebView) findViewById(R.id.myWebView);
//next line explained below
webview.setWebViewClient(new MyWebViewClient(this));
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl(url);
}
If you'd like specific options, like catching pages as they load, you need an inner WebViewClient class. For example, you can use the onPageStarted(...) method to do something whenever a new page is loaded in your webview:
public class MyWebViewClient extends WebViewClient {
public MyWebViewClient() {
super();
//start anything you need to
}
public void onPageStarted(WebView view, String url, Bitmap favicon) {
//Do something to the urls, views, etc.
}
}
Upvotes: 8