Android: AdView not getting displayed, though Log shows Ad is loaded

Hi,

I am trying to implement Admob directly in Android without going through pluginx, or changing anything is my js files…

Created AdUnit and implemented the AdView as follows:

            AdRequest request = new AdRequest.Builder()
		    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
		    .addTestDevice("AC2DFFFAFB44FE1EBC80F6B40559AB14")
			.build();
		
		AdView adView = new AdView(AppActivity.this);
		adView.setAdSize(AdSize.BANNER);
		adView.setAdUnitId("ca-app-pub-XYZ/XYZ");
		adView.loadAd(request);
		
		@SuppressWarnings("deprecation")
        ViewGroup.LayoutParams ad_layout_params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);                        
		adView.setLayoutParams(ad_layout_params);

		mFrameLayout.addView(adView);

In Logcat, I am getting the following, the AD is not displayed !!

Starting ad request.
JS: [Some JS Logs]
Ad finished loading.
Scheduling ad refresh 60000 milliseconds from now.
Ad is not visible. Not refreshing ad.
Scheduling ad refresh 60000 milliseconds from now.
Ad is not visible. Not refreshing ad.
Scheduling ad refresh 60000 milliseconds from now.
Ad is not visible. Not refreshing ad.
[repeating]

I guess, the admob Integration is done right, but issue with adding the AdView to the right parent view or issue with setting the position of AdView.

As per this thread (http://stackoverflow.com/questions/25089759/move-adview-to-bottom-cocos2dx-activity), changed the mFrameLayout in Cocos2dxActivity to RelativeLayout

PS: Not sure if this is the right forum to post this, since its completely related to android !! Moderators, please move it to right place, If it dont fit here :smile:

Resolved the issue…

the glSurfaceView that is getting returned by onCreateView is added to mFrameLayout in the superclass Cocos2dxActivity, hence taking over the adview.

Hence,

  • assigned some tag(999) to the AdView in here,
  • in the super class Cocos2dxActivity,
    – once the glSurfaceView is added to mFrameLayout,
    – the child with the tag 999 (our adview) is brought to the front…

Code:

onCreateView of our Activity:

        AdRequest request = new AdRequest.Builder()
            .build();

        AdView adView = new AdView(AppActivity.this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId("ca-app-pub-XYZ");
        adView.setId(999);
        adView.loadAd(request);

        @SuppressWarnings("deprecation")
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);    
        adView.setLayoutParams(params);

        mFrameLayout.addView(adView);

In frameworks/js-bindings/cocos2d-x/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java:190 end of init function,

mFrameLayout.bringChildToFront(mFrameLayout.findViewById(999));
2 Likes