Banner ads occupy a spot within an app's layout, either at the top or bottom of the device screen. 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. Case study.
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.
Prerequisites
- Complete the Get started guide.
Add AdView to the layout
The first step toward displaying a banner is to place
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 activity's
AdView
:
# main_activity.xml
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>
Note the following required attributes:
ads:adSize
: Set this to the ad size you'd like to use. If you don't want to use the standard size defined by the constant, you can set a custom size instead. See the banner size section for details.ads:adUnitId
: Set this to the unique identifier given to the ad unit in your app where ads are to be displayed. If you show banner ads in different activities, each would require an ad unit.
You can alternatively create AdView
programmatically:
Java
AdView adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
// TODO: Add adView to your view hierarchy.
Kotlin
val adView = AdView(this)
adView.adSize = AdSize.BANNER
adView.adUnitId = "ca-app-pub-3940256099942544/6300978111"
// TODO: Add adView to your view hierarchy.
Always test with test ads
When building and testing your apps, make sure you use test ads rather than live, production ads. Failure to do so can lead to suspension of your account.
The easiest way to load test ads is to use our dedicated test ad unit ID for Android banners:
ca-app-pub-3940256099942544/6300978111
It's been specially configured to return test ads for every request, and you can use it in your own apps while coding, testing, and debugging. Just make sure you replace it with your own ad unit ID before publishing your app.
For more information about how the Mobile Ads SDK's test ads work, see Test Ads.
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
:
MainActivity (excerpt)
Java
package ...
import ...
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
public class MainActivity extends AppCompatActivity {
private AdView mAdView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
}
Kotlin
package ...
import ...
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.AdView
class MainActivity : AppCompatActivity() {
lateinit var mAdView : AdView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
MobileAds.initialize(this) {}
mAdView = findViewById(R.id.adView)
val adRequest = AdRequest.Builder().build()
mAdView.loadAd(adRequest)
}
}
If your ad fails to load, you don't need to explicitly request another one as long as you've configured your ad unit to refresh; the Google Mobile Ads SDK respects any refresh rate you specified in the AdMob web interface. If you haven't enabled refresh, you will need to issue a new request.
That's it! Your app is now ready to display banner ads.
Ad events
To further customize the behavior of your ad, you can hook onto a number of
events in the ad's lifecycle: loading, opening, closing, and so on. You can
listen for these events through the
AdListener
class.
To use an
AdListener
with
AdView
,
call the
setAdListener()
method:
Java
mAdView.setAdListener(new AdListener() {
@Override
public void onAdClicked() {
// Code to be executed when the user clicks on an ad.
}
@Override
public void onAdClosed() {
// Code to be executed when the user is about to return
// to the app after tapping on an ad.
}
@Override
public void onAdFailedToLoad(LoadAdError adError) {
// Code to be executed when an ad request fails.
}
@Override
public void onAdImpression() {
// Code to be executed when an impression is recorded
// for an ad.
}
@Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
}
@Override
public void onAdOpened() {
// Code to be executed when an ad opens an overlay that
// covers the screen.
}
});
Kotlin
mAdView.adListener = object: AdListener() {
override fun onAdClicked() {
// Code to be executed when the user clicks on an ad.
}
override fun onAdClosed() {
// Code to be executed when the user is about to return
// to the app after tapping on an ad.
}
override fun onAdFailedToLoad(adError : LoadAdError) {
// Code to be executed when an ad request fails.
}
override fun onAdImpression() {
// Code to be executed when an impression is recorded
// for an ad.
}
override fun onAdLoaded() {
// Code to be executed when an ad finishes loading.
}
override fun onAdOpened() {
// Code to be executed when an ad opens an overlay that
// covers the screen.
}
}
Each of the overridable methods in
AdListener
corresponds to an event in the lifecycle of an ad.
Overridable methods | |
---|---|
onAdClicked() |
The onAdClicked()
method is invoked when a click is recorded for an ad.
|
onAdClosed() |
The onAdClosed()
method is invoked when a user returns to the app after viewing an ad's
destination URL. Your app can use it to resume suspended activities or
perform any other work necessary to make itself ready for interaction.
Refer to the AdMob
AdListener example for an implementation of the ad listener methods in the
Android API Demo app.
|
onAdFailedToLoad() |
The onAdFailedToLoad()
method is the only one that includes a parameter. The error parameter of type
LoadAdError describes what error occurred. For more information,
refer to the Debugging Ad Load Errors
documentation.
|
onAdImpression() |
The onAdImpression()
method is invoked when an impression is recorded for an ad.
|
onAdLoaded() |
The onAdLoaded()
method is executed when an ad has finished loading. If you want to delay
adding the AdView
to your activity or fragment until you're sure an ad will be loaded, for
example, you can do so here.
|
onAdOpened() |
The onAdOpened()
method is invoked when an ad opens an overlay that covers the screen.
|
Banner sizes
The size of the container in which you place your ad must be at least as big as the banner. If your container has padding, that effectively decreases the size of your container. In the event that the container cannot fit the banner ad, the banner will not appear, and you will get this warning in the logs:
W/Ads: Not enough space to show ad. Needs 320x50 dp, but only has 288x495 dp.
The table below lists the standard banner sizes.
Size in dp (WxH) | Description | Availability | AdSize constant |
---|---|---|---|
320x50 | 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 |
Provided width x Adaptive height | Adaptive banner | Phones and tablets | N/A |
Screen width x 32|50|90 | Smart banner | Phones and tablets | SMART_BANNER |
To define a custom banner size, set your desired
AdSize
as shown:
Java
AdSize adSize = new AdSize(300, 50);
Kotlin
val adSize = AdSize(300, 50)
Hardware acceleration for video ads
In order for video ads to show successfully in your banner ad views, hardware acceleration must be enabled.
Hardware acceleration is enabled by default, but some apps may choose to disable
it. If this applies to your app, we recommend enabling hardware acceleration for
Activity
classes that use ads.
Enabling hardware acceleration
If your app does not behave properly with hardware acceleration turned on
globally, you can control it for individual activities as well. To enable or
disable hardware acceleration, you can use the android:hardwareAccelerated
attribute for the
<application>
and
<activity>
elements in your AndroidManifest.xml
. The following example enables hardware
acceleration for the entire app but disables it for one activity:
<application android:hardwareAccelerated="true">
<!-- For activities that use ads, hardwareAcceleration should be true. -->
<activity android:hardwareAccelerated="true" />
<!-- For activities that don't use ads, hardwareAcceleration can be false. -->
<activity android:hardwareAccelerated="false" />
</application>
See the Hardware acceleration guide for more information about options for controlling hardware acceleration. Note that individual ad views cannot be enabled for hardware acceleration if the activity is disabled, so the activity itself must have hardware acceleration enabled.
अन्य संसाधन
GitHub पर उदाहरण
बैनर RecyclerView ऐप्लिकेशन का नमूना: Java
मोबाइल विज्ञापन गैराज के वीडियो ट्यूटोरियल
सफलता की कहानियां
अगले चरण
इन विषयों को एक्सप्लोर करें: