Android Native Ad Unit - freestarcapital/SDK_documentation_Android GitHub Wiki

Native Ad

Small Native Ad Example

small native ad

Medium Native Ad Example

medium native ad

Freestar supports the Native Ad unit. Native ads are well suited for content-based or feed-based apps because developers can actually customize the ad layout to better match their user experience.

Get Started

        import com.freestar.android.ads.NativeAd;
        ...


        NativeAd nativeAd = new NativeAd(this);  //this Activity
        nativeAd.setTemplate( com.freestar.android.ads.NativeAd.TEMPLATE_SMALL );  //or NativeAd.TEMPLATE_MEDIUM
        nativeAd.setNativeAdListener(...);
        nativeAd.loadAd( adRequest, placement);
        //Note: you may pass in a "placement" parameter in loadAd but it requires prior remote staff setup
        

When the native ad is ready, the onNativeAdLoaded callback will occur.

    @Override
    public void onNativeAdLoaded(View nativeAd, String placement) {

        //Note: Placement will be null if not specified in the original loadAd request.

        ((ViewGroup) findViewById(R.id.ad_container)).removeAllViews();
        ((ViewGroup) findViewById(R.id.ad_container)).addView(nativeAd);   //Add the native ad to your container!
   
    }

Another Way

Another way to display native ads is to put NativeAd directly into your XML layout as follows. NOTE: If you choose this route, then you do not need any code in java or kotlin since the ad will automatically be fetched and displayed. Also, if you need to set targeting parameters in the AdRequest, then please use the programmatic approach (above) and do not put the native ad in the layout.

            <com.freestar.android.ads.NativeAd xmlns:ads="http://schemas.android.com/apk/res-auto"
            ads:FreestarNativeAdPlacement="test_placement"
            ads:FreestarNativeAdTemplate="SMALL_TEMPLATE"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />


            <com.freestar.android.ads.NativeAd xmlns:ads="http://schemas.android.com/apk/res-auto"
            ads:FreestarNativeAdPlacement="test_placement_2"
            ads:FreestarNativeAdTemplate="MEDIUM_TEMPLATE"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

Native Templates

Freestar supports two native ad templates: small and medium

By default, you are not required to modify the native ad templates. However, if you want to modify, keep reading...

Modify Small and/or Medium Templates

If you wish to modify the templates, get a copy here.

Copy res/layout/* to your app/src/main/res/layout folder

Copy res/values/* to your app/src/main/res/values folder

Copy res/drawable/* to your app/src/main/res/drawable folder

You may modify ANY of the above files to better meet your needs. However, when you modify freestar_medium_native_template.xml or freestar_small_native_template.xml please do not remove any elements or rename any id's.

Custom Native Ad Templates

You can also have multiple Native Ads templates in your app. Simply copy freestar_medium_native_template.xml or freestar_small_native_template.xml and rename them to something different in your res/layout folder.

For example, let's say you want your app to display 2 different medium sized Native Ads. Copy freestar_medium_native_template.xml to your layout folder and rename it to my_other_medium_native_template.xml (example)

Modify my_other_medium_native_template.xml however you desire without removing any of the original tags/ids. You can modify the height or width, colors, margins/paddings, re-arrange things, change fonts, etc.

To utilize the original native ad:

NativeAd originalNativeAd = new NativeAd(context);
originalNativeAd.setTemplate( NativeAd.TEMPLATE_MEDIUM );

To display the new native ad:

NativeAd newNativeAd = new NativeAd(context);
newNativeAd.setTemplate( NativeAd.TEMPLATE_MEDIUM );
newNativeAd.setCustomTemplateResourceId( R.layout.my_other_medium_native_template );
⚠️ **GitHub.com Fallback** ⚠️