Android: Admob banner without overlapping the main view

I am trying to add an Admob banner to a custom SurfaceView (my main game screen). I can place a banner at the top or bottom of the screen, but in both cases it overlaps the game screen (and hides some essential user interface elements). How can I make the banner resize the game screen upon login so that it doesn't overlap anything?

This thread provides an xml solution: How to get the ad displayed at the bottom of the screen without overlapping , but since my view is written in java, I need a way to do this programmatically. I tried to translate it, but no banner appears when I implement the code. Below are two solutions: the first one does not show the banner, and the second one shows the banner below, but with overlapping. What parameters do I need to change to get rid of the overlap?

/* Admob advert on Android done programmatically! */
    adView = new AdView(this, AdSize.BANNER, "a14dc6ed8aead31");
    gameView = new GameView(this, gameEng, adView);

    //no banner displays with this code
    RelativeLayout rl = new RelativeLayout(this);
    RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
            RelativeLayout.LayoutParams.FILL_PARENT);
    RelativeLayout.LayoutParams gameViewParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, 
            RelativeLayout.LayoutParams.WRAP_CONTENT); 
    adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    gameViewParams.addRule(RelativeLayout.ABOVE);
    rl.addView(adView, adParams);
    rl.addView(gameView, gameViewParams);
    setContentView(rl);

    //a banner at the bottom displays, but it overlaps my game screen
    /*FrameLayout layout = new FrameLayout(this);
    FrameLayout.LayoutParams gameParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,
            FrameLayout.LayoutParams.FILL_PARENT);
    FrameLayout.LayoutParams adsParams =new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, 
            FrameLayout.LayoutParams.WRAP_CONTENT, android.view.Gravity.BOTTOM|android.view.Gravity.CENTER_HORIZONTAL); 
    layout.addView(gameView, gameParams);
    layout.addView(adView, adsParams);
    setContentView(layout);*/

    AdRequest request = new AdRequest();
    request.setTesting(true);
    adView.loadAd(request);
+3
source share
1 answer

I never solved this, but I found a job. In case anyone else is faced with this problem, try just moving all your drawing coordinates up: subtract the height of the advertising banner (you can get this using the getHeight () method).

, . .

0

All Articles