How do you display Admob banner on Android


#1

Hi,

How do you display Admob banner?

I used below link (C++ part) but it does not work.
https://developers.google.com/admob/games?hl=en#opengl_games_-_banner_ads

I have to add height explicit to make it working:

  int height = getDisplaySize().y - AdSize.SMART_BANNER.getHeightInPixels(this);
  // Create a RelativeLayout as the main layout and add the gameView.
  RelativeLayout mainLayout = new RelativeLayout(this);
  RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
  		LayoutParams.MATCH_PARENT, height);
  mainLayout.addView(gameView, params);

Above code makes space for banner (squeezes cocos view ).

Questions:

  1. Is it OK. Will it work on all Android devices?
  2. What is recommended way?

p.s.
I do not want to use SDKBOX, AnySDK etc.

Regards,
CHP


#2
public class AppActivity extends Cocos2dxActivity {

  private static AppActivity _appActivity;
   private AdView mAdView;

 @Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
_appActivity = this;

 //======================BANNER=============================//


mAdView = new AdView(this);
mAdView.setAdSize(AdSize.SMART_BANNER);
mAdView.setAdUnitId("ca-app-pub-BLALLAA");

RelativeLayout relativeLayout = new RelativeLayout(_appActivity);
mFrameLayout.addView(relativeLayout);

RelativeLayout.LayoutParams adViewParams = new RelativeLayout.LayoutParams(AdView.LayoutParams.WRAP_CONTENT, AdView.LayoutParams.WRAP_CONTENT);
adViewParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adViewParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);


AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);

mAdView.loadAd(adRequestBuilder.build());

relativeLayout.addView(mAdView, adViewParams);

mAdView.setVisibility(View.GONE);
mAdView.setEnabled(false);

//========================================================//

}

public static void showBanner() {

_appActivity.runOnUiThread(new Runnable() {
    @Override
    public void run() {

        if (_appActivity.mAdView != null) {
            _appActivity.mAdView.setVisibility(View.VISIBLE);
            _appActivity.mAdView.setEnabled(true);
        }

    }


});
}


public static void hideBanner() {

_appActivity.runOnUiThread(new Runnable() {
    @Override
    public void run() {

        if (_appActivity.mAdView != null) {
            _appActivity.mAdView.setVisibility(View.GONE);
            _appActivity.mAdView.setEnabled(false);
        }

    }
});
}

}

#3

energyy,

Thank you for answer.
This display banner on top of cocos2dx view (overlap cocos2dx game).

How can I squeeze cocos2dx view (make space for banner) and put banner there?

Regards,
CHP


#4

Like 99.9% games showing overlay banner as u can remove ads and then get full screen. If u still want to do that, re-resize your game interface to keep that place for banner which will be still overlay.


#5

The cocos2d-x view can be reached with getGLSurfaceView() (in your activity). The only way not to overlay both, is to change the LayoutParams of this GLSurfaceView. If you need more information, give more me information, where du you need the Admob banner.


#6

mars3142,

I would like to see banner on the bottom.
Could you provide sample code?

Regards,
CHP


#7

I’ve checked the Cocos2dxActivity and the GLSurfaceView is embedded into a FrameLayout. There are no build-in dependencies between views. So we need to do more work to change it (incl. the framework), if we don’t want to create nested layouts. Do you really want this? This could create a lot of work in the future, if you want to update cocos2d-x in your project.


#8

mars3142,

OK. Thank you for answer.
I will do what @energyy shows in How do you display Admob banner on Android

Regards,
CHP


#9

I will create a pull request for changing the FrameLayout into a RelativeLayout, so it will be easier in the future to add views on top, bottom, left or right to the GLSurfaceView.