How to implement Admob in Cocos2d-x 2.0.3

Hi,
I upgrade my app to use Cocos2d-x 2.0.3 recently. I cannot figure out how to implement Admob. I know how to do that in 1.x.
I am trying to do this by the xml way. I find that there is no .xml in the layout folder. If I create a .xml in the layout folder and use the code “setContentView(R.layout.main)” to load, it gives me a black screen with an ad on the bottom(no content).

Can someone tell me how to do this?
Thank you very much.

I looking for same solution.
As i can see layouting in 2.0.3 performed in java code.
I tried to add LinearLayout, add cocos2dx FrameLayout in it and then add AdView
But FrameLayout consume all space and give AdView 0px space.
Will be thankful for any solution.

update:
If i add AdView before FrameLayout game screen is overlapped and touch coordinates shifted down
seems that touch coordinates calculated according view size, but GLView uses screen coordinates instead of view

I used the following instruction and it works perfectly on 2.0.3.

http://cocos2d-x.org/boards/6/topics/13233

As the setup of cocos2d-x changed in 2.0.3. (java code is now a separate library project) i’ve added the code to: Cocos2dxActivity

Only i have to do now is find a way to disable admob (after inapp purchases when i have that working).

This solution works for me with some issues.

  1. Seems AdView changes size on receive ad, but opengl view remains with same size. It cause game elements out of screen (covered by ad banner)
  2. I tried to modify example to show ad at top of screen, but ad not shows

Ernest Poletaev wrote:

This solution works for me with some issues.

  1. Seems AdView changes size on receive ad, but opengl view remains with same size. It cause game elements out of screen (covered by ad banner)
  2. I tried to modify example to show ad at top of screen, but ad not shows

I will post my code tonight (i’m at work now), i will also try if i have the same problems as you have.
My upcoming game is in landscape, with the example the admob banner was shown in the topleft corner and with a small modification it’s now at the centerbottom.
I had no issues with size changes, normaly the banner has the same size each time?

I tried to use https://developers.google.com/mobile-ads-sdk/docs/admob/smart-banners

But when i changed to SMART_BANNER to BANNER i got it down-right aligned inside GL view full screen

What i want to get is vertical stack:

[banner]
[gl-view]

instead of this, banner resides inside gl-view

update:
I removed RelativeLayout and set height of AdView in pixels, after that i get it in vertical stack. (this points to initial zero size of AdView).
But banner fills only 50% width on my 800x480 tablet, and i cannot know what size will be with SMART_BANNER, cause it depends on sceen size and available ads. If use 90px - for smart banner it will take too much space on small screens. Seems it no have universal solution

This is the code i use:

XML layout:

This is the code in Cocos2dxActivity

Admob import:

import com.google.ads.AdView;

Change the init() to the following (without the facebook part).

    public void init() {

        // Init facebook
        myContext = getApplicationContext();        
        Cocos2dxActivity.facebookConnector = new FacebookConnector(FACEBOOK_APPID, this, getApplicationContext(), new String[] {FACEBOOK_PERMISSION});

        // Init handler
        this.mHandler = new Cocos2dxHandler(this);

        // View
        setContentView(R.layout.main);

        // Cocos2dxGLSurfaceView
        this.mGLSurefaceView = (Cocos2dxGLSurfaceView) findViewById(R.id.game_gl_surfaceview);       
        this.mGLSurefaceView.setCocos2dxRenderer(new Cocos2dxRenderer());

        // Admob view
        this._adMob = (AdView) findViewById(R.id.adView);
        this._adMob.setVisibility(AdView.VISIBLE);
     }

http://cocos2d-x.org/boards/6/topics/13233

This code also works fine to me!

But when I cope with the Admob, I cannot figure out how can the Admob be disabled (setVisibility(AdView.INVISIBLE)) by using JNI as it always have an java.lang.nullpointerexception when I call it using JNI.

Pan HO,
I’ve done it, making static method in my Activity class

Do you mean that in my Activity class there is a method

public static void ShowOrHideAds()
    {
        // my code

    }

and call the method using JNI in native code (C++)?

Besides, has someone tried that if the below code in my activity.java is deleted, the adView will also appear automatically

// Admob view
        mAdView = (AdView)this.findViewById(R.id.adView);
        mAdView.setVisibility(AdView.VISIBLE);
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, "com/company/app/Activity", "ShowOrHideAds", "()V")) {
    t.env->CallStaticVoidMethod(t.classID, t.methodID, i);
    t.env->DeleteLocalRef(t.classID);
}

I can already call the java method using JNI in native code
But I cannot control the visibility of the adView inside my activity.java
It seems that even if I don’t call the following code

            mAdView = (AdView)this.findViewById(R.id.adView);
            mAdView.setVisibility(AdView.VISIBLE);

The Admob Ads can also display:(

You have to perform changes in it’s own thread

It can be done, using post method

this.adView.post( new Runnable() {
   @Override
   public void run() {
      // your code here
   }
});

Thank you very much
I have tried something like this before (like creating an adsHandler to handle it)

        adsHandler.post(showAdsRunnable);
    final Runnable showAdsRunnable = new Runnable() {
        public void run() {

            ShowOrHideAds();
        }
    };

May be I will try using the method you suggested and see if it can work

Hi Ernest, Pao: How did you get set visible/invisible the Ad from the static method via adView.post?

My code in Activity:
public static void hideBanner() {
mAdView.post( new Runnable() {
public void run() { }
});
}

The hideBanner method is invoked fine, but how can i hide the Ad from the new Runnable run()?

Thanks

I get it:

public static void hideBanner() {
mAdView.post( new Runnable() {
public void run() { mAdView.setVisibility(AdView.INVISIBLE); }
});
}

Thank you very much