Android - Admob - Loading ad then changing visibil

2019-05-30 04:47发布

问题:

Long version: I have an Android game. In each "screen" I'm showing the ads BUT the game screen itself - where you actually play. Now, if you launched the app-game (ad begins to load) and then clicked on "Start" before the ad has finished loading - it won't be shown when it should be (only a little strip of it)

Short version: Calling AdView.loadAd and then calling AdView.setVisibility(View.INVISIBLE) before the ad was received will yield a weird result if we call AdView.setVisibility(View.VISIBLE) after the ad is loaded.

This happens:

Instead of, for example:

Now, clicking on this tiny strip left is still counted as clicking on the ad - clicking on it does whatever clicking on the ad would do (launching the browser and such...)

And should you go back to the home screen (I.E click the home button) then get back to the application, the ad is seen back again as it should.

Did anyone encouter it, and does anyone have any idea how to solve it? Thanks.

EDIT: Here is the relevant code:

Creating the ad view:

this.mAdView = new AdView(this, AdSize.BANNER, "xxxxxxxxxxxxx");

Creating the layout params:

final FrameLayout.LayoutParams adViewLayoutParams =
            new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
                    FrameLayout.LayoutParams.WRAP_CONTENT,
                    Gravity.TOP | Gravity.CENTER_HORIZONTAL);

Creating the root view group, and its layout params:

final FrameLayout frameLayout = new FrameLayout(this);
final FrameLayout.LayoutParams frameLayoutLayoutParams =
            new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,
                    FrameLayout.LayoutParams.FILL_PARENT);

Adding to the root view group:

frameLayout.addView(this.mAdView, adViewLayoutParams);

And eventually, setting content view:

this.setContentView(frameLayout, frameLayoutLayoutParams);

回答1:

adContainer = (LinearLayout) view.findViewById(R.id.adViewContainer);
    adContainer.setVisibility(View.GONE);
    adContainer.addView(adView);

// your code for defining adView goes here.

adView.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            super.onAdLoaded();
            adContainer.setVisibility(View.VISIBLE);
        }
    });


回答2:

I have managed to solve it in this way:

  1. In the method which changes the visibility, I first check is the ad was loaded. If it was not, I don't change the visibility:

  2. Set an AdListener for the AdView. in the onReceivedAd method, I check the condition to hide it - if it should be hidden, I hide.

Works fine this way.



回答3:

Maybe you can try to put the admobView inside an AbsoluteLayout and make the Invisible the full layout?