1

I'm trying to set webview as a live wallpaper, but I have a problem with sizing it.

In the Engine I create a webview with WallpaperService's Context:

public WallpaperEngine(Context context) {
    webView = new WebView(context);
    ...
}

And I draw it to wallpaper's canvas:

SurfaceHolder holder = getSurfaceHolder();
Canvas canvas = null;
try {
    canvas = holder.lockCanvas();
    if (canvas != null) {
        webView.draw(canvas);
    }
} finally {
    if (canvas != null)
        holder.unlockCanvasAndPost(canvas);
}

But the wallpaper will be white and javascript reports, that window's size is 0px.

How to set size of the WebView?

1 Answers1

1

Solved it by using WindowManager, defining size in LayoutParams and changing WebView visibility to GONE.

webView = new WebView(context);
webView.setVisibility(webView.GONE);
webView.loadUrl("http://example.com/");

wm = (WindowManager)context.getSystemService(WINDOW_SERVICE);
params = new WindowManager.LayoutParams(
    WindowManager.LayoutParams.WRAP_CONTENT,
    WindowManager.LayoutParams.WRAP_CONTENT,
    WindowManager.LayoutParams.TYPE_PHONE,
    WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
    PixelFormat.TRANSLUCENT
 );
params.width = //width
params.height = //height

wm.addView(webView, params);
  • Hello! I just found this page because I'm trying to put a WebView in a WallpaperService. I'm new to Android development, so I'm having trouble figuring out what to do. I've used your example here and I'm getting `android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@e1a9690 -- permission denied for window type 2002` when I preview the wallpaper. I know it's been awhile since you posted this but any suggestions would be most welcome! – iangilman Jan 15 '19 at 23:54
  • Android has been changed after I posted the question and I don't remember what the version was. The error you got seems to be permission related so maybe searching for permissions of the method you are using could help. – Jesse Sivonen Jan 19 '19 at 20:29
  • Yes! Looks like TYPE_PHONE needs to become TYPE_WALLPAPER. That gets me past that error. Now the new error is `android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?`. Since the WallpaperService is a service, not an activity, do I even have an activity to add the window to? Is it possible to add a window to a service? If not, can I add an activity to the service so I can add the window to that? BTW, I've posted the general question here: https://stackoverflow.com/questions/54263346/android-use-webview-for-wallpaperservice – iangilman Jan 23 '19 at 00:27
  • Or is there another way to set the size of the WebView without having to call addView? I've tried calling setLayoutParams on the WebView, but that doesn't seem to have any effect. – iangilman Jan 24 '19 at 21:59
  • Okay, I found the solution to sizing the WebView: https://stackoverflow.com/a/54540300/306351 Thanks again for getting me started on the right track! – iangilman Feb 05 '19 at 17:55