changing admob banner size dynamically
Asked Answered
M

2

4

Here is the thing, as you might know Admob has a AdSize.* function, where u put Banner to show banner ads, and AD_banner` for tablet banners, what i want to do is take a screen size of a device so that i could throw it in my if statement and then put the right banner for right device, i hope i was clear enough.So anyone can tell me how can i get the screen size of device? Thank u ////////// Here's what i have done so far

     DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

       if (dm.density==DisplayMetrics.DENSITY_HIGH) {
        AdView adView = new AdView(this, AdSize.BANNER,s);
        LinearLayout layout = (LinearLayout)findViewById(R.id.admob);
        layout.addView(adView);
        adView.loadAd(new AdRequest());
       }
       if (dm.density==DisplayMetrics.DENSITY_DEFAULT || dm.density==DisplayMetrics.DENSITY_LOW ) {
             AdView adView = new AdView(this, AdSize.BANNER,s);
             LinearLayout layout = (LinearLayout)findViewById(R.id.admob);
             layout.addView(adView);
            adView.loadAd(new AdRequest());

        }
        if(dm.density==DisplayMetrics.DENSITY_MEDIUM)
        {
            AdView adView = new AdView(this, AdSize.IAB_BANNER,s);
            LinearLayout layout = (LinearLayout)findViewById(R.id.admob);
            layout.addView(adView);
            adView.loadAd(new AdRequest());

        }
Movable answered 5/8, 2011 at 7:49 Comment(0)
S
7

If your layout is define in a xml, you can create one layout per screen size (layout-xlarge/mylayout.xml, layout-large/mylayout.xml, layout-normal/mylayout.xml, etc...)

More info here : http://developer.android.com/guide/practices/screens_support.html

Don't look at density, because, a 10.1" tablet has a medium density, but a 4.3" phone with a 480x850 resolution will have a high density. Use screen size instead (xlarge large normal small).

If you need to do it programatically, you can get the screen size with this :

Configuration config = activity.getResources().getConfiguration();
int screenlayout = config.screenLayout;

and to compare, use Configuration.SCREENLAYOUT_xxx .

Sapanwood answered 5/8, 2011 at 8:54 Comment(0)
A
13

The most recent AdMob SDK includes AdSize.SMART_BANNER which enables the best size matched ad for the device.

I was using an older version myself and didn't knew about this feature :D

Ataractic answered 5/11, 2012 at 11:48 Comment(3)
But note that you can't currently use smart banners with AdMob Network Mediation, as you can read in documentation.Actinon
I would strongly suggest avoiding smart banners for this very reason; if you later decide to mediate using an ad network that doesn't support them, your only choice would be to upgrade the app code; and you can't guarantee that all users will be using the upgraded app, so you might break your ads.Seow
Also, smart banners are not no smart, they cannot handle device rotations, meaning that if the app is opened in landscape, after switching to portrait the ad will disappear because it doesn't fit.Ziska
S
7

If your layout is define in a xml, you can create one layout per screen size (layout-xlarge/mylayout.xml, layout-large/mylayout.xml, layout-normal/mylayout.xml, etc...)

More info here : http://developer.android.com/guide/practices/screens_support.html

Don't look at density, because, a 10.1" tablet has a medium density, but a 4.3" phone with a 480x850 resolution will have a high density. Use screen size instead (xlarge large normal small).

If you need to do it programatically, you can get the screen size with this :

Configuration config = activity.getResources().getConfiguration();
int screenlayout = config.screenLayout;

and to compare, use Configuration.SCREENLAYOUT_xxx .

Sapanwood answered 5/8, 2011 at 8:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.