How to create an Android webview
n this tutorial we will see how to use Android WebView in your
Android Application. Instead of loading the webpage directly in your
Android browser you can embed WebView to load the URL inside your
Android Application.
Initial Setup:
For Initial setup of Android SDK and AVD refer this tutorial Setup Android SDK /IDE /AVD in Windows and Ubuntu.Creating the project:
Open Eclipse and select File -> New -> Android Application Project. Then enter the Application Name, Project Name and Package Name. Here I have given the project name as learn2crack.webview. Then Give the Main Activity name and Main Layout Name. Here my Activity name is L2CWebView and layout name is webview.Layout – webview.xml
The layout has a WebView to load the external webpage in the Application. The Id for WebView is defined as “webview”.webview.xml
< 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:paddingBottom = "@dimen/activity_vertical_margin" android:paddingLeft = "@dimen/activity_horizontal_margin" android:paddingRight = "@dimen/activity_horizontal_margin" android:paddingTop = "@dimen/activity_vertical_margin" tools:context = ".WebView" > < TextView android:id = "@+id/textview" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "Webview Loading Learn2Crack Website" /> < WebView android:id = "@+id/webview" android:layout_width = "match_parent" android:layout_height = "match_parent" android:layout_below = "@+id/textView1" android:layout_centerHorizontal = "true" android:layout_marginTop = "75dp" /> </ RelativeLayout > |
Activity – L2CWebView.java
Initially we are defning the WebView and linking with the layout with the Id R.id.webview. The learn2crack.loadurl() function loads the external webpage. The learn2crack.getSettings().setJavaScriptEnabled(true); is defined to enable JavaScript Support in your Application.L2CWebView.java
package learn2crack.webview; import android.os.Bundle; import android.webkit.WebView; import android.app.Activity; public class L2CWebView extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.webview); final WebView learn2crack = (WebView)findViewById(R.id.webview); learn2crack.loadUrl( "https://128.199.224.11" ); learn2crack.getSettings().setJavaScriptEnabled( true ); } } |
Manifest – AndroidManifest.xml
This defines the Activities and Permissions used by the Android Application. Since we load the webpage from internet we need to set permission to use Internet connection.<uses-permission android:name=”android.permission.INTERNET”/>
AndroidManifest.xml
<? xml version = "1.0" encoding = "utf-8" ?> < manifest xmlns:android = "http://schemas.android.com/apk/res/android" package = "learn2crack.webview" android:versionCode = "1" android:versionName = "1.0" > < uses-sdk android:minSdkVersion = "8" android:targetSdkVersion = "17" /> < application android:allowBackup = "true" android:icon = "@drawable/ic_launcher" android:label = "@string/app_name" android:theme = "@style/AppTheme" > < activity android:name = "learn2crack.webview.L2CWebView" android:label = "@string/app_name" > < intent-filter > < action android:name = "android.intent.action.MAIN" /> < category android:name = "android.intent.category.LAUNCHER" /> </ intent-filter > </ activity > </ application > < uses-permission android:name = "android.permission.INTERNET" /> </ manifest > |
Execution
Finally run the project in Android Emulator.
Enjoy
How to create an Android webview
Reviewed by Unknown
on
07:28:00
Rating:
No comments: