Key art image for WebView in the Developer Preview of L

WebView in the Developer Preview of L

The WebView in the Android Developer Preview of L has once again received an update, this time to Chromium M36 and brings with it some new web platform features.

If you haven’t been following the WebView updates since the KitKat release everything you need to know is right here in this video.

Text Sizing in KK MR2 and Above

In the KitKat MR2 release, where the WebView was updated to M33, one small but important change was made to the way the fonts are sized inside the WebView. In KK MR2 and above the font size takes into account the platforms text sizing.

Looking at an example, the normal font would look something along the lines of:

Gauntface.co.uk with normal font inside a WebView

However, if you go to Settings > Display > Font size and select Large or Huge, you’ll get the following:

Gauntface.co.uk with large font inside a WebView

A few people have asked how to switch this behaviour off as it’s playing havoc with their layouts and the solution is to use setTextZoom() with a value of 100.

webview.getSettings().setTextZoom(100);

Please bare in mind that these users have opted in to having larger text on their device, chances are they’ll want it for browsing your WebView’s content as well….just saying.

New Permissions Model in the WebView

In the Developer Preview of L the WebView team enabled WebRTC, WebAudio and WebGL.

Any scenario where you’d see a permission dialog in a browser (i.e. access to camera or microphone) will require the use of some new WebChromeClient API’s to handle permission requests in the WebView.

Warning: These API’s are only available in the L developer preview release and may very well change in the final version of L.

Let’s look at some rough examples and use cases of these new API’s.

If you have an application where you want to authorize certain permissions for specific domains, then you can use the preauthorizePermission method. At the moment this doesn’t work in the preview of L, but it’ll be useful for this common use case.

webview.preauthorizePermission(Uri.parse("http://mywebsite.com/"), PermissionRequest.RESOURCE_AUDIO_CAPTURE | PermissionRequest.RESOURCE_VIDEO_CAPTURE);

If you’re implementing a browser type application, then you’ll want to look at implementing the onPermissionRequest callback, which is called whenever a permission would normally show up in Chrome.

The way you implement this is like so:

webview.setWebChromeClient(new WebChromeClient() {
  @Override
  public void onPermissionRequest(PermissionRequest request) {
    // Show a grant or deny dialog to the user

    // On accept or deny call
    // request.grant(request.getResources())
    // or
    // request.deny()
  }
});

The request.grant() method takes a set of permissions you wish to grant. This caters to scenarios where you may want to allow some permissions requested but not all.

The final thing you need to consider for these permissions is including the appropriate Android permissions. The camera permission from the WebView requires the android.permission.CAMERA permission, for accessing audio from the microphone we need android.permission.RECORD_AUDIO and finally for hooking up the volume controls we need android.permission.MODIFY_AUDIO_SETTINGS (Useful in WebRTC).

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

You can find a working WebRTC sample on the Chromium WebView Samples Github Repo which uses the permission model.

Sample of WebRTC Running in the Android L Preview WebView

Found an issue?

All my posts are available to edit on GitHub, any fix is greatly appreciated!

Edit on GitHub