Show: Delphi C++
Display Preferences

Using the AdMob Service

From RAD Studio
Jump to: navigation, search

Go Up to Creating an Android App


Advertising services enable you to display ads in your applications to gain some income. AdMob is the Google advertising service for Android. This topic describes how to place ads from AdMob on your applications using FireMonkey.

Note: The following information complements the main documentation topic about Android development. Read Android Mobile Application Development before you continue.

Supported Devices

FireMonkey supports AdMob on devices running Android 2.3 or later. See also FireMonkey Platform Prerequisites, Android Requirements.

Prerequisites

Before you can use the AdMob service, you must prepare your development environment and register an AdMob account.

Creating an Ad Unit for Your Application in AdMob

To be able to configure TBannerAd to connect to the AdMob service, first you must create an ad unit in AdMob. When you configure your instance of TBannerAd to connect to AdMob, you must copy the ID of your ad unit into the AdUnitID property.

Adding AdMob Support to Your Mobile Apps

Here is the general procedure for adding ads to your multi-device applications on mobile platforms:

  1. Add an instance of the TBannerAd component to your application.
  2. Configure TBannerAd to load an ad from AdMob.
  3. Handle how your user interacts with this ad.

Loading an Ad

To display an ad from AdMob in your application, you must configure TBannerAd to connect to AdMob and load an ad from your service.

Configuring the Connection Data for AdMob

To connect to the AdMob service on Android:

1. Create your ad unit ID.
2. Copy your ad unit ID into the AdUnitID property of TBannerAd on an event such as the OnCreate event of your form:
Delphi:  BannerAd1.AdUnitID := 'YourAdUnitID';
C++:  BannerAd1->AdUnitID = "YourAdUnitID";
3. Go to Project > Options > Entitlement List, and select AdMob Service. Set its value to True.

Loading an Ad from AdMob

To load an ad from AdMob, call LoadAd. The LoadAd procedure is asynchronous, and when it finishes, it triggers one of the following events:

  • OnDidFail is triggered if there is an error. OnDidFail provides a parameter, Error, that provides detailed information about the error that prevented the banner from loading an ad.
  • OnDidLoad is triggered if the request is successful.

You usually want to call LoadAd from a form event such as OnShow, to load an ad as soon as the form is displayed.

Handling User Interaction with Ads

When your user taps an ad, this triggers an action. This action typically consists of a full-screen version of the ad, or of opening a URL on a Web browser.

Between your user tapping the ad and the triggered action, TBannerAd triggers its OnActionCanBegin event. Handle this event to decide whether or not you want to let the action happen.

You might also want to use your handler for the OnActionCanBegin event to pause some running features of your applications, to avoid wasting processing during the ad action. Then you can define an event handler for the OnActionDidFinish event where you resume these features of your application.

Testing Your Ads

To show test ads in Android, simply set the TBannerAd.TestMode property to True.

Troubleshooting Loading an Ad from AdMob

The following table lists some problems that prevent your application from showing an ad, and potential solutions:

Cause Description Recommendation

Network error

No network connectivity to the ad server

Verify your Wi-Fi or data connection

Internal server error

An invalid response was received from the ad server

--

Invalid ad request

Wrong AdUnitID or no AdUnitID used

Use a valid unit ID (AdUnitID)

Lack of ad inventory

No ads are currently available to download from the ad server

--

Wrong ad properties Visible property of TBannerAd is set to False Set TBannerAd visible property to True
Fixed-size of the banner is too big for the screen resolution Set ad size property to a proper value

Wrong application configuration

Ad service configuration parameters of your application are wrong

Set the AdMob Service property of your application (see step 3)

See Also

Personal tools
RAD Studio 10.2 Tokyo
In other languages
Previous Versions
Assistance