cocos2d-x 3.1 AdMob iOS and Android integrations (align bottom)

I have looked for this so long. So I post here, for the others, in order to find it easier. This worked for me with cocos2d-x 3.1. I think this is the latest fresh step by step tutorial.

This tutorial links are for iOS:


When you will finish it you will get some linker errors. Just add following frameworks to (http://stackoverflow.com/questions/24844766/linking-errors-when-adding-admob-to-ios-cocos2d-x-3-2):

MediaPlayer.framework
GameController.framework

Now the Android part:
Here is official instructions: https://developers.google.com/mobile-ads-sdk/docs/admob/android/quick-start

Here is what I have done. I don’t know how good is this, but it works:

In AppActivity.java you add this in onCreate

// AdMob
        try {
            
            DisplayMetrics displaymetrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
            int screenWidth = displaymetrics.widthPixels;
            int screenHeight = displaymetrics.heightPixels;
            LinearLayout.LayoutParams adParams = new LinearLayout.LayoutParams(screenWidth, 2 * screenHeight - 75);
            
             // Create an ad.
            adView = new AdView(this);
            adView.setAdSize(AdSize.BANNER);
            adView.setAdUnitId(AD_UNIT_ID);
            
            // Create an ad request. Check logcat output for the hashed device ID to
            // get test ads on a physical device.
            AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .addTestDevice(“INSERT_YOUR_HASHED_DEVICE_ID_HERE”)
                .build();

// Start loading the ad in the background.
            adView.loadAd(adRequest);

// Adding full screen container
            addContentView(adView, adParams);
        } catch (Exception e) {
            Log.d("", "error: " + e);
        }

and also define there methods:

@Override
    public void onResume() {
      super.onResume();
      if (adView != null) {
        adView.resume();
      }
    }
    
    @Override
    public void onPause() {
      if (adView != null) {
        adView.pause();
      }
      super.onPause();
    }

But I think both these solutions have positioning problems on iOS and Android. For iOS if you change

_bannerView = [[GADBannerView alloc] initWithAdSize: kGADAdSizeSmartBannerLandscape];

with

 _bannerView = [[GADBannerView alloc] initWithAdSize: kGADAdSizeBanner];

seems it fixed the positioning problem.

And for Android, I have fixed the problem by using this tutorial: Admob bottom placement tutorial v3.0

My cocos2d-x version is 3.1 and I have changed the code slightly (as there is a private member mFrameLayout`)

AdRequest adRequest = new AdRequest.Builder()
  .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
  .build();

RelativeLayout relativeLayout = new RelativeLayout(this);
mFrameLayout.addView(relativeLayout);


RelativeLayout.LayoutParams adViewParams = new RelativeLayout.LayoutParams(
   AdView.LayoutParams.WRAP_CONTENT,
   AdView.LayoutParams.WRAP_CONTENT);
 // align bottom
adViewParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
 // align center
adViewParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);


relativeLayout.addView(adView, adViewParams);


adView.loadAd(adRequest);
adView.setBackgroundColor(Color.BLACK);
adView.setBackgroundColor(0);

Please write here, if you have a better solution.

1 Like

Hi Narek, I wish I could help, but I don’t know anything about AdMob

I could find the solutions :smile: But thank you very much for your attitude @slackmoehrle!