Search code examples
androidadmobadview

How do I show and hide admob banner ads?


Giving another chance to this comunity, my latest questions weren't ever answered

Well I have a game, the game have a pause button that hide most of the game interface to just show a pause text in the middle. There are so much free space, so I thought to put a banner at the bottom until the pause button is pressed again and resumes the game.

I know how to make banners work:

//When pause button is pressed
AdView adView = (AdView) this.findViewById(R.id.adView);
adView.setVisibility (View.VISIBLE);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);

But I don't know how to stop them when pause button is pressed again, just this:

adView.setVisibility (View.GONE);

I am pretty sure adView wont stop making requests with this line only.

I see some questions about this here but looks like they were using older admob SDK versions.

can somebody please, PLEASE help me?

Thanks.


Solution

  • You can try to Destroy and hide the AdView when button click and load the ad back when required.

        final AdView adView = (AdView) this.findViewById(R.id.adView);
        final AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);
    
        button.setText("ClickMe");
        button.setOnClickListener(new View.OnClickListener() {
             boolean isPause = false;
            @Override
            public void onClick(View v) {
                if(isPause){
                    adView.loadAd(adRequest);
                    adView.setVisibility(View.VISIBLE);
                    isPause = false;
                }else {
                    adView.destroy();
                    adView.setVisibility(View.GONE);
                    isPause = true;
                }
            }
        });