Android SDK: Embed a WebView with the WebKit Engine

http://code.tutsplus.com/tutorials/android-sdk-embed-a-webview-with-the-webkit-engine--mobile-1459


This quick tip will demonstrate how you can embed an Android WebView in your application with the the WebKit engine. You will learn how to both load an external URL and render custom markup that you supply in-app.

Start off by creating a new Android SDK project in your IDE of choice. You can name the main activity, package root and project anything you want. Choose Android 1.6 as your platform to maintain broad compatibility.

Android WebView: Create New WebView Project With WebKit

The WebView widget works just like any other Android SDK widget: you declare it in a layout file and can then access it from an Activity and tell it what to do.

Open your main.xml layout file or create it if your IDE didn’t already do so. Next, declare the WebView widget as a child of the LinearLayout element, making sure to specify an ID so you can access the widget later. As always, don’t forget to declare the width and height or else your app will throw an exception. For demonstration purposes, we’re going to have the component fill up the whole screen by setting the android:layout_width and android:layout_height attributes to fill_parent.

Since we’re going to load a URL in the WebView widget, we have to make sure to request permission to access the Internet in AndroidManifest.xml

Open it up and declare a uses-permission element where you specify android.permission.INTERNET as the android:name attribute, like so (line 6):

If you start your application in the Android emulator now, you will see a blank, white screen. Why? Well, we haven’t told WebView to render anything yet – that’s what we’ll do next.

Open up your main activity (com.webkitdemo.WebKitDemo in our case) and add the following code into the onCreate() method:

WebView engine = (WebView) findViewById(R.id.web_engine);
engine.loadUrl("http://mobile.tutsplus.com");

This snippet of code accesses the WebView widget via its resource ID and invokes loadUrl(). Start the app in the emulator and view the result: it should display the site you’re currently on!

Android Webview: Webkit Rendering

Next, we’re going to replace the loadUrl() call with loadData(), which takes three arguments:

  1. String htmlData
  2. String mimeType
  3. String encoding

Comment out the engine.loadUrl() line and insert the following code underneath it:

String data = "<html>"  +
              "<body><h1>Yay, Mobiletuts+!</h1></body>"  +
              "</html>"; 
engine.loadData(data, "text/html", "UTF-8");

Compile and restart the application in the emulator and you should see this:

Android WebView: Load Custom HTML/CSS

Technically, you can pass another type of data with its respective mime-type to this method, but most of the time you’ll be using text/html with a UTF-8 encoding. Don’t forget that you’re using WebKit, a powerful web engine; you can use CSS3, JavaScript etc. when you design your manuals or anything you want it to display.

NOTE: One of WebView’s peculiarities is the fact that JavaScript is disabled by default. The reason for this is that in most cases when you’re embedding a WebView into your application, you’re using it for a help manual or something that doesn’t require JavaScript. Nevertheless, it’s easy to enable it if you do need it:

engine.getSettings().setJavaScriptEnabled(true);

You may have noticed that the widget doesn’t have a toolbar. This is intended, as you would normally launch the native web browser app if you want navigation capabilities. There is, however, a way to control it programmatically. Try invoking one of the following methods on the instance of your widget:

  • reload(): Refreshes and re-renders the page.
  • goForward(): Goes one step forward in browser history.
  • goBack(): Goes one step back in browser history.

The API has a lot more to offer, but that goes beyond the scope of this Quick Tip.

You should now be familiar with the basic usage of the very useful Android WebView widget. You can use it to display an “about” web page for your application or a user manual – all sorts of stuff. Technically, you can even write your application using JavaScript, CSS and HTML and display it using the widget, although that wouldn’t be too practical... Or would it?


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值