AdMob Integration with Cocos2d-x for Android

Now I understand what you want.
When I try to add AdView(for Admob) in cocos2d-x for the first time I try to do the same way you want now, but I try to change the Cocos2dxActivity instead of try to change Cocos2dxGLSurfaceView.
I look to Cocos2dxGLSurfaceView and I don’t know how to change it, since it extends GLSurfaceView, but you can change the init() method from Cocos2dxActivity to achieve what you want, but I don’t recommend, since cocos2d-x 2.0.3 or later versions use the same Cocos2dxActivity and Cocos2dxGLSurfaceView for all projects that you have(at least in android).
I mean if you change it Cocos2dxActivity, all your games you show the same adview, including the same ID for all your projects, this is bad, since each app need to have a unique admob id.
I hope you can understand my explaination.

I understand. It’s difficult to resize the Cocos2dxGLSurfaceView. I also understand all the changes and why this could be bad as you mentioned.
Still I was hoping that I don’t need to handle the Ad’s 50px in the actual game code. Now I have to account the mentioned 50px in every calculation in the game. If I would change the Cocos2dxGLSurfaceView in one place (layout in android java code), everything in the game code would stay the same.

I’ll manage somehow. Thanks for your time.

Hi guys,
I’ve tried to implement Admob as Fabio wrote. It seems to be working well but there is one little problem.

Sometimes (one attempt from ten, pretty random stuff) the bug appears. All game input is blocked! Even back button or volume control doesn’t work (I can only press HOME)! But the game itself seems to be working - animations are animated, music is looped in right way and so on.
Logcat is silent…

Don’t you have any experience like this?

Thanks for any help.

Jiri from Mingle Games

Hi Jiri Formanek,
I never experience a problem like yours, I put this admob banner implementation in 2 games, none of them have appear to have a similar problem.

Thanks i use it too.

Got compatibility problems with:

@ LinearLayout.LayoutParams adParams = new LinearLayout.LayoutParams(
getWindowManager().getDefaultDisplay().getWidth(),
getWindowManager().getDefaultDisplay().getHeight()+getWindowManager().getDefaultDisplay().getHeight()50);
@
I use the lines below, but i do not know if it is wise to use it :
@ DisplayMetrics metrics = new DisplayMetrics;
getWindowManager.getDefaultDisplay.getMetrics;
LinearLayout.LayoutParams adParams = new LinearLayout.LayoutParams
(50+50));
@

Also looking for a way to pause cocos2d-x on adMob click (found it: http://www.cocos2d-x.org/boards/6/topics/3153?r=24875#message-24875)

What exactly compatibility problems do you have with my code?

I think that your code will not work on earlier version of android.
Try to test your code in android emulator, try diffent emulator configurations, test in many resolution screens as you can.

The ‘problem’ is .getWidth is deprecated (i am new to android so i do not want to have warnings) thats why i want to use .getMetrics but i do not know if this functions the same on big screens (retina style).

Karthik k uses:
@
Point size = new Point();
display.getSize(size);
@

and i read constructions like:
@
Point size = new Point();
Display display = getWindowManager().getDefaultDisplay();
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB_MR2) {
size.set(display.getWidth(), display.getHeight());
} else
getWindowManager().getDefaultDisplay().getSize(size);
@
So i am not sure what is the right way to do it.

The .getMetrics method woll not work on earlier versions of android, if your app is intended to work only in newest android versions you can use getMetrics method without problems.
I look at your code and seems you will develop for android honeycomb, that is firmware 3.0, api 11.
Today are many android phones that use version 2.3.3, that is api 10, your app will not work on these phones.
There’s no a correct path to follow, you need to try and see what works better for you.

I forgot to tell one thing.
The admob view will be over the cocos2dx view.
When I say HelloCpp project, I reffer to the HelloCpp (or HelloWorld) project that is inside ‘samples’ folder of cocos2d-x.

Hey and thanks for this solution.

I just have 1 question, which is probably trivial but I don’t get it:

How come the height parameter in new LinearLayout.LayoutParams is basically
2*height - banner’s height
instead of
height - banner’s height

?

There’s many time that I use this code, when I create it I remember that I have problems to position the banner in the right position.
I remember to use something that double the height position to put the banner in the right place.
But I don’t remember if this will put the banner in the top or bottom of the app.
Make tests and see what suits you better.

LOL, and I thought I was crazy there for a moment :slight_smile: Thank you for your answer but this is not a good approach, certainly not with Android.

