Cocos2dxGLSurfaceView hide my addView withusing admob for android device

Hello everyone.
I’ll try to display admob on Android Device.
Curiously, When I run my game. First Time, I can’t see addView screen.
But Lock and UnLock, If I come back my game. Then I can see addView without any problem.
I’m searching many code snip.
But I don’t find correct answoer.
This just my own isseu? Kindly can you check my source code.
Why,First Time, I can’t see admob screen.

import org.cocos2dx.lib.Cocos2dxActivity;
import org.cocos2dx.lib.Cocos2dxGLSurfaceView;

import android.app.ActionBar.LayoutParams;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

public class FlappyRookie extends Cocos2dxActivity {

private AdView adView;
private static final String AD_UNIT_ID = "xxxxxxxxxxx";

protected void onCreate(Bundle savedInstanceState){
	super.onCreate(savedInstanceState);	
	
	
	 LinearLayout.LayoutParams adParams = new LinearLayout.LayoutParams(
             getWindowManager().getDefaultDisplay().getWidth(),                        
              LinearLayout.LayoutParams.WRAP_CONTENT);
    
	
	    adView = new AdView(this);
	    adView.setAdSize(AdSize.BANNER);
	    adView.setAdUnitId(AD_UNIT_ID);
	 
	    
	    AdRequest adRequest = new AdRequest.Builder()
	        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
	        .addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
	        .build();
	    
	    adView.loadAd(adRequest);
	    addContentView(adView, adParams);

	
}

public Cocos2dxGLSurfaceView onCreateView() {
	Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
	// FlappyRookie should create stencil buffer
	glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);
	return glSurfaceView;
}

static {
    System.loadLibrary("cocos2dcpp");
}

@Override
protected void onResume() {
	// TODO Auto-generated method stub
	super.onResume();
	if (adView != null) {
      adView.resume();
    }
}

@Override
protected void onPause() {
	// TODO Auto-generated method stub
	 if (adView != null) {
	      adView.pause();
	}
	super.onPause();
}

}

Try activity.runOnUiThread(…).

Example:

      me.runOnUiThread(new Runnable() {
      public void run() {
          // Insert your admob code here.

       });

Hello suveerjacob.
First Thanks for your reply.
I changed my code as below. But Result is same.
Currently, I don’t want to detail control about admob.
Just Display fixed area from game start to end
(I found, If we want to detail control we have to integrate with JNI)

protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
me = this;

        {
            @Override
            public void run()
            {
            	 LinearLayout.LayoutParams adParams = new LinearLayout.LayoutParams(
                         getWindowManager().getDefaultDisplay().getWidth(),                        
                          LinearLayout.LayoutParams.WRAP_CONTENT);
                
        		
        		    adView = new AdView(me);
        		    adView.setAdSize(AdSize.BANNER);
        		    adView.setAdUnitId(AD_UNIT_ID);
        		 
        		    
        		    AdRequest adRequest = new AdRequest.Builder()
        		        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        		        .addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
        		        .build();
        		    
        		    adView.loadAd(adRequest);
        		    addContentView(adView, adParams);
            }
        } );

Maybe, This is not thread issue. Why, First time my adview under Game Screen(Always)
After long time,result is same.

But,I already told this situation. After Locking Android screen and unlock, re-entrance my game.
Then, Admob display top of screen correctly.

very interesting, but I don’t know reason.
Thanks.

yes it is because there is a difference between the solution i gave to you n the solution you are trying…
just try:
me.runOnUiThread(new Runnable() {
public void run() {
// Insert your admob code here.
}
});

from the above solution i can see that you are not using ‘me.runOnUiThread(…)’

I dont know the exact n detailed explanation of runOnUiThread(…), use it n let me know if it is working or not…


There can be another problem that you are calling AdView in constructor before the before loading cocos2dx libraries…
For tat problem, yes have to manage ad initialization from your c++ code, i.e you have to use JNI.
For JNI you can google it. I am sending some links you can refer it…
http://www.cocos2d-x.org/wiki/How_to_use_jni


Just FYI, Google and Apple are both rejecting Flappy games :wink:

Hello.
I found solution. This is not JNI issue.
Simpley, This is come from admob with using google play service.
For solving this issue, I just add below line

adView.loadAd(adRequest);
adView.setBackgroundColor(Color.BLACK);
addContentView(adView, adParams);

Then without any problem, From application starting, I can see admob view.
Thanks.

If you need further infomation can you refer below
http://stackoverflow.com/questions/17975659/how-to-duplicate-sprite-of-sprites-in-cocos2d-x-2-1-4

Strange but it worked - Thanks
If somebody wants to remove the black line after, just set background color to 0

                    adView.loadAd(adRequest);
                    adView.setBackgroundColor(Color.BLACK);
                    addContentView(adView,adParams);
                    adView.setBackgroundColor(0);

Hi… when game is running with google ad (google services) … I lock the screen and unlock the screen and the ad dissappers… I tried the solution above… atleast my ad shows… but unless revmob loads an another ad the google ad doesnt show… pls help

to ymkimwizard,
firstly i want to thank you very much because i was having the very same problem, and searching all around to try to solve it, at last your solution worked for me. (i was thinking that it might because of the child index problem, and tried to bring adview to front, but failed)

but i still don’t know how you found the solution, because i couldn’t see any relationship between your reference link “how-to-duplicate-sprite-of-sprites-in-cocos2d-x-2-1-4” and your solution, which solve the problem by calling setBackgroundColor function of adview, maybe you can point out that if you someday come back to this thread.