3

Finally, I have a banner ad at the top of my GLSurfaceview. However, it appears with a black background that takes up the whole width of the screen and covers the top of my game area like so (I should also point out that the play area is also moved downwards a little so the bottom is also missing).

enter image description here

What I need to do is move the banner to the bottom of the screen and, keep it central and remove this black background so it looks like this:

enter image description here

I tried to use XML but was getting a lot of errors so I switched to doing this completely in Java (and managed to get this far) - however quality information on how to do this with GLSurfaceView is lacking IMHO so I'm hoping someone can tell me where I'm going wrong.

Code

Here is my onCreate() method:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Create an ad.
        adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId(AD_UNIT_ID);

        // Add the AdView to the view hierarchy. The view will have no size
        // until the ad is loaded.
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);

        // Create an ad request.
        // get test ads on a physical device.
        AdRequest adRequest = new AdRequest.Builder()
          .addTestDevice(TestDeviceID)
          .build();

        // Start loading the ad in the background.
        adView.loadAd(adRequest);

        //Request full screen
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

        //Create a displayMetrics object to get pixel width and height
        metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        width = metrics.widthPixels;
        height = metrics.heightPixels;

        //Create and set GL view (OpenGL View)
        myView = new MyGLSurfaceView(MainActivity.this);
        layout.addView(adView);
        layout.addView(myView);


        //Create a copy of the Bundle
        if (savedInstanceState != null){
            newBundle = new Bundle(savedInstanceState);         
        }

        //Set main renderer             
        setContentView(layout);

}

When the banner changes, it also appears to 'flicker', but I can deal with that in a separate question.

Fran Marzoa
  • 4,293
  • 1
  • 37
  • 53
Zippy
  • 3,826
  • 5
  • 43
  • 96
  • how about the flickering issue? Did you manage to solve it? – LightYearsBehind Sep 08 '14 at 10:08
  • Hi i@haike00 I can't remember what I meant by flickering, however, I have since changed to using the MobFox SDK and using mediation through MobFox to serve Ads from both MobFox and AdMob. However, some of the banners still randomly flicker (not just when changing though), but I've seen this behaviour on other apps too so I assume it's just something we have to live with....however, this could be a different issue to that which you're talking about. I'm thinking about switching back to the AdMob SDK for my next app...... – Zippy Sep 08 '14 at 15:29
  • I guess we are talking about the same issue, where the banners will flicker at random time making it like a broken TV with bad reception (sort of..). I tried using `FrameLayout insteand of `RelativeLayout which turns out to be better for most of my cases but doesn't solve the problem altogether. Perhaps the native android view does not work well with open gl es... – LightYearsBehind Sep 09 '14 at 00:42

2 Answers2

3

