Search code examples
androidopengl-esadmobcocos2d-android

Cocos2d Android - Combining CCGLSurfaceView and other views (eg. Admob or Imageview)


I'm stuck in the problem to get a combined CCGLSurfaceView working together with other android native views like an ImageView for example. I started to develop a game using Cocos2D and I'm planning to add advert via admob in it later. Therefore I want to run the CCGLSurfaceView (OpenGl View for my game) at the top and the advert should be displayed below it.

I already tried several things like adding both views manually (not via XML) to a relative layout but keep getting null pointers and my app force closes.

I also tried this tutorial where they explain how to integrate admob into a OpenGl SurfaceView. But still it didn't work, maybe it's because I'm using Cocos2d?

I don't know, can someone help me out with this (maybe cocos2d specific) problem?


Solution

  • We need your code (putting a link to what you based yourself on is not very helpful in the overall)

    My approach is to define all this in the layout

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:gravity="center"
            android:orientation="vertical" 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
    
            <LinearLayout 
                android:gravity="center_horizontal"
                android:orientation="horizontal"
                android:layout_alignParentTop="true"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
    
            </LinearLayout>
    
            <android.pixelrain.opengl.GLSurfaceViewChipmunk android:id="@+id/composed"
                android:gravity="center"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
            />
    
            <LinearLayout
                android:id="@+id/linearLayoutAd"
                android:layout_height="wrap_content"
                android:layout_width="fill_parent"
                android:layout_alignParentBottom="true"
                android:orientation="vertical"
                android:gravity="center_horizontal"
            />
    
    </RelativeLayout>
    

    Note the GLSurfaceViewChipmunk is what you replace by cocos2d.

    and then in the application

     private void InitAdView()
        {
            LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayoutAd);
    
            // Create the adView
            adView = new AdView(this, AdSize.BANNER, AppSettings.AdmobAppId);
    
            ...set your add listener
    
            // Add the adView to the layout
            layout.addView(adView);