Search code examples
androidadmobcocos2d-android

AdMob + cocos2d: How display banner over game scene?


I'm trying to create my first game based on cocos2d engine but I stuck with AdMob implementation. Banner displays well but if I run the game it overlaps the banner. I've tried a lot of variants but nothing works.

How can I display AdMob banner over the game? I will appreciate any help.

Here is my MainActivity.java

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
        mGLSurfaceView = new CCGLSurfaceView(this);

        setContentView(R.layout.activity_main);

        adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId("xxxxxxxxxxxxxxxxxx");

        AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        .build();

        adView.loadAd(adRequest);

        LinearLayout game_layer = (LinearLayout)findViewById(R.id.gameLayout);
        game_layer.addView(mGLSurfaceView);

        LinearLayout banner_layer = (LinearLayout)findViewById(R.id.adLayout);
        banner_layer.addView(adView);
    }

    @Override
    public void onStart() {
        super.onStart();
        CCDirector.sharedDirector().attachInView(mGLSurfaceView);
        CCDirector.sharedDirector().setDisplayFPS(true);
        CCDirector.sharedDirector().setAnimationInterval(1.0f / 60.0f);
        CCScene scene = GameStartLayer.scene();
        CCDirector.sharedDirector().runWithScene(scene);
    }

and activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:id="@+id/linearLayout"
                  android:orientation="vertical"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
            <LinearLayout
                android:id="@+id/adLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" 
                android:layout_alignParentBottom="true">
            </LinearLayout>
            <LinearLayout
                android:id="@+id/gameLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>

Solution

  • Your problem is with your layout. You have told gameLayout to consume all of the height of it's parent. Try using the following instead:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:id="@+id/linearLayout"
                  android:orientation="vertical"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
            <LinearLayout
                android:id="@+id/gameLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical" >
            </LinearLayout>
            <LinearLayout
                android:id="@+id/adLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
        </LinearLayout>
    </LinearLayout>
    

    The layout_weight will let gameLayout expand to fill the remaining space.