Fabio Cunha, I’ve just tested your code (i took it from your blog :slight_smile: ). It works thanks! Tell me please, is there an ability to place a banner under the game at the bottom? Not just over the game at the bottom. So I wanna place my game layout above the admob banner. I can make LinearLayout with two vertical cells, one for game, another for banner, but I don’t know how to add the game layout at the first one?

Airex Rest, If I understand correctly you want to create one linear layout that have two linear layouts inside.
The top layout will show cocos2d-x game and the bottom layout will show the admob banner. This way the admob banner will always be visible and cocos2d-x game layout will not interfere with admob banner, is this right? Is that you want to do?

Fabio Cunha wrote:

Airex Rest, If I understand correctly you want to create one linear layout that have two linear layouts inside.
The top layout will show cocos2d-x game and the bottom layout will show the admob banner. This way the admob banner will always be visible and cocos2d-x game layout will not interfere with admob banner, is this right? Is that you want to do?

Yeah, you are absolutely right! :slight_smile:

Airex Rest, I have the same request from rDevsign .raw some months ago, but unfortunately I didn’t know how to help him because I don’t know in that time.
But now I have knowledge to do that.
First, find the Cocos2dxActivity file and open it.
Find the init() method and clear everything inside this method and put this code inside it.

        // FrameLayout
        ViewGroup.LayoutParams framelayout_params =
            new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                                       ViewGroup.LayoutParams.FILL_PARENT);
        FrameLayout framelayout = new FrameLayout(this);
        framelayout.setLayoutParams(framelayout_params);

        // Cocos2dxEditText layout
        ViewGroup.LayoutParams edittext_layout_params =
            new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                                       ViewGroup.LayoutParams.WRAP_CONTENT);
        Cocos2dxEditText edittext = new Cocos2dxEditText(this);
        edittext.setLayoutParams(edittext_layout_params);

        // ...add to FrameLayout
        framelayout.addView(edittext);

        // Cocos2dxGLSurfaceView
        this.mGLSurfaceView = this.onCreateView();

        // ...add to FrameLayout
        framelayout.addView(this.mGLSurfaceView);

        // Switch to supported OpenGL (ARGB888) mode on emulator
        if (isAndroidEmulator())
           this.mGLSurfaceView.setEGLConfigChooser(8 , 8, 8, 8, 16, 0);

        this.mGLSurfaceView.setCocos2dxRenderer(new Cocos2dxRenderer());
        this.mGLSurfaceView.setCocos2dxEditText(edittext);

        LinearLayout ll_full=new LinearLayout(this);
        LinearLayout ll_1=new LinearLayout(this);
        LinearLayout ll_2=new LinearLayout(this); 

        int myArbitraryHeightTop = 270;
        int myArbitraryHeightBottom = 60;

        ll_full.setLayoutParams(framelayout_params);
        ll_1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, myArbitraryHeightTop));
        ll_2.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, myArbitraryHeightBottom));

        //ll_2.addView(bannerView);

        ll_full.addView(ll_1);
        ll_full.addView(ll_2);

        ll_1.addView(framelayout);
        // Set framelayout as the content view
        setContentView(ll_full);

My code don’t use XML.

This will create three linear layouts, the first one will hold the other two linear layouts, I add the cocos2dview to the linearlayout ll_1, the second linearlayout will hold the bannerView.

See there’s a code that have a comment, this will be the place that you add the AdView from Admob.

Since the First layout that I use is a LinearLayout, you need to define the height of the two sub linear layouts.

I defined 270 and 60 pixels, because my device has 320 pixels of heigth, but you need to get the screen height to put correct values, I think will probably better create layouts in the same order that Fernando Altuzar created.

I think my code will help you to begin create the solution for your problem.

hi……
please help me to how to implement admob ads in cocos2dx android project
step by step. i will try many time but it’s give exception in device not in visual studio.

mohit parihar, write the exception that appear on the log, without that is impossbile to help you.
I not work with visual studio, you will need to find the log messages inside visual studio
I use eclipse, for me it’s easier than visual studio

Fabio Cunha
Hi. Please tell me you are using the plugin-x? Do you have any problems with integration AdMob? See my post: http://www.cocos2d-x.org/forums/6/topics/36042
Thanks!

I don’t use Plugin-X. I just use JNI to connect anything with cocos2d-x. Admob ads, Applovin ads, including Socialize(twitter and facebook connection).
Unfortunately I don’t know how to help you using plugin-x.