Use a RelativeLayout or a FrameLayout as your parent layout, then just define the layout parameters for the adView to be positioned at the bottom center of the screen like this. Below is a solution:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Create an ad.
        adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId(AD_UNIT_ID);
        adView.setBackgroundColor(Color.TRANSPARENT); 
        // Add the AdView to the view hierarchy. The view will have no size
        // until the ad is loaded.
        RelativeLayout layout = new RelativeLayout(this);
        layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,    LayoutParams.MATCH_PARENT));
        // Create an ad request.
        // get test ads on a physical device.
        AdRequest adRequest = new AdRequest.Builder()
          .addTestDevice(TestDeviceID)
          .build();

        // Start loading the ad in the background.
        adView.loadAd(adRequest);

        //Request full screen
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

        //Create a displayMetrics object to get pixel width and height
        metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        width = metrics.widthPixels;
        height = metrics.heightPixels;

        //Create and set GL view (OpenGL View)
        myView = new MyGLSurfaceView(MainActivity.this);
     RelativeLayout.LayoutParams adParams = 
                new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
            adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);

        layout.addView(myView);
        layout.addView(adView, adParams);


        //Create a copy of the Bundle
        if (savedInstanceState != null){
            newBundle = new Bundle(savedInstanceState);         
        }

        //Set main renderer             
        setContentView(layout);

}
Nana Ghartey
  • 7,901
  • 1
  • 24
  • 26
  • Hi @Nana many thanks for this. I changed my code as above, when I run it the game appears to run without any ads (ie, they are not visible) - I can confirm the ad is ad the bottom of the screen though because if I touch randomly in that general area, the web browser opens and takes me to the GoogleAdServices website :-) (also if I remove layout.addView(myView); then although (obviously) my game doesn't appear, I do get a blank screen with the ad at the bottom! - Any ideas?! Thanks :-) – Zippy Apr 15 '14 at 00:54
  • Did you change the lines layout.addView(myView)..layout.add(adView, adParams) ? In your original post you were adding the adview before adding the glsurfaceview to the layout. You probably maintained that, thereby causing the glsurfaceview to cover the adview. – Nana Ghartey Apr 15 '14 at 01:10
  • 2
    Hi @Nana, yeah I did change them and they are the correct way around. After a bit of poking around I found this: http://www.badlogicgames.com/forum/viewtopic.php?f=15&t=12677, the people talking here have the same problem (although they are using an engine, LIBGDX , which I'm not, but the problem is the same) it appears to be a bug of sorts? Anyhow, I added their suggestion to your code (adView.setBackgroundColor(Color.BLACK);) - Lo and behold, the ad appears. No idea why this is needed though. Many thanks for your help. – Zippy Apr 15 '14 at 01:19
  • Great! I wonder how that fixes the issue. Must be some hack. Just make sure you test on different devices/emulators – Nana Ghartey Apr 15 '14 at 01:24
  • Yeah, it is very strange, I tried with black, white and transparent and they all seem to fix it. Will definitely test on a few devices, don't think I can test on an emulator as I'm using openGL ES 2.0 which I don't think the emulators support. I have some more questions which I'm going to be posting in the next few days regarding adMob ads - the documentation is so poor :-( Cheers! – Zippy Apr 15 '14 at 01:30
  • I have the above code working fine in my game, when using adView.setBackgroundColor(Color.BLACK); Anyway if I click on an ad banner, then hit the back button to return to the game, the glView is just showing black. The app is still running ( I can still navigate through the game and hear the background sounds ), new ads are still loaded, the glview stays black. Is anyone experiencing the same problem ? (The game is running in landscape mode.) – NULL May 07 '14 at 22:37
  • @NULL Does this happen when you remove the adview? Color.TRANSPARENT works too – Nana Ghartey May 08 '14 at 00:23
  • When I comment this line out it works: layout.addView(adView, adParams); I have a facebook button which I can use to be redirected to the game's fb page. Hitting back from the fb page returns me to the app and the gl view is fine. I can see that onPause and onResume are called properly. I can also see that textures are flushed upon onPause and textures are recreated upon onResume(). – NULL May 08 '14 at 08:11
  • @Nana Another strange thing: when I just touch an ad the glview also freezes, onPause is not called. Only when I touch the banner's link button the link is followed and onPause gets called. Might there be something like 'focus change' between the 2 views that freezes the glview when it looses focus? – NULL May 08 '14 at 08:18
  • @NULL I haven't experienced such issues. Check what happens to the glview in the onAdLeftApplication() & onAdOpened() callbacks in the adListener interface. Also, make sure you call adview.pause() and adview.resume() in the game activity's onPause() and onResume() respectively – Nana Ghartey May 08 '14 at 09:59
  • @Nana Everything is working now. I can confirm the above code to work well on 2.3.3, 2.3.4, and above. Thank you for your support! It turned out that I was using glSurfaceView.IsFocused() to check if my engine needs updates. I was relying on onFocusChanged to be called whenever the focus changes but somehow the focus of the glSurfaceView changes when an ad is touched, without onFocusChanged being called. – NULL May 11 '14 at 21:46
0

You have two separate views that are divided when you add them both to a linear layout (surface view and your ad view). I think what you want to accomplish is to overlay the ad on top of your surface view. This page is old but check out http://www.curious-creature.org/2009/03/01/android-layout-tricks-3-optimize-part-1/ for an example on how to do this.

  • Thankyou @RazApps for this, however, I'm trying to do this programmatically, I'm not using XML at all. – Zippy Apr 15 '14 at 00:36
  • Sorry. I wasn't more specific where I should've been. The XML gets converted into programatically by taking all the attributes in the XML and passing it on to the actual Java objects. So you can create your FrameLayout layout with fill parent attributes and then add your surface view to the layout and then create a linear layout with the same layout attributes as TextView in the example (wrap content for layout width and height and gravity). Then add your ad to the layout. and add the LinearLayout to your FrameLayout. –  Apr 15 '14 at 00:42