Thursday, April 30, 2015

Dynamic Admob layout in android to display an ad

I am making a free app that has an ad in it, and I want the ad to be some kind of dynamic, like if there are no ads serve, the layout that wrap the ad will not be visible. It's just like this.


  1. Guidelines of implementing the ad
  2. Make sure that the layout of the ad is set to View.GONE. We will show it only if there is an ad serve.
  3. Add the AdListener
mAdView = (AdView) findViewById(R.id.adView);
mAdView.setAdListener(new AdListener() {
    @Override
    public void onAdClosed() {
        super.onAdClosed();
    }

    @Override
    public void onAdLoaded() {
        super.onAdLoaded();
        mAdView.setVisibility(View.VISIBLE);
    }

    @Override
    public void onAdOpened() {
        super.onAdOpened();
    }

    @Override
    public void onAdFailedToLoad(int errorCode) {
        super.onAdFailedToLoad(errorCode);
        mAdView.setVisibility(View.GONE);
    }
});
As you see, we only set the method onAdLoaded() to be visible and onAdFailedToLoad to be gone. Because, 
onAdOpened() - This will only happen if the ad will overlay the app after the user taps the ad. onAdClosed() - This will trigger if the overlay of the ad will be closed by the user.

That's it. Thanks.