Android WebViews
If you have followed through a couple of the tutorials already, or have already made a couple of applications on your own, then you have no doubt used several of the built in View types available from Android. One view you may not have known about is the Android WebView.
AndroidDom loaded in a webview |
Creating the Layout
For some unknown reason, the WebView does not show up on the list of available views from the Android layout editor, so to add the WebView we must edit the layout XML directly.
The file below represents a simple layout that features a WebView that takes up the entire space.
Main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="fill_parent"> <WebView android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
Adding the Internet Permission
In order for the WebView to download webpages from the internet, you will need to enable the internet permission for your application. To do this, edit the manifest file for the app.
Add the following line after the closing application tag, but before the closing manifest tag.
<uses-permission android:name="android.permission.INTERNET" />
Loading a Website
Now that we have a webview and enabled the app to access the internet, we can download a webpage. The WebView is given the id "webview" so we can use that to get access to the view, and load a webpage from the activity.
Add the following import to the top of your java file.
import android.webkit.WebView;
Use the following code to load a web page
WebView webview = (WebView) findViewById(R.id.webview); webview.loadUrl("http://www.androiddom.com");
If you haven't properly added the Internet permission to your application, or you have entered a bad url you will get a message similar to the following.
WebView displaying an error |
If the web page loads successfully, you should see actual content, like what is listed below.
Screenshot of AndroidDom loaded in the webview |
Conclusion
Web Views are a great way to integrate more content into your Android apps and very easy to use and work with.
Full Source
The full source of MainActivity.java is listed below. The full source for the layout is listed under the "Creating a Layout" Step earlier in the tutorial
package com.androiddom.webview; import android.app.Activity; import android.os.Bundle; 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 webview = (WebView) findViewById(R.id.webview); webview.loadUrl("http://www.androiddom.com"); }
I have tried this code, but it is displaying the same error on each url that "web page is not available", even though i have added the internet access permission in the manifest.
ReplyDeletekindly tell me the solution. really need it!
I have tried this code but it is not working in my android app. Only my android layout display but in webview i am not able to load/open url. I have added internet permission in manifest file.
ReplyDeleteSo kindly tell me the solution, it is needed.
Thanks in advance,
sumit.tyagi@kmgin.com
Definately u guys have no internet connection...........
DeleteThis comment has been removed by the author.
ReplyDeletethank you,i tried it's working,but it does not occupy complete activity.
ReplyDeletelil bit space is remaining from all sides
remove the padding or set them to 0
DeleteThis comment has been removed by the author.
ReplyDeletehow do i get it from not opening up the browser when tapping links?
ReplyDeletehello friends take a look of this link for more good examples.
ReplyDeletehttp://androidexample.com/Show_Loader_To_Open_Url_In_WebView__-_Android_Example/index.php?view=article_discription&aid=125&aaid=145
How do I display part of webpage in an activity
ReplyDeleteThanks.. its working for me.It displaying the home but its opening the links in web browser. Tellme how to open the links with in the app itself.
ReplyDelete