Widget via Native

Have you gone through the instructions of Getting started?

  • Make sure to complete the SDK installation before performing the procedures in the following article
  • Widget via Native supports Android API 14 and above

See our code examples!

On our Github repository, you may find numerous code examples demonstrating the usage of our features.

Step 1: Update the AndroidManifest.xml

In addition to the SDK installation update, add the following tag to any activity intended to show the TaboolaWidget. It is important to include the following attribute to avoid reloading the Taboola recommendations multiple times unnecessarily:

<!-- Add to your activity section on the AndroidManifest.xml -->
<activity android:name=".MainActivity" 
   android:configChanges="orientation|screenSize|keyboardHidden">
</activity>

Step 2: Initialize Taboola

Create a PublisherInfo object and init Taboola

PublisherInfo publisherInfo = new PublisherInfo(<MyPublisherId>);
Taboola.init(publisherInfo);
  • publisherId - Your publisher ID as provided to you by Taboola account manager

Step 3: Displaying the TaboolaWidget

There are two options of adding TaboolaWidget to your layout. One is using the layout XML file and one is dynamically creating the object in the code.

Layout XML

In your Activityor Fragment layout xml file:

  1. Include the Taboola XML declaration and include the TaboolaWidget tag
  2. Configure the TaboolaWidget properties.
    In the XML below, replace the following attribute values with those provided by your Taboola account manager: publisher, mode, placement, url, page_type, and `target_type.
<!-- Android SDK 2.2.2 and below: -->
<com.taboola.android.TaboolaWidget
    android:id="@+id/taboola_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    taboola:publisher="<publisher-as-supplied-by-taboola>"
    taboola:mode="<mode-as-supplied-by-taboola>"
    taboola:placement="<placement-as-supplied-by-taboola>"
    taboola:url="<public-web-url-which-reflects-the-current-content>"
    taboola:page_type="<my-page-type>"
    taboola:target_type="<my-target-type>"
/>

<!-- Android SDK 2.2.3 and above: -->
<com.taboola.android.TaboolaWidget
    android:id="@+id/taboola_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    taboola:tb_publisher="<publisher-as-supplied-by-taboola>"
    taboola:tb_mode="<mode-as-supplied-by-taboola>"
    taboola:tb_placement="<placement-as-supplied-by-taboola>"
    taboola:tb_url="<public-web-url-which-reflects-the-current-content>"
    taboola:tb_page_type="<my-page-type>"
    taboola:tb_target_type="<my-target-type>"
/>
  • taboola:tb_mode - Your mode parameter as provided by your Taboola account manager
  • taboola:tb_page_type - Your page type parameter as provided by your Taboola account manager
  • taboola:tb_placement - Your placement parameter as provided by your Taboola account manager
  • taboola:tb_publisher - Your publisher parameter as provided by your Taboola account manager
  • taboola:tb_target_type - Your target type parameter as provided by your Taboola account manager
  • taboola:tb_url - The full URL of public web page which reflects the current screens content. If the view is used on other screens, please change this value using the setPageUrl function as described below in "Dynamically via the code" section

"tb_" prefix for Taboola attributes

From Android SDK version 2.2.3 and above you should add "tb_" prefix before each Taboola attribute. For example instead of "mode" you should use "tb_mode"

Note!

Taboola Widget will automatically resize itself to show its content. When defining the Widget via XML please set the height to wrap_content. The Widget will resize itself when it loads.

  1. Import and init the TaboolaWidget.
// Import TaboolaWidget
import com.taboola.android.TaboolaWidget;

// Declare an class member instance TaboolaWidget
private TaboolaWidget taboolaWidget;

  1. In your Activity's OnCreate() or Fragment's OnCreateView(), find the TaboolaWidget defined in the XML.
taboolaWidget = (TaboolaWidget) <Activity>.findViewById(R.id.taboola_view);

Dynamicly via the code

You can create TaboolaWidget in your code as early as possible in the app lifecycle.

  • Your activity's onCreate() method
  • Your fragmant's onCreateView() method
//Import TaboolaWidget
import com.taboola.android.TaboolaWidget;

//Declare an class member instance TaboolaWidget
private TaboolaWidget taboolaWidget;

//In your Activity's OnCreate() or Fragment's OnCreateView(), create a new TaboolaWidget instance
taboolaWidget = new TaboolaWidget(<Context>);

//Assign LayoutParams to TaboolaWidget
taboolaWidget.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT));

//Add TaboolaWidget to your layout (This example assumes your parent layout is a FrameLayout with an arbitrary id parent_layout)
FrameLayout frameLayout = (FrameLayout) <Activity>.findViewById(R.id.parent_layout);
frameLayout.addView(taboolaWidget);

//Set the following parameters in your TaboolaWidget instance, before calling fetchContent()
taboolaWidget.setPublisher("<publisher-as-supplied-by-taboola>")
  .setMode("<mode-as-supplied-by-taboola>")
  .setPlacement("<placement-as-supplied-by-taboola>")
  .setPageUrl("<public-web-url-which-reflects-the-current-content>")
  .setPageType("<my-page-type>")
  .setTargetType("<my-target-type>")
	//Optional: sets an internal ID for the current page
  .setPageId("<your-internal-id-for-the-current-page>");

//fetch the contebt
taboolaWidget.fetchContent();

  • setPublisher - Your publisher id parameter as provided by your Taboola account manager
  • setMode - Your mode parameter as provided by your Taboola account manager
  • setPlacement - Your placement parameter as provided by your Taboola account manager
  • setPageUrl - The full URL of a public web page which reflects the current screen's content
  • setPageType - Your page type parameter as provided by your Taboola account manager
  • setTargetType - Your target type parameter as provided by your Taboola account manager
  • setPageId (optional) - An internal identification for the current screen's content. Pass this along the page URL to set the item ID as your internal ID

Step 4: Fetch content

  1. To show TaboolaWidget recommendations, request fetchContent().
  2. Adjust the Widget height according to the next step
taboolaWidget.fetchContent();

Important Note

  • To maintain good UX - please fetch content as soon as the screen loads and not when the user reaches the Taboola area on the screen
  • When working with ViewPager, Please fetch the widget content only when the screen/fragment is visible to the user. Fetch the content using the ViewPager.OnPageChangeListener listener

Step 5: Adjust the Widget height

As described in step 2, TaboolaWidget adjusts it height automatically. The height of the widget may dynamically change according to the different UI mode that is loaded in Taboola Backstage. When the widget resizes itself it will also resize the cell the widget is in.

In order to adjust the app layout (the cell that contains the widget) to these changes you can call the taboolaViewResizeHandler method of the TaboolaEventListener.

If wish to adjust the size of Taboola Widget manually, you can do so by setting autoResizeHeight to false and set fixed height for TaboolaWidget manually. Please keep in mind that setting a fixed height for widget will require a code change in case the widget layout changes in the future. We recommend to use dynamic height.

taboolaWidget.setTaboolaEventListener(new TaboolaEventListener() {
 @Override
 public boolean taboolaViewItemClickHandler(String url, boolean isOrganic) {
  doLogic();
  return false; // See step 5 "Intercepting Recommendation Clicks"
 }

 @Override
 public void taboolaViewResizeHandler(TaboolaWidget widget, int height) {
  doResizeLogic();
   //Set the cell height according to the final height value...
 }
});

Important

In order to allow Taboola increase/decrease the widget size, please keep the Widget size definition as dynamic. Using fixed height can cause native code changes on your app size to reflect the changes

Step 6: Additional Integration Information (Optional)

This section describes some optional points in the integration.

Catching global notifications (broadcasts) from TaboolaWidget

Taboola fires app level broadcasts to notify registered objects within the app about certain events.

To catch those notifications, you can use the class com.taboola.android.globalNotifications.GlobalNotificationReceiver

  1. Create a new GlobalNotificationReceiver object in your Activity/Fragment
  2. In `OnResume() register GloablNotificationReceiver using the following code:
