If you want to deliver a web application (or just a web page) as a part of a client application, you can do it using
WebView. The WebView class is an extension of Android's View class that allows you to display web pages as a part of your activity layout. It doesn't include any features of a fully developed web browser, such as navigation controls or an address bar. All that WebViewdoes, by default, is show a web page.The web page can be loaded from same application or URL. It is used to display online content in android activity.The loadUrl() and loadData() methods of Android WebView class are used to load and display web page. Adding a WebView
To add a
WebView to your Application, simply include the <WebView> element in your activity layout. For example, here's a layout file in which the WebView fills the screen:?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
To load a web page in the
Before this will work, however, your application must have access to the Internet. To get Internet access, request theWebView, use loadUrl().WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.techizhub.com");
INTERNET permission in your manifest file. For example:<manifest ... >
<uses-permission android:name="android.permission.INTERNET" />
...</manifest>
That's all you need for a basic
WebView that displays a web page. Ok let us try itactivity_main
<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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</RelativeLayout>MainActivity.java
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.techizhub.com");
}
}Now we want to add INTERNET permission in manifest
Androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cooltechiz.blogspot.com.helloworld" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity2"
android:label="@string/title_activity_main_activity2" >
</activity>
</application>
</manifest>
Now its the time to hit RUN button on your IDE
OUTPUT
we loaded http://www.techizhub.com using WebView in android application successfully.

No comments:
Post a Comment