# Native Ads

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

## Create RTBNativeAdLoader&#x20;

First, create an instance of [`RTBNativeAdLoader`](https://android-sdk-rtb.gravite.net/references/-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://android-sdk-rtb.gravite.net/references/-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://android-sdk-rtb.gravite.net/references/-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.&#x20;

```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://android-sdk-rtb.gravite.net/references/-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://android-sdk-rtb.gravite.net/references/-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
            }
        }
    }
    
}
```