@Override
public void onResume() {
   super.onResume();
   mGlobalNotificationReceiver.registerNotificationsListener(this);
   mGlobalNotificationReceiver.registerReceiver(getActivity());
}
  1. Implement the interface OnGlobalNotificationsListener. Make sure to include the methods:
public void taboolaDidReceiveAd(TaboolaWidget widget);
public void taboolaViewResized(TaboolaWidget widget, int height);
public void taboolaItemDidClick(TaboolaWidget widget);
public void taboolaDidFailAd(TaboolaWidget widget, String reason);
  1. Make sure you unregister the GloablNotificationReceiver on onPause() using this code:
@Override
public void onPause() {
   super.onPause();
   mGlobalNotificationReceiver.unregisterNotificationsListener();
   mGlobalNotificationReceiver.unregisterReceiver(getActivity());
} 

You can see a code example of the GlobalNotificationReceiver here

Intercepting Organic Recommendation Clicks

The SDK enables app developers to change the default behavior when a user clicks on an organic item. For example, you might want to create a click-through or to override the default way of opening the organic recommended article which is opening the click using Chrome custom tab.

To intercept organic clicks, implement the interface com.taboola.android.listeners.TaboolaEventListener:

//TaboolaEventListener include the methods:
public boolean taboolaViewItemClickHandler(String url, boolean isOrganic);
public void taboolaViewResizeHandler(TaboolaWidget widget, int height);

