> For the complete documentation index, see [llms.txt](https://docs.digitalturbine.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.digitalturbine.com/dt-exchange/sdk-configuration/integrating-the-android-sdk/android-ad-formats/native-ads.md).

# Native Ads

Native ads are video or interactive ads that appear within the context of the user experience, such as in-feed sponsored content.

## Building the Ad Placement (Ad Spot) <a href="#h_01k83r5g5686erh7rkh5tvqmve" id="h_01k83r5g5686erh7rkh5tvqmve"></a>

Add the required controllers to set up the ad placement:

{% code title="Java" %}

```java
// Spot integration for native ad
InneractiveAdSpot mSpot = InneractiveAdSpotManager.get().createSpot();

// Adding the native controller
NativeAdUnitController adUnitController = new NativeAdUnitController();

// Adding the native video controller
NativeAdVideoContentController nativeAdVideoContentController = new NativeAdVideoContentController();

// Adding the native video controller to the ad unit controller
adUnitController.addContentController(nativeAdVideoContentController);
mSpot.addUnitController(adUnitController);
```

{% endcode %}

## Adding Listeners <a href="#h_01k83r5g56qryngr783gsksmrb" id="h_01k83r5g56qryngr783gsksmrb"></a>

Add listeners for the controller and the spot.

### Adding Event Listener for the Controller <a href="#h_01k83r5g56ngdd8tj304kavbdz" id="h_01k83r5g56ngdd8tj304kavbdz"></a>

The examples below demonstrate how you would add an `EventListener` to receive native ad callbacks.

#### Events: <a href="#h_01k83r5g56vfrzh52d8cr9n6gm" id="h_01k83r5g56vfrzh52d8cr9n6gm"></a>

{% code title="Java" %}

```java
adUnitController.setEventsListener(new NativeAdEventsListenerWithImpressionData() {
  @Override
  public void onAdImpression(InneractiveAdSpot adSpot, ImpressionData impressionData) { }

  @Override
  public void onAdImpression(InneractiveAdSpot adSpot) { }

  @Override
  public void onAdClicked(InneractiveAdSpot adSpot) { }

  @Override
  public void onAdWillCloseInternalBrowser(InneractiveAdSpot adSpot) { }

  @Override
  public void onAdWillOpenExternalApp(InneractiveAdSpot adSpot) { }
});
```

{% endcode %}

#### Video Events: <a href="#h_01k83r5g56wnrtf7b65fqyb5fy" id="h_01k83r5g56wnrtf7b65fqyb5fy"></a>

{% code title="Java" %}

```java
nativeAdVideoContentController.setEventsListener(new VideoContentListener() {
  @Override
  public void onProgress(int totalDurationInMsec, int positionInMsec) {
                
  }

  @Override
  public void onCompleted() {

  }

  @Override
  public void onPlayerError() {

  }
});

adUnitController.addContentController(nativeAdVideoContentController);
```

{% endcode %}

### Adding Request Listener for the Spot <a href="#h_01k83r5g56b7yc9qd607gfqks2" id="h_01k83r5g56b7yc9qd607gfqks2"></a>

The example below demonstrates how you would add a `RequestListener` for the Spot:

{% code title="Java" %}

```java
InneractiveAdSpot.NativeAdRequestListener spotListener = new InneractiveAdSpot.NativeAdRequestListener() {
  // When the ad loaded successfully
  @Override
  public void onInneractiveSuccessfulNativeAdRequest(InneractiveAdSpot adSpot, NativeAdContent adContent) {
  // Native ad is ready
  adContent.bindMediaView(new MediaView(applicationContext));
    
  populateNativeAdLayout(adContent);  
  registerViewsForInteraction(adContent);
  }
  // When the ad request fails to send
  @Override
  public void onInneractiveFailedAdRequest(InneractiveAdSpot adSpot, InneractiveErrorCode errorCode) {
  // Handle ad request failure
  }
};

mSpot.setRequestListener(spotListener);
```

{% endcode %}

## Fetching the Ad <a href="#h_01k83r5g56wgkfeaw56fabj18v" id="h_01k83r5g56wgkfeaw56fabj18v"></a>

Native ads are only available through SDK Bidding mediation. In SDK Bidding, DT Exchange must first generate a token for the Mediation SDK to use when identifying the request to the server.

Use `getBidderToken` to generate a Bidder Token in a background thread:

{% code title="Java" %}

```java
BidTokenProvider.getBidderToken();
```

{% endcode %}

Return type: `java.lang.String`. If an error occurs, the returned value is null.

If DT Exchange wins the auction, load the ad with the `signaldata (adm)` that was received from the server:

{% code title="Java" %}

```java
mSpot.loadAd(adm);
```

{% endcode %}

Optionally, you can mute ads by including `setMuteVideo(true)`:

{% code title="Java" %}

```java
InneractiveAdManager.setMuteVideo(true);
```

{% endcode %}

## Displaying the Ad <a href="#h_01k83r5g56b2qm9zmbgzz1g9wt" id="h_01k83r5g56b2qm9zmbgzz1g9wt"></a>

Define the ad layout in your XML file:

{% code title="XML" %}

```xml
<LinearLayout>
  <ImageView android:id="@+id/ad_icon"/>
  <LinearLayout>
    <TextView android:id="@+id/ad_title"/>
    <TextView android:id="@+id/ad_description"/>
    <RatingBar android:id="@+id/ad_rating"/>
  </LinearLayout>
</LinearLayout>


<FrameLayout
  android:id="@+id/media_view_container"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  app:layout_constraintHeight_min="<SET_HEIGHT>"/>	
    
<Button android:id="@+id/ad_cta_btn"/>
</androidx.constraintlayout.widget.ConstraintLayout>
```

{% endcode %}

Use `populateNativeAdLayout` to create the Native layout:

{% code title="Java" %}

```java
public void populateNativeAdLayout(NativeAdContent content) {
	
LayoutInflater inflater = LayoutInflater.from(context);
       adView = (ViewGroup) inflater.inflate(R.layout.native_ad_layout, null))

String adTitle = content.getAdTitle();
String adDescription = content.getAdDescription();
Uri appIconUri = content.getAppIcon();
String callToAction = content.getAdCallToAction();
Float rating = content.getRating();
MediaView mediaView = content.getMediaView();
Float aspectRatio = content.getMediaAspectRatio();

// Bind to native ad layout view e.g.:
// ImageView iconImageView = adView.findViewById(R.id.ad_icon);
// iconImageView.setImageURI(appIconUri);
// FrameLayout mediaViewContainer = adView.findViewById(R.id.media_view_container);
// mediaViewContainer.addView(mediaView);
}
```

{% endcode %}

For a list of supported Native Ads asset types, see the [oRTB Specification for Native Ads](https://gitlab.com/digitalturbine/dt-developer-portal/gitbook/-/blob/main/14_exchange_dsp/dt-exchange-openrtb-2.5-specs#object-native).

Use `registerViewsForInteraction` to enable user interaction with the Native ad and register tracking events:

{% code title="Java" %}

```java
public void registerViewsForInteraction(NativeAdContent content) {
  adView.setTag(NativeAdContent.ViewTag.ROOT);
  content.getMediaView().setTag(NativeAdContent.ViewTag.MEDIA_VIEW);
  iconImageView.setTag(NativeAdContent.ViewTag.AD_ICON);
  ctaButton.setTag(NativeAdContent.ViewTag.CTA);
  ratingView.setTag(NativeAdContent.ViewTag.RATING);
  descriptionTextView.setTeg(NativeAdContent.ViewTag.AD_DESCRIPTION);
  titleTextView.setTag(NativeAdContent.ViewTag.AD_TITLE);
	
  content.registerViewsForInteraction(rootViewGroup, content.getMediaView(), iconImageView, 
    Arrays.asList(ctaButton, ratingView, adDescription, adTitle));
}
```

{% endcode %}

## Releasing an Ad Placement <a href="#releasing-an-a-d-placement-0-17" id="releasing-an-a-d-placement-0-17"></a>

DT recommends releasing the allocated resources for ad display. For more information, see [Releasing Ad Instance Resources](/dt-exchange/sdk-configuration/integrating-the-android-sdk/releasing-ad-instance-resources.md).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.digitalturbine.com/dt-exchange/sdk-configuration/integrating-the-android-sdk/android-ad-formats/native-ads.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
