Question

I am using to add a simple banner to my app (a game written using openGL ES 2.0) - Please note, I am not using any XML, everything is done in code

I've tried to Google this and look around on SO but I can't find any info at all - how do we deal with screen fragmentation when using AdMob?

My App is locked to Landscape mode.

For instance, I run my app on a tablet (with a resolution of 2560 x 1600) and everything looks great:

enter image description here

Then I run it on a old Galaxy Ace phone (with a resolution of 480 x 320) and I get this:

enter image description here

Obviously, this is wrong for a number of reasons (banner too big generally, too close to the controls, extends down to bottom of screen when I really want it only to extend down to the bottom of the GLViewPort (if this is even possible) - basically, every single object in my game is scaled so that it will appear at the same relative size on every screen / DPI as well as retaining the ratio for which it was intended. I've spend a very long time making sure the game looks and feels the same on every device!

As it is, the banners are completely useless on the phone. Am I stuck with this? Is there anything I can do here? Can I make the banner any smaller? Is there any way at all that I can scale it myself? Basically, I guess what I'm asking is do I have any real control over these banners?

I've looked at Google's Official documentation too and I don't find it great. Same goes with AdMob's help.

I'm very new to AdMob and have been trying all day to figure out a way to do this, any help would be appreciated.

Code:

Code

@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.
        RelativeLayout layout = new RelativeLayout(this);
        layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,    LayoutParams.MATCH_PARENT));

        // Create an ad request. Check logcat output for the hashed device ID to
        // 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;

        //Work out values for resizing screen while keeping aspect ratio

        width = (int) Math.min(width, height * 1.702127659574468);
        height = (int) Math.min(height, width / 1.702127659574468);

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

        //Set the colour if we don't, the ad won't show (bug?)
        adView.setBackgroundColor(Color.BLACK);
        layout.addView(myView);
        layout.addView(adView, adParams);

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


        setContentView(layout);

}
Was it helpful?

Solution 3

The size of the banner you are being given is correct for that size and density phone (320*50). You can't get a smaller banner than that. On your tablet you are also asking for and displaying a phone sized banner ad. You would normally display a larger format ad (or use SmartBanner to choose for you) when rendering on a tablet.

You are going to have to think about how you can share the screen between the banner ad and your game UI. It appears that you have centered the GameUI on the phone and have room above and below. It looks like if you aligned the Game UI to the top you would have room for the banner ad.

This would have been a lot easier if you had laid things out using XML.

OTHER TIPS

Zippy, you can dynamically load adsize based on screen resolution:

        AdSize adSize = AdSize.SMART_BANNER;

        DisplayMetrics dm = getResources().getDisplayMetrics();

        double density = dm.density * 160;
        double x = Math.pow(dm.widthPixels / density, 2);
        double y = Math.pow(dm.heightPixels / density, 2);
        double screenInches = Math.sqrt(x + y);

        if (screenInches > 8) { // > 728 X 90
            adSize = AdSize.LEADERBOARD;
        } else if (screenInches > 6) { // > 468 X 60
            adSize = AdSize.MEDIUM_RECTANGLE;
        } else { // > 320 X 50
            adSize = AdSize.BANNER;
        }



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

For other banner sizes, check out the banners' official page

Ah! I am late in the party. This is what I did to solve the same problem. I hope it will help future readers:

Use Smart banner as it automatically decides the height of ad based on device size. Use full width of screen to show the ad.

From android developers site:

Smart Banners are ad units that will render screen-wide banner ads on any screen size across different devices in either orientation. Smart Banners help deal with increasing screen fragmentation across different devices by "smartly" detecting the width of the phone in its current orientation, and making the ad view that size.

Three ad heights (in dp, density-independent pixel) are available:

32 - used when the screen height of a device is less than 400
50 - used when the screen height of a device is between 400 and 720
90 - used when the screen height of a device is greater than 720

Now, get the height of AdView and adjust the margin of the layout where you wish to place the banner ad.

public static int getAdViewHeightInDP(Activity activity) {
        int adHeight = 0;
        
        int screenHeightInDP = getScreenHeightInDP(activity);
        if (screenHeightInDP < 400)
            adHeight = 32;
        else if (screenHeightInDP >= 400 && screenHeightInDP <= 720)
            adHeight = 50;
        else
            adHeight = 90;
        
        return adHeight;
    }

public static int getScreenHeightInDP(Activity activity) {
        DisplayMetrics displayMetrics = ((Context) activity).getResources().getDisplayMetrics();
        
        float screenHeightInDP = displayMetrics.heightPixels / displayMetrics.density;

        return Math.round(screenHeightInDP);
    }

I just wanted to add that you could use native ads instead, and solve it on your own, by your own rules.

You do this by creating your own layout and fill the data for each of the Views you've added.

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