Search code examples
androidadsglsurfaceview

Have multiple content views simultaneously?


Alright, so the concept seems simple enough, but Have searched all over the web and have yet to find a solution. So I am making my first android game, woot. But I want to monetize it with ads. This is where the problem comes in. As I am Not using a layout for my game, but instead a GLSurfaceView, I cannot find anything about setting an ad over that. Every ad tutorial by google uses either a layout, or requires you to create a layout through java. The problem is, that those all still use a SurfaceView, not a GLSurfaceView. How can I implement an ad over the GLSurfaceView? I mean, most games, logically would be made using openGL, not canvas, so there would have to be a way, right? Any suggestions will help. If you guys need to see some of the code, let me know, but it's pretty standard code from the glSurfaceView Tutorials.


Solution

  • GLSurfaceView is just a SurfaceView with some code wrapped around it to manage threads and the EGL context. They're not very different.

    SurfaceView has two parts, the "surface" and the "view". The view is part of the View hierarchy, and is generally just a transparent window used for the layout. All of the View-based UI is drawn on a single layer. GLES rendering is done on the surface part, which is a separate layer composited by the system. By default, the surface layer sits behind the view layer, but you can configure that when the SurfaceView is created.

    Since the Surface is behind the View, you can draw your ads with Views on top of your game without interfering with it.

    You can find some examples in Grafika. For example, the "record GL app" activity has UI elements (some text and radio buttons) on top of GLES animation. It uses a relative layout and positions the UI elements on top of the SurfaceView.