Android Multiple SurfaceViews

You can have multiple SurfaceViewsin one layout. The “Multi-surface test” activity in Grafika has three.

The first post cited in @nonsleepr’s answer was followed up 9 months later with this post by the same author, which mentioned the existence of SurfaceView#setZOrderMediaOverlay().

The key thing to understand is that SurfaceView is not a regular view. When your app comes to the foreground it gets a surface to draw on. Everything in your app’s UI is rendered onto the app’s surface by the app, and then that surface is composited with other surfaces (like the status bar and navigation bar) by the system compositor. When you create a SurfaceView, it’s actually creating an entirely new surface that is composited by the system, not by your app.

You can control the Z-ordering (i.e. “depth”) of the SurfaceView surface very loosely. There are four positions, from top to bottom:

  • SurfaceView + ZOrderOnTop
  • (app UI goes here)
  • SurfaceView + ZOrderMediaOverlay
  • SurfaceView (default)

If you have two SurfaceViews at the same depth, and they overlap, the results are undefined — one will “win”, but you can’t control which.

The system compositor on modern devices is very efficient when you have N surfaces. At N+1 surfaces you hit a performance cliff. So while you can have three SurfaceViews, you’re generally better off keeping the number down. The value of N varies from device to device.

Update: if you really want to understand how SurfaceView works, see the Android System-Level Graphics doc.

Leave a Comment