lacas
lacas

Reputation: 14066

Android add advert (admob) to OPENGL-ES screen

Is there a way to do this? I am new to opengl-es. I cant find any information how to add an advert to a simple glsurfaceview.

Upvotes: 2

Views: 6465

Answers (3)

lacas
lacas

Reputation: 14066

Thank you, my solution was:

init.xml layout

<FrameLayout xmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:myapp = "http://schemas.android.com/apk/res/com.lacroix.start"
android:orientation = "vertical" android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
>

    <android.opengl.GLSurfaceView android:id="@+id/glSurface" android:layout_width="fill_parent" 
    android:layout_height="fill_parent" android:layout_gravity="top|left"/>

    <com.admob.android.ads.AdView android:id = "@+id/ad" android:layout_width = "fill_parent" 
    android:layout_height = "wrap_content" android:layout_alignParentRight="true" 
    myapp:backgroundColor = "#000000" myapp:primaryTextColor = "#FFFFFF" 
    myapp:secondaryTextColor = "#CCCCCC" myapp:keywords = "Android game" />


</FrameLayout>

then on Start.java activity onCreate

  setContentView (R.layout.init);

    adview = (AdView) this.findViewById(R.id.ad);
    adview.setVisibility(AdView.VISIBLE);
    adview.requestFreshAd (); 

    view            = (GLSurfaceView) this.findViewById (R.id.glSurface); 
    renderer        = new OpenGLRenderer(this);
    view.setRenderer(renderer);

Upvotes: 4

candiru
candiru

Reputation: 4542

You won't need to draw ads yourself on your OpenGL surface at all, just set some layout to your activity as the content view, and add the ad and the surface to the layout.

You can use a FrameLayout if you don't mind that the ad will cover your surface, or a RelativeLayout if you need a dedicated area, say, on the top, but make sure you put some nice banner below the ads to avoid showing a black box in case there is no network connection.

We use Admob via Adwhirl, so in our case it looks something like the following. I never used Admob directly, but I guess it should be something really similar.

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_main"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
</FrameLayout>

YourActivity.java:

FrameLayout layout = (FrameLayout) findViewById(R.id.layout_main);

GLSurfaceView surface = ...
FrameLayout.LayoutParams surfaceParams = new FrameLayout.LayoutParams(...);
layout.addView(surface, surfaceParams);

AWhirlLayout adWhirlLayout = ...
FrameLayout.LayoutParams adsParams = new FrameLayout.LayoutParams(...);
mainLayout.addView(adWhirlLayout, adsParams);

Upvotes: 5

Drakk Lord
Drakk Lord

Reputation: 96

Hey! Well I'm not familiar with admob, but I guess you can do this by rendering the advert into an image and add that image to OpenGL and render that image with OpenGL. Since adding images to OpenGL is a heavy task I don't recommend doing this every frame, only if you're sure It wont cause any problems. Or if you only need the advert to be on top of an OpenGL surface you can simply put the advert's view over the OpenGL view and it should render correctly.

Upvotes: 0

Related Questions