Question

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.

Was it helpful?

Solution

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);

}

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top