Wednesday 11 March 2015

Android WebView Example

Webview 


The WebView enables you to embed a web browser in your activity. This is very useful if your appli- cation needs to embed some web content, such as maps from some other providers, and so on. The following Try It Out shows how you can programmatically load the content of a web page and dis- play it in your activity.

Using the WebView View


1 . Using Eclipse, create a new Android project and name it as WebView.

2 . Add the following statements to the main.xml file:

<?xml​ version=”1.0” ​encoding=”utf-8”?>
<LinearLayout ​xmlns:android=”http://schemas.android.com/apk/res/android” ​​​​android:orientation=”vertical” ​​​​
android:layout_width=”fill_parent” ​
​​​android:layout_height=”fill_parent” ​​​​>

<WebView
android:id=”@+id/webview1” ​​
​​android:layout_width=”wrap_content” ​
​​​android:layout_height=”wrap_content” />

</LinearLayout>

3 . In the MainActivity.java file, add the following statements in bold:

package​ com.emergingandroidtech.WebView;
import​ android.app.Activity;
import ​android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;

public ​class ​MainActivity​ extends​ Activity
​{
​​​​
/**​Called​ when​ the ​activity ​is ​first ​created.​*/

​@Override ​​
​​public ​void ​onCreate(Bundle​savedInstanceState)​
{
​​​​​​​​super.onCreate(savedInstanceState);
​setContentView(R.layout.main);
​​​​​​​​
WebView wv = (WebView) findViewById(R.id.webview1);
​​​​​​​​WebSettings webSettings = wv.getSettings(); ​​
​​​​​​webSettings.setBuiltInZoomControls(true);
​​​​​​​​wv.loadUrl( ​​​​​​​​“www.google.com”);
​​​​}
}

No comments:

Post a Comment