Native (Google)

The native ads are a kind of ad that can be shown using the same visual style like the rest of the app, which allows a natural and non-intrusive integration with the user interface.

Currently, Wortise provides the possibility of use the native ads from Google's ad platform, in a simple and direct way.

Integration

First of all, it is needed to create a Java (or Kotlin) class that implements a GoogleNativeAdFactory object. This object will contain a method that will receive a NativeAd from Google and will return a NativeAdView object with the rendered native ad.

This step is very similar to the one explained by Google in their own documentation, with the only difference of using GoogleNativeAdFactory instead of NativeAdFactory.

Below we show an implementation example of this class:

import android.graphics.Color
import android.view.LayoutInflater
import android.widget.TextView
import com.google.android.gms.ads.nativead.NativeAd
import com.google.android.gms.ads.nativead.NativeAdView

class NativeAdFactoryExample(private val layoutInflater: LayoutInflater)
    : GoogleNativeAdFactory {

    override fun createNativeAd(nativeAd: NativeAd): NativeAdView {
        val adView = layoutInflater.inflate(R.layout.my_native_ad, null) as NativeAdView
        
        val headlineView = adView.findViewById<TextView>(R.id.ad_headline)
        val bodyView     = adView.findViewById<TextView>(R.id.ad_body)
    
        headlineView.setText(nativeAd.headline)

        bodyView.setText(nativeAd.body)
    
        adView.setBackgroundColor(Color.YELLOW)
        adView.setNativeAd(nativeAd)
        adView.setBodyView(bodyView)
        adView.setHeadlineView(headlineView)
        
        return adView
    }
}

Once implemented, this class has to be registered inside the MainActivity as in the following example:

In this example, test-factory is the identifier that is assigned to the GoogleNativeAdFactory object, which should to be used later when creating a native ad in Flutter.

Next, to request a native ad, it is needed to make a code integration like the following:

Once the native ad has been successfully loaded, it is needed to create an AdWidget widget that must be added to the app's interface.

This step can be implemented like in the following example:

Listener events

The assigned listener to an GoogleNativeAd can receive the following events:

Última actualización