Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 964
lacas Avatar asked Jan 29 '26 17:01

lacas


2 Answers

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);
like image 198
candiru Avatar answered Jan 31 '26 06:01

candiru


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);
like image 23
lacas Avatar answered Jan 31 '26 06:01

lacas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!