I have created a small game using Canvas, and now I want to display AdMob ads on top of the canvas. The Example on the AdMob site suggests I do the following:
// Create an ad.
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
// Add the AdView to the view hierarchy. The view will have no size
// until the ad is loaded.
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
layout.addView(adView);
However, I am setting the contentView to a Custom Canvas that I created as follows:
setContentView(new CanvasView(this));
CanvasView:
public class CanvasView extends View {
public CanvasView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
...
How can I display AdMob ads on top of my Custom Canvas?
You'd have to set the parent layout containing the ad and canvas as the content view. Modify your code as follows:
}
Instead of Ad Banners, Try Interstitial Ads so that Ad will automatically populate over the View.
// Create ad request
AdRequest adRequest = new AdRequest();
InterstitialAd interstitial = new InterstitialAd(getActivity(), AD_UNIT_ID);
// Begin loading your interstitial
interstitial.loadAd(adRequest);
// Set Ad Listener to use the callbacks below
interstitial.setAdListener(this);
Make sure your class should implements AdListener.