In one of my projects I needed to integrate AdMob into an GLSurfaceView. This is how I made it:
1. Get the
GoogleAdMobAds.jar from Google itself:
http://code.google.com/mobile/ads/ and add it to your buildpath.
2. Add a new xml file to your layout folder, let's say "main.xml": This file needs to contain an
android.opengl.GLSurfaceView and a
com.google.ads.AdView
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android">
http://schemas.android.com/apk/res/android</a>"
xmlns:ads="<a href="http://schemas.android.com/apk/lib/com.google.ads">
http://schemas.android.com/apk/lib/com.google.ads</a>"
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.google.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="@string/AD_MOB_PUBLISHER_ID" />
</FrameLayout>
3. Add following code to your activity:
private GLSurfaceView surface;
private GlRenderer renderer; // your renderer implementation
private AdView adView;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView (R.layout.main);
surface = (GLSurfaceView) this.findViewById (R.id.glSurface);
renderer = new GlRenderer(this); // your renderer implementation
surface.setRenderer(renderer);
AdRequest adRequest = new AdRequest();
// adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
adView = (AdView) this.findViewById(R.id.adView);
adView.loadAd(adRequest);
}
4. Furthermore you need to define the AdActivity in your AndroidManifest.xml
<activity
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|
screenSize|smallestScreenSize"
android:name="com.google.ads.AdActivity" />
Note: Make sure you're compiling against at least Android v3.2 (set
target in
default.properties to
android-13). Otherwise it won't work!
5. AdMob requires following permissions to work properly. Add them to your AndroidManfiest.xml too.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Let me know if it worked in your case!