Adex

This feature allows customer to add inside app different kinds of ads. At this moment we support banner, full screen (interstitial), native and app open ads. First we need to initialize adex feature (choose one).

  • Add Ocamba Track Id to manifest:

Open app module’s AndroidManifest.xml file and add a new line in Application tags:

<meta-data android:name="com.ocamba.TrackId" android:value=TRACK_ID" /> 

Open application class and add to onCreate method:

OcambaHoood.adex().init();
  • Add Ocamba Api Key in init method:
OcambaHoood.init(API_KEY);

NOTE: replace TRACK_ID with track id created on Ocamba platform.

Banner ads will be part of app’s layout, usually at the top or bottom of the screen. They will stay on the screen while user interacts with app. After each screen view banner will be refreshed (it show same ad again or different one).

  1. Add view
    This should be done inside activity or fragment layout or there this is an options to add this programatically. We will give you code snippet how to do it in XML:
<com.ocamba.hoood.adex.banner.OcambaAdexBanner
   android:id="@+id/ocamba_adex_banner"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_alignParentBottom="true"
   android:visibility="gone"
   app:bannerSize="SMALL"
   app:layout_constraintBottom_toBottomOf="parent"
   app:layout_constraintEnd_toEndOf="parent"
   app:layout_constraintStart_toStartOf="parent"
   app:placementId="Mobile_Banner"
   app:showCloseIcon="SHOW" />

We have three new attributes:

  • showCloseIcon - with value true/false, this will show/hide close icon.

  • placementId - unique identifier for this ad.

  • bannerSize - with value SMALL/MEDIUM/LARGE/ADAPTIVE, check size section for more info.

  1. How to load ad - this will show ad when it finish loading:
ocambaAdexBanner = view.findViewById(R.id.ocamba_adex_banner);
ocambaAdexBanner.setOcambaAdexAdListener(this);
ocambaAdexBanner.loadAd();
  1. Listen for ad events:
@Override
public void onAdLoaded() {
//ad loaded - when ad is loaded
}
@Override
public void onAdLoadError() {
//ad loading error - when ad is not loaded for some reason
}
@Override
public void onAdClicked() {
//ad clicked - when user clicks in ad
}
@Override
public void onAdClosed() {
//ad closed - when user clicks on close icon
}
  1. Banner sizes:
  • SMALL - 320X50
  • MEDIUM - 320x100
  • LARGE - 300x250
  • ADAPTIVE - 320x32/50/90
Full screen

Full screen ads (interstitial) are action based and displayed in full screen. They are typically displayed at natural transition points in app flow, between screens, clicks… When this ad is shown user has option to click on it or to close it and return to app.

  1. Define and load ad:
ocambaAdexInterstitial = new OcambaAdexInterstitial(getActivity());
ocambaAdexInterstitial.setOcambaAdexInterstitialListener(this);
ocambaAdexInterstitial.loadAd(placementId);
  1. Show ad:
ocambaAdexInterstitial.displayAd();
  1. Listen for events:
@Override
public void onAdStartLoading() {
//ad started loading - when ad started loading
}
@Override
public void onAdError() {
   //ad loading error - when ad is not loaded for some reason 
}
@Override
public void onAdLoaded() {
//ad loaded - when ad is loaded
}
@Override
public void onAdDisplayed() {
//ad displayed - when user sees ad
}
@Override
public void onAdClosed() {
//ad closed - when user clicks on close icon
}
@Override
public void onAdClicked() {
   //ad clicked - when user clicks on ad
}
Native ads

Native ads should be presented to users with native UI components. This ads should be formatted to match visual design of developers app. This means that when ad loads, developer’s app receives object from which layout should be shown.

  1. Create ad layout which contains few UI elements.
  2. Define and load ad:
OcambaAdexNativeAd nativeAd = new OcambaAdexNativeAd();
nativeAd.loadAd(requireContext(), "Mobile_Banner", OcambaAdexNativeAd.NativeAdSize.SMALL, new OcambaAdexNativeAdLoadListener() {

});
  1. In OcambaAdexNativeAdLoadListener callback user developer should override method and populate defined layout (from step one) with items from OcambaAdexNativeAd object:
OcambaAdexNativeAd nativeAd = new OcambaAdexNativeAd();
nativeAd.loadAd(requireContext(), "Mobile_Banner", OcambaAdexNativeAd.NativeAdSize.SMALL, new OcambaAdexNativeAdLoadListener() {
   @Override
   public void onOcambaAdexNativeAdLoaded(OcambaAdexNativeAd ocambaAdexNativeAd) {

       @LayoutRes int layoutId = R.layout.native_ad_practise_small;
       OcambaAdexNativeAdView adView = (OcambaAdexNativeAdView) getLayoutInflater().inflate(layoutId, null);
       TextView adTitle = adView.findViewById(R.id.ad_title);
       OcambaMediaView adImage = adView.findViewById(R.id.ad_image);
       OcambaAdexAdObject object = ocambaAdexNativeAd.getAdexAdObject();
       adTitle.setText(object.getTitle());
     
       //use image
       adImage.setMediaContent(ocambaAdexNativeAd);
       //clicks are handled from SDK
       adView.setOcambaAdexAd(ocambaAdexNativeAd);
       adView.setClickableView(getContext(), adClick);
       adView.setClickableView(getContext(), adImage);
       //clicks
       //add view to placeholder
       adPlaceholder.removeAllViews();
       adPlaceholder.addView(adView);
   }
});
Open ads