//Example implementation: In the same Activity/Fragment as TaboolaWidget instance:
TaboolaEventListener taboolaEventListener = new TaboolaEventListener() {
@Override
public boolean taboolaViewItemClickHandler(String url, boolean isOrganic) {
   //Code...
   return false;
}

@Override
public void taboolaViewResizeHandler(TaboolaWidget taboolaWidget, int height) {
   //Code...
}};

//Connect the event listener to your TaboolaWidget instance.
taboolaWidget.setTaboolaEventListener(taboolaEventListener);

Event: taboolaViewItemClickHandler
boolean taboolaViewItemClickHandler(String url, boolean isOrganic). This method is called every time a user clicks a Taboola Recommendation, immediately before it is handled by the operating system to resolve the relevant action. The return value of this method enables you to control further system behavior (after your own code executes).
URL - the original click URL
isOrganic - indicates whether the item clicked was organic Taboola Recommendation content or not. Best practice is to suppress the default behavior for organic items and, instead, open the relevant screen in your app which shows that piece of content.
Return value:

  • Returning false - the click's default behavior is aborted. The app should display the Taboola Recommendation content on its own (for example, using an in-app browser).
  • Returning true - the click is a standard one and is sent to the Android OS for default behavior.
    Example:
@Override
public boolean taboolaViewItemClickHandler(String url, boolean isOrganic) {
    ...
    if (isOragnic){
        ...
        showInAppContent(url);
        return false;
    }
    return true;
}

Using more than one Taboola asset on the same screen

It is possible to add more than one Taboola asset on the same screen. For example, placing two Taboola widgets on a feed style screen. When doing so it is important to avoid duplicate items on each asset

To do so, please make sure you do the following:

  1. Set the same viewId value for each taboolaWidget object before fetching content. The viewId value must be a string of digits. We recommend using the current epoch timestamp. In any case, the viewId value can not exceed 19 digits.
  2. Fetch the content for the second asset only after a response is received for the first asset. Please use the GlobalNotificationReceiver to listen to render is done events
//Sets view ID value for the first item and fetch content
taboolaWidgetMiddleScreen.setViewId("12345");
taboolaWidgetMiddleScreen.fetchContent();

//Sets view ID value for the second item
taboolaFooterScreen.setViewId("12345");


//Fetch content for the 2nd Taboola asset only after the completion of the 1st item
@Override
public void taboolaDidReceiveAd(TaboolaWidget taboolaWidget) {
 Log.d(TAG, "taboolaDidReceiveAd() called with: taboolaWidget = [" + taboolaWidget + "]");
 taboolaFooterScreen.fetchContent();
}

Step 7: Test the integration

To test the integration, you can use our Android SDK Integration Verifier special mode

In addition to that, you can use the following parameters for testing and development. Sponsored items are flagged with an SC: prefix in the title; organic items are flagged with an OC: prefix in the title.

Testing parameters
Mid article widget
Below article widget

publisher

"sdk-tester-demo"

"sdk-tester-demo"

placement

"Mid Article"

"Below Article"

PageType

"article"

"article"

TargetType

"mix"

"mix"

mode

"alternating-widget-without-video-1-on-1"

"alternating-widget-without-video-1x4"

Step 8: Code Examples

You can find the entire code examples on our Github repository

For your convenience we listed some quick links for noticable examples:

Step 9: Submit APK for QA

When you have finished the integration, contact your account manager and send a debuggable version of your Android project. Set the project to allow SSL proxying.

You're done!

Integration Support

Should you have any problems integrating the product, log a ticket with us by emailing your account manager. For non official SLA support you can also check our discussion forum

Updated 21 days ago


Widget via Native


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.