how do i draw an adView to a SurfaceView canvas - android

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.

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.

Related

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.
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.
According to this manual, you can test the code blow:
TextView textView = new TextView(this);
textView.setText("overlay!");
addContentView(textView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
Obviously, the code is in the Activity.
So I did much researching and finally found a solution that works, thanks to fadden for getting me on the right track btw :)
mGLView = new MyGLSurfaceView(this);
LinearLayout screenLayout = new LinearLayout(this);
screenLayout.setOrientation(LinearLayout.VERTICAL);
// Create a banner ad. The ad size and ad unit ID must be set before calling loadAd.
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId("/6499/example/banner");
AdRequest.Builder adRequest= new AdRequest.Builder();
adView.loadAd(adRequest.build());
screenLayout.addView(adView);
screenLayout.addView(mGLView);
setContentView(screenLayout);

DecorView not drawing over SurfaceView

I have an application that uses SurfaceView do draw content. I wanted to add nice ShowcaseView library to my application which uses getWindow().getDecorView()).addView(...) to draw itself on top of other views and decorations. But in my case it does not draw over my SurfaceView (if I remove it from layout, everything works perfect).
Is it a limitation of Android architecture, or I can tune something in my SurfaceView or fix something in ShowcaseView library?
Update
If I call mySurfaceView.setVisibility(View.INVISIBLE) just before creating ShowcaseView everything works perfect too (except that my main view area is black).
Update 2
I've read about Z-indexes of SurfaceView in documentation. I do not touch them in my code.
SOLUTION
Add setWillNotDraw(false) after calling the super constructor of your SurfaceView.
Other hints regarding surfaceview
It might be helpful to
add the SurfaceView to a Container -e.g. RelativeLayout
call getWindow().getDecorView()).requestTransparentRegion(mySurfaceView); after making your view visible

OpenGL overlay on camera preview - need to set content views in reverse order?

I am trying to show a dynamic OpenGL over the Android camera preview. I have used the typical framework suggested in other guides/sites to create an object which implements the GLSurfaceView.Renderer interface and use setContentView() and addContentView() after. The problem is that the onDrawImage is occurring behind the surface holder which displays the camera preview.
This is part of a larger application and I have separately tested all components in a side application. The only difference between the two is that the larger application has several activities preceding this one, while the side application uses just this activity as the main one.
Update
The answer is to switch the order of the setContentView and addContentView. If it is the main application, use the order in this post: opengl overlay on camera view; however, if you are coming from another activity, run:
setContentView(theCameraCallbackView)
addContentView(theOpenGLView, new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT ))
If anyone can provide some reasoning behind this, it would be greatly appreciated! I spend about eight hours on this two-line swap so it would be great to know what the cause of this was (and hopefully prevent similar issues for others).

Admob for Android Canvas

I have a project on going and I am suppose to place Admob ads on canvas in android. Is it possible to place Admob ads on Canvas in Android.
You can place your canvas holding view in a FrameLayout and a admob AdView on top of it like
<FrameLayout>
<CanvasView/>
<AdView/>
</FrameLayout>
Also if your view is sophisticated and you don't want major performance impact you can draw it in a separate window layer. See my answer here.

Android : game interface

in my game id like to have some interface at the bottom of the screen,
like XP bar, health status etc.
should i create a custom XML view to hold game SurfaceView (to draw the game) and
SurfaceView to draw interface and then share data between two classes
(one huge frame layout for game drawing, and smaller frame layout for interface)
or draw the interface directly on the game canvas?
I would split the interface up in several smaller custom views (probably inheriting from imageview, or progress-bar for the xp-bar) and then put it all in a relative layout. And have the relative and the gameview in a frameview.

Categories

Resources