Android SDK Usage
Important: When building and testing your apps, make sure you use test ads rather than production ads. Failure to do so can lead to suspension of your account. Please see the Optional Parameters section for more info on toggling the test ads.
Banner Ads
Once the initialization of the SDK is complete, you can get and load banner ads from the EMAManager instance by using the following method:
EMAManager.getInstance().loadBannerAd(this, zoneId, bannerContainer)
this: Activity or Fragment
zoneId: Banner id that will be loaded in the view.
bannerContainer: ViewGroup container that banner ad is loaded.
Note: You should callloadBannerAd
method in the onCreate().
You can set a manager's AdStatusListener
. An AdStatusListener
is used for getting callbacks during the ads' lifecycles. You can send as a fourth parameter for the listener.Depending on the callbacks, you may either display the ad to the user or skip it entirely.
- Java
- Kotlin
EMAManager.getInstance().loadBannerAd(this, zoneId, bannerContainer, this)// AdStatusListener
If you implement AdStatusListener, Once the EMAManager.getInstance().loadBannerAd
method is called, one of these below three will happen, otherwise SDK will manage the lifecycle of bannerAd.
bannerStatusChanged
will be called withstatus
of bannerManager set toAdStatus.READY
.bannerStatusChanged
will be called withstatus
of bannerManager set toAdStatus.FAILED
.- If ads are disabled or the user in an unfinished ad session, then
bannerManager.loadBannerAd
call will be ignored and nothing will happen.
Note: In the full example code you will see sample zoneId 123456. You will be given your actual zoneIds by us, and you will have to replace these sample ids with actual zone ids.
Full Example
- Java
- Kotlin
public class MyActivity extends AppCompatActivity {private RelativeLayout relativeLayoutForBanner;@Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_sample);relativeLayoutForBanner = findViewById(relativeLayoutForBanner);EMAManager.getInstance().loadBannerAd(this,123456, relativeLayoutForBanner);}
Native Ads
Once the initialization of the SDK is complete, you can get and load native ads from the EMAManager instance by using the following method:
EMAManager.getInstance().loadNativeAd(this, zoneId, emaNativeDisplayAdapter)
this: Activity or Fragment
zoneId: Native ad id that will be loaded.
EMANativeAdDisplayAdapter: Adapter that takes viewGroup
as a parameter that native ad will be loaded. Responsible for loading ad that you configure in displayUnifiedAd
method and adding to container.
Note: You should callloadNativeAd
method in the onCreate().
If you implement AdStatusListener, Once the EMAManager.getInstance().loadNativeAd
method is called, one of these below three will happen, otherwise SDK will manage the lifecycle of bannerAd.
nativeAdStatusChanged
will be called withstatus
of bannerManager set toAdStatus.READY
.nativeAdStatusChanged
will be called withstatus
of bannerManager set toAdStatus.FAILED
.
Note: In the full example code you will see sample zoneId 123456. You will be given your actual zoneIds by us, and you will have to replace these sample ids with actual zone ids.
Full Example
- Java
- Kotlin
public class MyActivity extends AppCompatActivity {private RelativeLayout relativeLayoutForBanner;@Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_sample);relativeLayoutForNativeAd = findViewById(relativeLayoutForBanner);EMAManager.getInstance().loadNativeAd(this, 12345, relativeLayoutForNativeAd, new EMANativeAdDisplayAdapter(relativeLayoutForNativeAd) {@NotNull @Overridepublic UnifiedNativeAdView displayUnifiedAd(@NotNull UnifiedNativeAd unifiedNativeAd) {LayoutInflater inflater = LayoutInflater.from(getApplicationContext());UnifiedNativeAdView adView = ((UnifiedNativeAdView)inflater.inflate(R.layout.layout_native_banner, native_ad_view, false);TextView headlineView = adView.findViewById<TextView>(R.id.primary)TextView secondary = adView.findViewById<TextView>(R.id.primary)headlineView.setText( unifiedNativeAd.getHeadline()))secondary.setText(unifiedNativeAd.getBody())adView.headlineView = headlineView;return adView;}});}
Sticky Banner Ads
Sticky banner ads are usually displayed at the bottom of the screen or above the tab bar. These banner ads are always visible to the user.
- Java
- Kotlin
Full Example
public class MyActivity extends AppCompatActivity {RelativeLayout relativeLayoutForStickyBanner;@Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_sample);relativeLayoutForStickyBanner = findViewById(R.id.relativeLayoutStickyBanner)EMAManager.instance.loadBannerAd(this,123456, relativeLayoutForStickyBanner)}}
By setting StickyBannerManager
's listener
you can listen to the status events of sticky banner ads. These events are:
AdStatus.READY
sticky banner is ready for display.AdStatus.FAILED
sticky banner is failed to load an ad.AdStatus.PRESENT
sticky banner is recorded as impression.AdStatus.WILL_LEAVE
sticky banner is redirecting user to a URL.
Interstitial Ad
Interstitial ads are full-screen ads that cover the interface of their host app.
Full Example
- Java
- Kotlin
public class MyActivity extends AppCompatActivity {@Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_sample);InterstitialDisplayManager.getInstance().showInterstitial();}}
By setting InterstitialDisplayManager
's listener
you can listen to the status events of interstitial ads. These events are:
AdStatus.READY
: interstitial is ready for display.AdStatus.FAILED
: insterstitial is failed to load an ad.AdStatus.PRESENT
: interstitial is displayed and is on screen.AdStatus.WILL_LEAVE
: interstitial is redirecting user to a URL.AdStatus.USED
: interstitial is dismissed and needs reloading for another display
Rewarded Ads
Rewarded ads are the ads where users have the option of interacting with in exchange for in-app rewards.
Before you load and show a rewarded ad you must set EMASettings.instance.activity = currentActivity
and the listener for the status of ad RewardedAdDisplayManager.instance.listener = this
.
Full Example
- Java
- Kotlin
public class MyActivity extends AppCompatActivity implements AdStatusListener {@Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_sample);RewardedAdDisplayManager.getInstance().loadRewarded()}@Override public void rewardedStatusChanged(@NotNull RewardedAdManager manager) {if(manager.getStatus().equals(AdStatus.READY)) {RewardedAdDisplayManager.getInstance().showRewarded();}}}
By setting RewardedAdDisplayManager
's listener
you can listen to the status events of rewarded ads. These events are:
AdStatus.READY
: rewarded is ready for display.AdStatus.FAILED
: rewarded ad is failed to load.AdStatus.OPENED
: rewarded ad is started to play and is on screen.AdStatus.REWARDED
: user complete playing and earns reward.AdStatus.CLOSED
: rewarded ad is closed either by user or complete playing.
IMA Preroll Ads
IMA preroll ads can be displayed on ExoPlayer instances.
In order to be able to display a preroll ad, first you must request ads from the SDK:
- Java
- Kotlin
...// After setting the video request adsIMADisplayManager.getInstance().setListener(this) // AdStatusListenerIMADisplayManager.getInstance().requestAd(["YOUR_AD_CATEOGRY"], playerContainer, exoPlayer)
By setting IMADisplayManager
listener you can listen to the status events of preroll ads. These events are:
AdStatus.READY
: preroll ad is ready for display.
AdStatus.FAILED
: preroll ad is either failed to play or failed to load.
AdStatus.ACTIVE
: preroll ad is being played.
AdStatus.PAUSED
: preroll ad is paused by the user.