Android Advertising Library

An unified advertising API that supports Google AdMob and Samsung AdHub.

Features

  • Use the same API to display ads from both Google AdMob and Samsung AdHub
  • Ad targeting is supported for both Google and Samsung
  • Easily switch ads between Google and Samsung
  • Simple to use for common use cases

Quick Start

First you must have signed up an account from Google or Samsung. To display ads from Google, you must also install Google Play services. Or if you want to display ads from Samsung, you must also download Samsung AdHub SDK. Then you must import Android Advertising Library to your workspace as a library project.

The Layout

To show ads from Google or Samsung, you need to add a <com.google.android.gms.ads.AdView> element to your layout XML as illustrated in the sample project below.

Add xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads", and place your ad to an appropriate location:

<com.google.android.gms.ads.AdView
    android:id="@id/adview_google"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="invisible"
    ads:adUnitId="GOOGLE_AD_UNIT_ID"
    ads:adSize="SMART_BANNER" />

Note: You must replace GOOGLE_AD_UNIT_ID by your Google AdMob Ad ID.

We use SMART_BANNER here to let Google server choose the best-fit size for the ad. You may use other values if you want.

To show ads from Samsung, you need to add a <com.sec.android.ad.AdHubView> element to your layout XML as illustrated in the sample project below.

Place your ad to an appropriate location:

<com.sec.android.ad.AdHubView
    android:id="@id/adview_samsung"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center" />

How to show ads

First create an instance of AdService:

this.adService = new AdService(context);

Set your ad ID:

adService.setAdId("YOUR_AD_ID");

Attach a listener to handle various events:

adService.setAdListener(new AdService.AdListener() { ... });

You are ready to display Google ads:

// Displays an ad from Google AdMob.
adService.displayAd((AdView)findViewById(R.id.adview_google));

... or Samsung ads:

// Displays an ad from Samsung AdHub.
adService.displayAd((AdHubView)findViewById(R.id.adview_samsung), AdService.getBestFitAdSize(activity));

Ad targeting

Ad targeting is supported by calling the following methods before requesting to show ads:

  • setAge
  • setGender
  • addKeywords

Android Advertising Library provides an unified set of APIs for ad targeting APIs that supports both Google and Samsung.

AdService events

Android Advertising Library provides 2 methods to handle ad loading events for both Google and Samsung:

  • onAdLoaded
  • onAdFailedToLoad

Switch between Google and Samsung ads

By hosting both Google AdView and Samsung AdHubView on your activity, you can load both ads and control which one to display.

Caution

Samsung does not allow apps to show ads from a non-Samsung source. You may, however, load ads from Samsung first, and if it fails (as this is often the case), load from Google instead in onAdFailedToLoad callback method.

Sample Project

A sample project is provided to demonstrate all the features.

http://github.com/ayltai/Android-Lib-Advertising/Sample

Built With

Share this project:

Updates