I have an AndEngine game wich subclasses from
public class BaseActivity extends SimpleBaseGameActivity {
}
The game works fine, now when I try to add Admob view to it, I learned from posters on SO to override the onSetContentView() method.
Which I do like this:
@Override
protected void onSetContentView() {
//Creating the parent frame layout:
final FrameLayout frameLayout = new FrameLayout(this);
//Creating its layout params, making it fill the screen.
final FrameLayout.LayoutParams frameLayoutLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.FILL_PARENT);
//Creating the banner view.
AdView adView = new AdView(this, AdSize.BANNER, AdMobPubId);
// Initiate a generic request to load it with an ad
adView.loadAd(new AdRequest());
// set the layout properties
final FrameLayout.LayoutParams adViewLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.TOP | Gravity.CENTER_HORIZONTAL);
// Creating AndEngine's view - Reference made
this.mRenderSurfaceView = new RenderSurfaceView(this);
mRenderSurfaceView.setRenderer(mEngine, null);
//createSurfaceViewLayoutParams is an AndEngine method for creating the params for its view.
final android.widget.FrameLayout.LayoutParams surfaceViewLayoutParams = new FrameLayout.LayoutParams(super.createSurfaceViewLayoutParams());
//Adding the views to the frame layout.
frameLayout.addView(this.mRenderSurfaceView, surfaceViewLayoutParams);
frameLayout.addView(adView, adViewLayoutParams);
//Setting content view
this.setContentView(frameLayout, frameLayoutLayoutParams);
}
The ad gets loaded, but the screen is black. My game is gone.
The screen still registers touches (as I see in the debugger)
How come the engine is not shown in the lower view?!?!
EDIT: Finally got it working
used this code and suddenly it worked, not sure why it didn’t before
@Override
protected void onSetContentView() {
this.mRenderSurfaceView = new RenderSurfaceView(this);
this.mRenderSurfaceView.setRenderer(this.mEngine, this);
final android.widget.FrameLayout.LayoutParams surfaceViewLayoutParams = new FrameLayout.LayoutParams(super.createSurfaceViewLayoutParams());
//Creating the banner view.
AdView adView = new AdView(this, AdSize.BANNER, AdMobPubId);
adView.loadAd(new AdRequest());
final FrameLayout.LayoutParams adViewLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.RIGHT | Gravity.CENTER_HORIZONTAL);
final FrameLayout frameLayout = new FrameLayout(this);
final FrameLayout.LayoutParams frameLayoutLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.FILL_PARENT);
frameLayout.addView(this.mRenderSurfaceView,surfaceViewLayoutParams);
frameLayout.addView(adView,adViewLayoutParams);
this.setContentView(frameLayout, frameLayoutLayoutParams);
}
It doesn’t work because you change the layout after it has been inflated. You can’t add views after you already called
setContentViewand inflated the layout.All you have to do is to create the
AdViewearlier and add it to the layout before AndEngine callssetContentView. You can see an example of doing so here.I’m using
FrameLayoutin this example, it’s taken from my game whereFrameLayoutworks the best. YouronSetContentViewmethod will be simpler – all you have to do is to instatiate theRenderSurfaceViewobject and theAdViewobject, then add them to a newLinearLayoutand callsetContentView.