Implementing Banner Mobile Ads Garage
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.
Prerequisites
Import the Google Mobile Ads SDK, either by itself or as part of Firebase.
Add AdView to the layout
The first step toward displaying a banner is to place AdView in the layout for the Activityor 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 AdView at the bottom of an Activity:
main_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_height="match_parent" android:layout_width="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <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> </RelativeLayout>
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 below 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:
val adView = AdView(this) adView.adSize = AdSize.BANNER adView.adUnitId = "ca-app-pub-3940256099942544/6300978111" // TODO: Add adView to your view hierarchy.
AdView adView = new AdView(this); adView.setAdSize(AdSize.BANNER); adView.setAdUnitId("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're free to 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 anAdRequest 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 anActivity:
MainActivity (excerpt)
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, "ca-app-pub-3940256099942544~3347511713"); mAdView = findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder().build(); mAdView.loadAd(adRequest); } }
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, "ca-app-pub-3940256099942544~3347511713") mAdView = findViewById(R.id.adView) val adRequest = AdRequest.Builder().build() mAdView.loadAd(adRequest) } }
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.
mAdView.setAdListener(new AdListener() { @Override public void onAdLoaded() { // Code to be executed when an ad finishes loading. } @Override public void onAdFailedToLoad(int errorCode) { // Code to be executed when an ad request fails. } @Override public void onAdOpened() { // Code to be executed when an ad opens an overlay that // covers the screen. } @Override public void onAdLeftApplication() { // Code to be executed when the user has left the app. } @Override public void onAdClosed() { // Code to be executed when when the user is about to return // to the app after tapping on an ad. } });
mAdView.adListener = object: AdListener() { override fun onAdLoaded() { // Code to be executed when an ad finishes loading. } override fun onAdFailedToLoad(errorCode : Int) { // Code to be executed when an ad request fails. } override fun onAdOpened() { // Code to be executed when an ad opens an overlay that // covers the screen. } override fun onAdLeftApplication() { // Code to be executed when the user has left the app. } override fun onAdClosed() { // Code to be executed when when the user is about to return // to the app after tapping on an ad. } }
Each of the overridable methods in AdListener corresponds to an event in the lifecycle of an ad.
Overridable methods | |
---|---|
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. |
onAdFailedToLoad() | The onAdFailedToLoad() method is the only one that includes a parameter. The errorCode parameter indicates what type of failure occurred. The possible values are defined as constants in theAdRequest class:
|
onAdOpened() | This method is invoked when the user taps on an ad. |
onAdLeftApplication() | This method is invoked afteronAdOpened() , when a user click opens another app (such as the Google Play), backgrounding the current app. |
onAdClosed() | When a user returns to the app after viewing an ad's destination URL, this method is invoked. Your app can use it to resume suspended activities or perform any other work necessary to make itself ready for interaction. See the AdMob AdListener example for an implementation of the ad listener methods in the Android API Demo app. |
Banner sizes
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 |
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 here:
AdSize adSize = new AdSize(300, 50);
val adSize = AdSize(300, 50)
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 |
---|---|
32 dp | ≤ 400 dp |
50 dp | > 400 dp and ≤ 720 dp |
90 dp | > 720 dp |
Typically, Smart Banners on phones have a height of 50 dp in portrait and 32 dp in landscape. On tablets, height is normally 90 dp 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:
<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" ads:adSize="SMART_BANNER" ads:adUnitId="ca-app-pub-3940256099942544/6300978111"> </com.google.android.gms.ads.AdView>
To create a Smart Banner programmatically, use AdSize. SMART_BANNER as the ad size:
AdView adView = new AdView(this); adView.setAdSize(AdSize.SMART_BANNER);
val adView = AdView(this) adView.adSize = AdSize.SMART_BANNER
Additional resources
Samples on GitHub
Mobile Ads Garage video tutorials
Next steps
- If you haven't already, create your own app and banner ad unit in the AdMob UI and use your newly created app ID and ad unit ID in your code. Remember to configure your device with test ads.
- Learn about ad targeting and banner ad guidance.
- Try another ad format:
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 3.0 License, and code samples are licensed under the Apache 2.0 License. For details, see our Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Terakhir diperbarui September 7, 2018.
Tidak ada komentar