I can't seem to figure out how to display ads on my app while currently, instead of setting my content view to the layout file, I am setting my view to a custom class which draws a canvas. I have the following:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
MobileAds.initialize(getApplicationContext(), "ca-app-pub-3940256099942544~3347511713");
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
setContentView(new GamePanel(this));
}
Of course I get a null pointer error. Of course this happens because I edit the AdViews before setting the content view, but even if I switch the order it doesn't work (even without an error I am worried it will just be hidden behind my canvas). Here is my GamePanel
class.
public class GamePanel extends SurfaceView implements SurfaceHolder.Callback {
public GamePanel(Context context) {
super(context);
getHolder().addCallback(this);
thread = new MainThread(getHolder(), this);
thread.run();
setFocusable(true);
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
//draw a bunch of gameobjects...
}
public void update() {
//update objects
}
//required methods...
}
And essentially in the main thread I just loop through frame cycles, call the GamePanel
's update()
and draw(Canvas canvas)
methods and then use surfaceHolder.unlockCanvasAndPost(canvas);
(where surfaceHolder
is the one corresponding to the GamePanel) to draw the canvas to the screen. I don't use Android Studio's view structure, so I don't know how to implement an Ad View.