androidcocos2d-xandroid-framelayoutheyzap

Displaying a Banner over Cocos2d-x view


Whilst following the instructions on the HeyZap website with my cocos2d-x project, I noticed that the banner is added to a FrameLayout like so:

FrameLayout bannerWrapper = (FrameLayout) findViewById(R.id.banner_wrapper);

I created an activity_main.xml file in the layout folder using Android Studio and created a simple FrameLayout like this:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/banner_wrapper"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
</FrameLayout>

In order for me to access the newly created 'banner_wrapper' FrameLayout, I had to call setContentView(R.layout.activity_main); in my OnCreate() method. This does indeed show the banner but is probably also resulting in the Cocos2dx view not being displayed. Is there a way how to overlay my 'banner_wrapper' over the original Cocos2dx view? Or is there an even better way to do this?


Solution

  • I managed to solve this by skipping the activity_main.xml file altogether and creating all necessary views in code:

    LinearLayout layout = new LinearLayout(activity);
    int gravity= Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
    layout.setGravity(gravity);
    activity.addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    BannerAdView banner = new BannerAdView(activity);
    layout.addView(banner);
    layout.bringToFront();
    banner.load();