Supports Android OS 4.1.x (Jelly Bean) and up.
You will need a MobFox account.
MobFox SDK can easily be added as a dependency from JitPack package repository.
Add JitPack to your repositories, if missing.
In your build.gradle:
allprojects {
repositories {
//... other repositories ...
maven { url "https://jitpack.io" }
}
}Next, add Google Play Services and MobFox-Android-SDK-Core to your compile dependencies:
dependencies {
//... other dependencies ...
compile 'com.google.android.gms:play-services-ads:+'
compile 'com.github.mobfox:MobFox-Android-SDK-Core:3.2.7b'
}Done.
Download and unzip MobFox-Android-SDK or clone this repository and extract the MobFox-Android-SDK-Core-latest.jar and put it in your project under the libs directory.
Next, In your gradle.build add the following dependencies:
dependencies {
compile 'com.google.android.gms:play-services-ads:+'
compile 'com.danikula:videocache:2.7.0'
compile files('libs/MobFox-Android-SDK-Core-3.+.jar')
}In your project's AndroidManifest.xml under the manifest tag, add the following permissions:
<uses-permissionandroid:name="android.permission.INTERNET"></uses-permission>
<uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
...
<application
...
<!--mobfox interstitial activity-->
<activityandroid:name="com.mobfox.sdk.interstitialads.InterstitialActivity"android:hardwareAccelerated="true" />
<serviceandroid:name="com.mobfox.sdk.services.MobFoxService"android:launchMode="singleTop" />
...
</application>
-keep class com.mobfox.** { *; }
-keep class com.mobfox.adapter.** {*;}
-keep class com.mobfox.sdk.** {*;}
Banner's width equals screen width.
In your layout xml, in the Banner tag set smart banner by adding the mobfox attribute xmlns:mobfox="http://schemas.android.com/apk/lib/com.mobfox.sdk" and set smart to true mobfox:smart="true"
Full code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"xmlns:mobfox="http://schemas.android.com/apk/lib/com.mobfox.sdk">
<com.mobfox.sdk.bannerads.Banner
android:layout_height="50dp"android:layout_width="320dp"mobfox:smart="true"android:id="@+id/banner"android:layout_centerVertical="true"android:layout_centerHorizontal="true">
</com.mobfox.sdk.bannerads.Banner>
</RelativeLayout>Sending the user's location will provide you with higher CPMs.
In your layout xml, in the Banner tag enable location by adding the mobfox attribute xmlns:mobfox="http://schemas.android.com/apk/lib/com.mobfox.sdk" and enable location to true mobfox:enableLocation="true"
Full code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"xmlns:mobfox="http://schemas.android.com/apk/lib/com.mobfox.sdk">
<com.mobfox.sdk.bannerads.Banner
android:layout_height="50dp"android:layout_width="320dp"mobfox:enableLocation="true"android:id="@+id/banner"android:layout_centerVertical="true"android:layout_centerHorizontal="true">
</com.mobfox.sdk.bannerads.Banner>
</RelativeLayout>This could also be achieved by calling the static method
Banner.setLoc(true);
Enable location by calling the static method InterstitialAd.getLocation(true);
Enable location by calling the static method Native.setLoc(true);
Find Demoapp source here, apk can be found under demoapp/release, and via demoapp-latest.apk in the project's root folder.
Add to your activity's layout xml:
<com.mobfox.sdk.bannerads.Banner
android:layout_width="300dp"android:layout_height="250dp"android:id="@+id/banner"android:layout_centerHorizontal="true"android:layout_centerVertical="true">
</com.mobfox.sdk.bannerads.Banner>It's advised to select popular layout_width/layout_height combinations so you'll get a good fill rate.
Popular sizes are: 320x50, 300x250, 320x480
In your activity set up the banner:
// ...importcom.mobfox.sdk.bannerads.Banner;
importcom.mobfox.sdk.bannerads.BannerListener;
// ...Bannerbanner;
@OverrideprotectedvoidonCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
banner = (Banner) findViewById(R.id.banner);
finalActivityself = this;
banner.setListener(newBannerListener() {
@OverridepublicvoidonBannerError(Viewbanner, Exceptione) {
Toast.makeText(self, e.getMessage(), Toast.LENGTH_SHORT).show();
}
@OverridepublicvoidonBannerLoaded(Viewbanner) {
Toast.makeText(self, "loaded", Toast.LENGTH_SHORT).show();
}
@OverridepublicvoidonBannerClosed(Viewbanner) {
Toast.makeText(self, "closed", Toast.LENGTH_SHORT).show();
}
@OverridepublicvoidonBannerFinished() {
Toast.makeText(self, "finished", Toast.LENGTH_SHORT).show();
}
@OverridepublicvoidonBannerClicked(Viewbanner) {
Toast.makeText(self, "clicked", Toast.LENGTH_SHORT).show();
}
@OverridepublicvoidonNoFill(Viewbanner) {
Toast.makeText(self, "no fill", Toast.LENGTH_SHORT).show();
}
});
banner.setInventoryHash("<your-publication-hash>");
banner.load();
}
//permission dialog for marshmello and above@OverridepublicvoidonRequestPermissionsResult(intrequestCode, String[] permissions, int[] grantResults) {
banner.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
//add this so video ads will work properly@OverrideprotectedvoidonPause() {
super.onPause();
banner.onPause();
}
@OverrideprotectedvoidonResume() {
super.onResume();
banner.onResume();
}
// ...Enable refresh by calling the method banner.setRefresh(5)
- Integer Units seconds
- Minimum 5 seconds
In your activity set up the interstitialAd interstitial:
// ...importcom.mobfox.sdk.interstitialads.InterstitialAd;
importcom.mobfox.sdk.interstitialads.InterstitialAdListener;
// ...InterstitialAdinterstitial;
InterstitialAdListenerlistener;
// ...@OverrideprotectedvoidonCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
interstitial = newInterstitialAd(this);
finalActivityself = this;
InterstitialAdListenerlistener = newInterstitialAdListener() {
@OverridepublicvoidonInterstitialLoaded(InterstitialAdinterstitial) {
Toast.makeText(self, "loaded", Toast.LENGTH_SHORT).show();
//call show() to display the interstitial when its finished loadinginterstitial.show();
}
@OverridepublicvoidonInterstitialFailed(InterstitialAdinterstitial, Exceptione) {
Toast.makeText(self, e.getMessage(), Toast.LENGTH_SHORT).show();
}
@OverridepublicvoidonInterstitialClosed(InterstitialAdinterstitial) {
Toast.makeText(self, "closed", Toast.LENGTH_SHORT).show();
}
@OverridepublicvoidonInterstitialFinished() {
Toast.makeText(self, "finished", Toast.LENGTH_SHORT).show();
}
@OverridepublicvoidonInterstitialClicked(InterstitialAdinterstitial) {
Toast.makeText(self, "clicked", Toast.LENGTH_SHORT).show();
}
@OverridepublicvoidonInterstitialShown(InterstitialAdinterstitial) {
Toast.makeText(self, "shown", Toast.LENGTH_SHORT).show();
}
};
interstitial.setListener(listener);
interstitial.setInventoryHash("<your-publication-hash>");
interstitial.load();
}
//permission dialog for marshmello and above@OverridepublicvoidonRequestPermissionsResult(intrequestCode, String[] permissions, int[] grantResults) {
interstitial.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
//add this so video ads will work properly@OverrideprotectedvoidonPause() {
super.onPause();
interstitial.onPause();
}
@OverrideprotectedvoidonResume() {
super.onResume();
interstitial.onResume();
}
// ...In your project's AndroidManifest.xml under the application tag, declare the following activity:
<activityandroid:name="com.mobfox.sdk.interstitialads.InterstitialActivity"></activity>
the layout below is used to show native assets:
// ...
<RelativeLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/nativeLayout"android:layout_below="@+id/spinnerInvh">
<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/headline"/>
<ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:adjustViewBounds="true"android:maxWidth="300dp"android:maxHeight="300dp"android:scaleType="fitCenter"android:id="@+id/nativeIcon"android:layout_below="@+id/headline"/>
<ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:adjustViewBounds="true"android:maxHeight="300dp"android:scaleType="fitCenter"android:id="@+id/nativeMainImg"android:layout_below="@+id/nativeIcon"/>
</RelativeLayout>
// ...
In your activity set up the native ad:
// ...importcom.mobfox.sdk.nativeads.Native;
importcom.mobfox.sdk.nativeads.NativeAd;
importcom.mobfox.sdk.nativeads.NativeListener;
importcom.mobfox.sdk.customevents.CustomEventNative;
// ...privateNativeaNative;
privateActivityself;
privateNativeListenerlistener;
//creating variables for our layoutTextViewheadline;
ImageViewnativeIcon, nativeMainImg;
RelativeLayoutlayout;
@OverrideprotectedvoidonCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.our_layout);
self = this;
//assigning xml components to our layoutheadline = (TextView) findViewById(R.id.headline);
nativeIcon = (ImageView) findViewById(R.id.nativeIcon);
nativeMainImg = (ImageView) findViewById(R.id.nativeMainImg);
layout = (RelativeLayout) findViewById(R.id.nativeLayout);
aNative = newNative(this);
//we must set a listener for nativelistener = newNativeListener() {
@OverridepublicvoidonNativeReady(NativeaNative, CustomEventNativeevent, NativeAdad) {
Toast.makeText(self, "on native ready", Toast.LENGTH_SHORT).show();
//register custom layout clickevent.registerViewForInteraction(layout);
//fire trackersad.fireTrackers(self);
headline.setText(ad.getTexts().get(0).getText());
ad.loadImages(self, newNativeAd.ImagesLoadedListener() {
@OverridepublicvoidonImagesLoaded(NativeAdad) {
Toast.makeText(self, "on images ready", Toast.LENGTH_SHORT).show();
nativeIcon.setImageBitmap(ad.getImages().get(0).getImg());
}
});
}
@OverridepublicvoidonNativeError(Exceptione) {
Toast.makeText(self, "on native error", Toast.LENGTH_SHORT).show();
}
@OverridepublicvoidonNativeClick(NativeAdad) {
Toast.makeText(self, "on native click", Toast.LENGTH_SHORT).show();
}
};
aNative.setListener(listener);
//load our nativeaNative.load("<your-publication-hash>");
}
// ...The NativeAd object returned by the native listener contains the ad data used to construct the native ad:
publicclassNativeAd {
...
publicList<Tracker> getTrackerList() {
returntrackerList;
}
publicList<ImageItem> getImages() {
returnimages;
}
publicList<TextItem> getTexts() {
returntexts;
}
publicStringgetLink() {
returnlink;
}
...
}
publicclassTextItem {
...
publicStringgetType() {
returntype;
}
publicStringgetText() {
returntext;
}
...
}
publicclassImageItem {
...
publicStringgetType() {
returntype;
}
publicStringgetUrl() {
returnurl;
}
publicintgetH() {
returnh;
}
publicintgetW() {
returnw;
}
publicBitmapgetImg() {
returnimg;
}
...
}The List<Tracker> returned by getTrackerList contains tracker url's you must call before displaying the ad by calling the NativeAd.fireTrackers method :
publicclassTracker {
publicStringgetType();
//you must call this url!publicStringgetUrl();
}You can test your implementations with these test inventory hashes
Custom Events let you use your accounts on other advertising platforms such as MoPub inside MobFox's SDK.
Adapters are the opposite of Custom Events, they let you use MobFox as a Custom Event in other networks.
Plugins can be found in SDK-plugins repository.