Open ads are designed for developers who want’s to monetize app load screens. Should be presented when users bring app to the foreground.

  1. Extend Application class and override onCreate method. You will need to initialize AppOpenAdManager.
  2. Create class that will load ad. In our case we created inner class that loads and shows ads.
  3. Listen for activity lifecycle callbacks. We will need context to show ad, so keep track of the most current activity used by user.
  4. Show ad and handle callbacks when ad is presented, fails, or when it is dismissed.
  5. Implement and register LifecycleObserver, your app will need it to show ad at the appropriate times.

Full example:

public class MainApp extends Application implements DefaultLifecycleObserver {
   
   private AppOpenAdManager appOpenAdManager;

   @Override
   public void onCreate() {
   	super.onCreate();

       OcambaHoood.push().adex().init();

       ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
       	appOpenAdManager = new AppOpenAdManager(this);
   	}

   	@Override
   	public void onStart(@NonNull LifecycleOwner owner) {
   		DefaultLifecycleObserver.super.onStart(owner);
       	if(appOpenAdManager != null) appOpenAdManager.showAdIfAvailable();
   	}

   /**
    * Interface definition for a callback to be invoked when an app open ad is complete
    * (i.e. dismissed or fails to show).
    */
   public interface OnShowAdCompleteListener {
       void onShowAdComplete();
   }

   public void showAdIfAvailable(
       @NonNull Activity activity, 
       @NonNull OnShowAdCompleteListener onShowAdCompleteListener) {
   	appOpenAdManager.showAdIfAvailable(activity, onShowAdCompleteListener);
   }

   private static class AppOpenAdManager implements Application.ActivityLifecycleCallbacks 			{
       private Activity currentActivity;

       private OcambaAdexAppOpen appOpenAd = null;
       private boolean isLoadingAd = false;
       private boolean isShowingAd = false;

       /**
        * Keep track of the time an app open ad is loaded to ensure you don't show an expired ad.
        */
       private long loadTime = 0;

       /**
        * Constructor.
        */
       public AppOpenAdManager(Application application) {
           if (application != null) application.registerActivityLifecycleCallbacks(this);
      	}

       /**
        * Show the ad if one isn't already showing.
        */
       private void showAdIfAvailable() {
          	showAdIfAvailable(
               currentActivity,
               () -> {
                   // Empty because the user will go back to the activity that shows the ad.
               });
       }

       private void showAdIfAvailable(
           @NonNull final Activity activity,
           @NonNull OnShowAdCompleteListener onShowAdCompleteListener) {
           
   		// If the app open ad is already showing, do not show the ad again.

   		if (isShowingAd) {
   		// The app open ad is already showing.
              	return;
          	}

           if (!isAdAvailable()) {
   			// The app open ad is not ready yet.
               onShowAdCompleteListener.onShowAdComplete();
               loadAd(activity);
               return;
          	}

           if (appOpenAd != null) {
               appOpenAd.setAdCallbackListener(activity, 
               new OcambaAdexCallback() {    				
   			    @Override
                   public void onAdClicked() {
                       super.onAdClicked();

                       appOpenAd = null;
                       isShowingAd = false;
                       onShowAdCompleteListener.onShowAdComplete();
                   }

                   @Override
                   public void onAdDismissedFullScreenContent() {
                       super.onAdDismissedFullScreenContent();
                      
                       appOpenAd = null;
                       isShowingAd = false;
                       onShowAdCompleteListener.onShowAdComplete();
                       loadAd(activity);
                   }
               });

               appOpenAd.displayMediaAd(activity);
               isShowingAd = true;
           } else {
               // The app open ad is null.
           }
       }

       private void loadAd(Context context) {
           // Do not load ad if there is an unused ad or one is already loading.
          
           if (isLoadingAd || isAdAvailable() || context == null) {
               return;
           }

           isLoadingAd = true;

           new OcambaAdexAppOpen(context).loadAd(
   				context, 
   				"Mobile_Banner", 
   				new OcambaAdexAdLoadListener() {
               @Override
               	public void onAdLoaded(OcambaAdexAppOpen appOpenAd) {
                   
                   	AppOpenAdManager.this.appOpenAd = appOpenAd;
                   	loadTime = (new Date()).getTime();
                  		isLoadingAd = false;
               	}
               @Override
               	public void onAdLoadError(String error) {
                   
                   	isLoadingAd = false;
               	}
           	});

           }

       /**
        * Check if ad exists and can be shown.
        */
       private boolean isAdAvailable() {
       	// Ad references in the app open beta will time out after four hours, but this time limit
       	// may change in future beta versions. 
           return appOpenAd != null && wasLoadTimeLessThanNHoursAgo(4);
       }

       /**
        * Check if ad was loaded more than n hours ago.
        */
       private boolean wasLoadTimeLessThanNHoursAgo(long numHours) {
           long dateDifference = (new Date()).getTime() - loadTime;
           long numMilliSecondsPerHour = 3600000;
           return (dateDifference < (numMilliSecondsPerHour * numHours));
       }

       @Override
       public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle bundle) {

       }


       @Override
       public void onActivityStarted(@NonNull Activity activity) {
   		currentActivity = activity;
       }

       @Override
       public void onActivityResumed(@NonNull Activity activity) {
           currentActivity = activity;
       }

       @Override
       public void onActivityPaused(@NonNull Activity activity) {

       }

       @Override
       public void onActivityStopped(@NonNull Activity activity) {

       }

       @Override
       public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle bundle) {

       }

       @Override
       public void onActivityDestroyed(@NonNull Activity activity) {
           currentActivity = null;
       }
   }
}