GLSurfaceView glView; protected AdView adView; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); RelativeLayout layout = new RelativeLayout(this); glView = new GLSurfaceView(this); // Create AdMob LayOut adView = new AdView(this, AdSize.BANNER, "XXXXXXX"); // Your AdMob ID AdRequest request = new AdRequest(); request.addTestDevice(AdRequest.TEST_EMULATOR); request.addTestDevice("D77E32324019F80A2CECEAAAAAAAAA"); // My test phone adView.loadAd(request); layout.addView(glView); RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); adParams.addRule(RelativeLayout.CENTER_IN_PARENT); layout.addView(adView, adParams); setContentView(layout); }
This code make it very easy to place the ad where you want it. It's not pixel but you can place it kinda good around the phone. You change it with adParams.addRule. If you don't have a test device you can just remove request.addTestDevice.
If you want to turn on and off the add you need to add a Handler in the same class as you have create.
private final int SHOW_ADS = 1; private final int HIDE_ADS = 0; protected Handler handler = new Handler() { @Override public void handleMessage(Message msg) { switch(msg.what) { case SHOW_ADS: { adView.setVisibility(View.VISIBLE); break; } case HIDE_ADS: { adView.setVisibility(View.GONE); break; } } } }; public void showAds(boolean show) { handler.sendEmptyMessage(show ? SHOW_ADS : HIDE_ADS); }
This is the last code. Then you can just call showAds(true). I think this work very well.
Hope this is helpful for someone.