Question

i want to add admob advertising to my game app. however the game drawing is all on in code with no layouts, just the Surfaceview the game draws on.

I cannot use the layout.addView(adView); the docs give as an example since my game uses no layout, in fact the layout folder is deleted.

in the main activity I define a game panel:

gamepanel=new MainGamePanel(this);

the gamepanel is instantiated:

class MainGamePanel extends SurfaceView implements SurfaceHolder.Callback

and then I just draw right to the surfaceview using Canvas draw methods.

so how do I add the adView object I created in the activity to the surfaceview:

adView = new AdView(this, AdSize.BANNER, "MY_PUU_NUMBER");

gamepanel.addView(adView) does not work. maybe i'll be forced to use a layout from the start just for this, but that will complicate things.

Was it helpful?

Solution

The best workaround I've found for adding admob advertising to a surfaceView application is to create a layout programmatically and draw both views to it as follows:

LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);

ll.addView(adView); // The ad banner
ll.addView(gamepanel); // The SurfaceView object

It's not actually drawing the adView to the surfaceView, but it's the best way I've found to include it.

Note that a banner ad will slow your framerate so be judicious. Unfortunately nowadays it's hard to get android game players to shell out even .99 upfront for a game if your company's name isn't Square Enix, so ads can be a necessary evil.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top