Include AdMob Banner on Cocos2d-x Android Game

Hi,

In this post we will show a piece of code that will help you to add an admob banner on your cocos2d-x game in android.

Firstly, you need to setup the google play services on your app. Then you need to include on your AndroidManifest.xml the Admob activity and the required permissions.

    <activity android:name="com.google.android.gms.ads.AdActivity"
             android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>         

Then on your Activity, you need to put this piece of code on the onCreate method:

protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        AdView adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId("YOUR AD UNIT ID");

        RelativeLayout relativeLayout=new RelativeLayout(this);
        RelativeLayout.LayoutParams layoutParams1=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        RelativeLayout.LayoutParams layoutParams=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
        layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
        AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice("B3EEABB8EE11C2BE770B684D95219ECB")
        .build();

        adView.loadAd(adRequest);
        relativeLayout.addView(adView, layoutParams);
        this.addContentView(relativeLayout, layoutParams1);

}

I hope this help.

See you on the next post.