Banner Ads

Banner ads are rectangular image or text ads that occupy a spot within an app's layout. They stay on screen while users are interacting with the app, and can refresh automatically after a certain period of time. If you're new to mobile advertising, they're a great place to start.

This guide shows you how to integrate banner ads from AdMob into an Android app. In addition to code snippets and instructions, it also includes information about sizing banners properly and links to additional resources.


Add an AdView to the layout

The first step toward displaying a banner is to place an AdView in the layout for the Activity or Fragment in which you'd like to display it. The easiest way to do this is to add one to the corresponding XML layout file. Here's an example that shows an AdView at the bottom of an Activity:


<RelativeLayout xmlns:android=""

        <TextView android:text="@string/hello_world"
            android:layout_height="wrap_content" />



The AdView XML tag needs the following attributes:

  • ads:adSize - Set this to the AdMob ad size you'd like to use (see the banner size section below for details).
  • ads:adUnitId - Set this to the AdMob ad unit ID from which the AdView should load ads.

Ad unit IDs are created in the AdMob UI and represent a place in your app where ads appear. If you show banner ads in two Activities, for example, you should create an ad unit for each one.

You can alternatively create an AdView programmatically:

AdView adView = new AdView(this);
// TODO: Add the adView to your view hierarchy.

Always test with test ads

The sample code above contains an ad unit ID and you're free to request ads with it. It's been specially configured to return test ads rather than production ads for every request, which makes it safe to use.

However, once you register an app in the AdMob UI and create your own ad unit IDs for use in your app, you'll need to explicitly configure your device as a test device when you're developing. This is extremely important. Testing with real ads (even if you never tap on them) is against AdMob policy and can cause your account to be suspended. See Test Ads for information on how you can make sure you always get test ads when developing.

Load an ad

Once the AdView is in place, the next step is to load an ad. That's done with the loadAd() method in the AdView class. It takes an AdRequest parameter, which holds runtime information (such as targeting info) about a single ad request.

Here's an example that shows how to load an ad in the onCreate() method of an Activity: (excerpt)

package ...

import ...


public class MainActivity extends AppCompatActivity {
    private AdView mAdView;

    protected void onCreate(Bundle savedInstanceState) {
        mAdView = (AdView) findViewById(;
        AdRequest adRequest = new AdRequest.Builder().build();

That's it! Your app is now ready to display banner ads from AdMob.

The following banner sizes are supported:

Size (WxH) Description Availability AdSize constant
320x50 Standard Banner Phones and Tablets BANNER
320x100 Large Banner Phones and Tablets LARGE_BANNER
300x250 IAB Medium Rectangle Phones and Tablets MEDIUM_RECTANGLE
468x60 IAB Full-Size Banner Tablets FULL_BANNER
728x90 IAB Leaderboard Tablets LEADERBOARD
Screen width x 32|50|90 Smart Banner Phones and Tablets SMART_BANNER

Smart Banners

Smart Banners are ad units that render screen-width 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 device in its current orientation and making the ad view that size.

Three ad heights are implemented in smart banners:

Ad height Screen height
32dp ≤ 400dp
50dp > 400dp and ≤ 720dp
90dp > 720dp

Typically, Smart Banners on phones have a height of 50dp in portrait and 32dp in landscape. On tablets, height is normally 90dp in both orientations.

When an image ad isn't large enough to take up the entire allotted space, the image will be centered, and the space on either side will be filled in.

To use Smart Banners in XML, specify the constant SMART_BANNER for the ad size and set the width of the AdView to match_parent. For example:


To create a Smart Banner programmatically, use AdSize.SMART_BANNER as the ad size:

AdView adView = new AdView(this);

Additional resources


Mobile Ads Garage video tutorials

Next steps

Send feedback about...

AdMob for Android
AdMob for Android
Need help? Visit our support page.