> For the complete documentation index, see [llms.txt](https://aatkit.gitbook.io/rtbsdk-android-integration/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://aatkit.gitbook.io/rtbsdk-android-integration/formats/native-ads.md).

# Native Ads

This guide shows how to integrate native ads of RTBSDK into your app.

## Create RTBNativeAdLoader

First, create an instance of [`RTBNativeAdLoader`](https://gravite-sdk-releases.s3.eu-central-1.amazonaws.com/rtbsdk/android/docs/-r-t-b--s-d-k/com.rtb.sdk/-r-t-b-native-ad-loader/index.html) , passing context instance and implementation of [`RTBNativeAdLoadDelegate`](https://gravite-sdk-releases.s3.eu-central-1.amazonaws.com/rtbsdk/android/docs/-r-t-b--s-d-k/com.rtb.sdk/-r-t-b-native-ad-load-delegate/index.html)

```kotlin
val loader = RTBNativeAdLoader(context, loadListener)
```

## Request Ad

To load an ad, you will need to pass an instance of [`RTBNativeAdRequestConfiguration`](https://gravite-sdk-releases.s3.eu-central-1.amazonaws.com/rtbsdk/android/docs/-r-t-b--s-d-k/com.rtb.sdk/-r-t-b-native-ad-request-configuration/index.html) with `placementID` and the `bundleId`. Please contact our [support](mailto:support@gravite.net) to get the needed IDs.

```kotlin
val request = RTBNativeAdRequestConfiguration(<PLACEMENT_ID>, <BUNDLE_ID>)
request.sellerId = "<SELLERID>" // Optional
loader.load(request)
```

#### Optional - set user targeting <a href="#complete-code-example" id="complete-code-example"></a>

You can pass user targeting data to each request, allowing ads to be more relevant to your audience. Each field in [RTBUserTargeting](https://gravite-sdk-releases.s3.eu-central-1.amazonaws.com/rtbsdk/android/docs/-r-t-b--s-d-k/com.rtb.sdk/-r-t-b-user-targeting/index.html) is optional.

```kotlin
request.userTargeting = RTBUserTargeting(userID = "<USER_ID>", gender = RTBGender.MALE, keywords = listOf("keyword1", "keyword2"), yearOfBirth = 1990)
// yearOfBirth must be a 4-digit number, otherwise it will be ignored
```

## Retrieve Native Ad Assets

Once the ad is loaded and the method `nativeAdDidReceiveAd` is called, you can get the needed native ad assets and assemble your view.

```kotlin
override fun nativeAdDidReceiveAd(nativeAd: RTBNativeAd, bidInfo: RTBBidInfo, networkName: String) {
    // Prepare your layout for native ad
    val adView: View = findViewById(R.id.native_ad)
    val iconView = adView.findViewById<ImageView>(R.id.iconImage)
    val mainImageView = adView.findViewById<ImageView>(R.id.mainImage)
    val titleView = adView.findViewById<TextView>(R.id.title)
    val descriptionView = adView.findViewById<TextView>(R.id.description)
    val callToActionView = adView.findViewById<TextView>(R.id.cta)

    // Fill the views with native ad assets
    nativeAd.iconUrl?.let {
        loadBitmapForView(it, iconView)
    }
    nativeAd.imageUrl?.let {
        loadBitmapForView(it, mainImageView)
    }
    titleView.text = nativeAd.title
    descriptionView.text = nativeAd.body
    callToActionView.text = nativeAd.callToAction
}
```

## (Optional) Use InteractionDelegate

If you are interested in native ad interaction events like clicks, you can pass optional [`RTBNativeAdInteractionDelegate`](https://gravite-sdk-releases.s3.eu-central-1.amazonaws.com/rtbsdk/android/docs/-r-t-b--s-d-k/com.rtb.sdk/-r-t-b-native-ad-interaction-delegate/index.html) to native ad instance

```kotlin
nativeAd.listener = yourListener
```

## Track Native Ad

To make sure impression reporting and click handling is handled, call `trackImpression` method on native ad instance, passing the view used for rendering it.

```kotlin
nativeAd.trackImpression(adView)
```

## Complete Code Example

```kotlin
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Prepare native ad loader
        val loader = RTBNativeAdLoader(this, createLoadListener())

        // Prepare request configurations
        val request = RTBNativeAdRequestConfiguration(<PLACEMENT_ID>, <BUNDLE_ID>)
        request.sellerId = "<SELLERID>" // Optional
        
        // Request an ad
        loader.load(request)
    }
    
    private fun createLoadListener(): RTBNativeAdLoadDelegate {
        return object : RTBNativeAdLoadDelegate {
            override fun nativeAdDidReceiveAd(nativeAd: RTBNativeAd, bidInfo: RTBBidInfo, networkName: String) {
                // Ad had been loaded
                // Optionally - examine bid details:
                val priceCPM = bidInfo.priceCPM
                
                // Optionally - set interaction listener
                native.listener = createInteractionListener()
                
                // Render the native ad and start tracking
                renderNativeAd(nativeAd)
            }

            override fun nativeAdDidFailToReceiveAd(errorMessage: String, networkName: String) {
                // Failed to load an ad
            }
        }
    }
    
    private fun renderNativeAd(ad: RTBNativeAd) {
        // Prepare your layout for native ad
        val adView: View = findViewById(R.id.native_ad)

        val iconView = adView.findViewById<ImageView>(R.id.iconImage)
        val mainImageView = adView.findViewById<ImageView>(R.id.mainImage)
        val titleView = adView.findViewById<TextView>(R.id.title)
        val descriptionView = adView.findViewById<TextView>(R.id.description)
        val callToActionView = adView.findViewById<TextView>(R.id.cta)

        // Fill the views with native ad assets
        ad.iconUrl?.let {
            loadBitmapForView(it, iconView)
        }
        ad.imageUrl?.let {
            loadBitmapForView(it, mainImageView)
        }
        titleView.text = ad.title
        descriptionView.text = ad.body
        callToActionView.text = ad.callToAction

        // Track impression
        ad.trackImpression(adView)
    }    
    
    private fun createInteractionListener(): RTBNativeAdInteractionDelegate {
        return object : RTBNativeAdInteractionDelegate {
            override fun nativeAdDidRecordClick(nativeAd: RTBNativeAd, networkName: String) {
                // Native ad recorded click
            }

            override fun nativeAdDidPauseForAd(nativeAd: RTBNativeAd, networkName: String) {
                // The app paused after the user clicked on the ad
            }

            override fun nativeAdDidResumeAfterAd(nativeAd: RTBNativeAd, networkName: String) {
                // The app resumed after returning from ad
            }
        }
    }
    
}
```


---

# 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:

```
GET https://aatkit.gitbook.io/rtbsdk-android-integration/formats/native-ads.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
