VOOZH about

URL: https://www.geeksforgeeks.org/android/how-to-integrate-facebook-audience-network-fan-native-ads-in-android/

⇱ How to Integrate Facebook Audience Network (FAN) Native Ads in Android? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Integrate Facebook Audience Network (FAN) Native Ads in Android?

Last Updated : 23 Jul, 2025

In order to earn money from the Android app or game, there are many ways such as in-App Purchases, Sponsorship, Advertisements, and many more. But there is another popular method to earn money from the Android app is by integrating a third-party advertisement e.g known as Facebook Audience Network (FAN). Facebook Audience Network is designed to help monetize with the user experience in mind. By using high-value formats, quality ads, and innovative publisher tools it helps to grow the business while keeping people engaged.

Why Facebook Audience Network?

  • Facebook Audience Network is one of the best alternatives for Google Admob to monetize the Android or IOS App.
  • Minimum Payout is $100
  • Wide Range of Ad Formats
  • Maximum Fill Rates
  • High eCPM(Effective Cost Per Mille)
  • Quality Ads
  • Personalized Ads

Formats of Facebook Audience Network

There are mainly five types of flexible, high-performing format available in Facebook Audience Network

  • Native: Ads that you design to fit the app, seamlessly
  • Interstitial: Full-screen ads that capture attention and become part of the experience.
  • Banner: Traditional formats in a variety of placements.
  • Rewarded Video: An immersive, user-initiated video ad that rewards users for watching.
  • Playables: A try-before-you-buy ad experience allowing users to preview a game before installing.

In this article let’s integrate Facebook Audience Network Native Ads in the Android app.

Native Ads:

A Native Ads ad is used to build a customized experience for the ads of the app. The eCPM (Effective Cost Per Mille) of Native Ads ads are relatively higher than banner ads and also leads to higher CTR(Click Through Rate) which results in more earning from the app. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language. 

πŸ‘ Sample gif

Approach

Step 1: Creating a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that choose Java as language though we are going to implement this project in Java language.

Step 2: Before going to the coding section first do some pre-task

  • Go to app > res > values > colors.xml file and set the colors for the app.
  • Go to Gradle Scripts > build.gradle (Module: app) section and import the following dependencies and click the β€œsync Now” on the above pop up.

implementation 'com.facebook.android:audience-network-sdk:5.+'
 

<uses-permission android:name="android.permission.INTERNET" />
 

Step 3: Designing the UI 

  • Create a new layout, which contains the layout of the native ad. Go to app > res > layout > right-click > New > Layout Resource File and name the file as fan_native_ad_layout.
  • Below is the code for the fan_native_ad_layout.xml file. Comments are added inside the code to understand the code in more detail.
  • Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.

Step 4: Working with MainActivity.java file

  • Open the MainActivity.java file there within the class, first of all, create the object of NativeAdLayout, LinearLayout, NativeAd class.

  // creating NativeAdLayout object

  private NativeAdLayout nativeAdLayout;

  // creating  LinearLayout object

  private LinearLayout adView;

  // creating  NativeAd object

  private NativeAd nativeAd;

  • Now inside the onCreate() method, initialize the Facebook Audience Network SDK.

// initializing the Audience Network SDK

AudienceNetworkAds.initialize(this);

  • Create a private void loadNativeAd() method outside onCreate() method and define it.
  • Inside loadNativeAd() add ad listener to the native ad and show the relative Toast Message.
  • And inside onAdLoaded() method call the inflateAd() ,which we create later.

  // loading native Ad

  private void loadNativeAd() {

       // initializing nativeAd object

       nativeAd = new NativeAd(this, "YOUR_PLACEMENT_ID");

       // creating  NativeAdListener

       NativeAdListener nativeAdListener = new NativeAdListener() {

           @Override

           public void onMediaDownloaded(Ad ad) {

               // showing Toast message

               Toast.makeText(MainActivity.this, "onMediaDownloaded", Toast.LENGTH_SHORT).show();

           }

           @Override

           public void onError(Ad ad, AdError adError) {

               // showing Toast message

               Toast.makeText(MainActivity.this, "onError", Toast.LENGTH_SHORT).show();

           }

           @Override

           public void onAdLoaded(Ad ad) {

               // showing Toast message

               Toast.makeText(MainActivity.this, "onAdLoaded", Toast.LENGTH_SHORT).show();

               if (nativeAd == null || nativeAd != ad) {

                   return;

               }

               // Inflate Native Ad into Container

               inflateAd(nativeAd);

           }

           @Override

           public void onAdClicked(Ad ad) {

               // showing Toast message

               Toast.makeText(MainActivity.this, "onAdClicked", Toast.LENGTH_SHORT).show();

           }

           @Override

           public void onLoggingImpression(Ad ad) {

               // showing Toast message

               Toast.makeText(MainActivity.this, "onLoggingImpression", Toast.LENGTH_SHORT).show();

           }

       };

       // Load an ad

       nativeAd.loadAd(

               nativeAd.buildLoadAdConfig()

                       .withAdListener(nativeAdListener)

                       .build());

   }

Note:

  • Replace "YOUR_PLACEMENT_ID" with your own placement id to show real ads.
  • Facebook does not provide any test ids, so you have to create FAN account and then create new placement id and then add your device test AD id in the FAN to get ads in your app.
  • Now create a void inflateAd, which will inflate the views from fan_native_ad_layout.xml and activity_main.xml file and show the ad to the user.

  // inflating the Ad

  void inflateAd(NativeAd nativeAd) {

       // unregister the native Ad View

       nativeAd.unregisterView();

       // Add the Ad view into the ad container.

       nativeAdLayout = findViewById(R.id.native_ad_container);

       LayoutInflater inflater = LayoutInflater.from(MainActivity.this);

       // Inflate the Ad view.

       adView = (LinearLayout) inflater.inflate(R.layout.fan_native_ad_layout, nativeAdLayout, false);

       // adding view

       nativeAdLayout.addView(adView);

       // Add the AdOptionsView

       LinearLayout adChoicesContainer = findViewById(R.id.ad_choices_container);

       AdOptionsView adOptionsView = new AdOptionsView(MainActivity.this, nativeAd, nativeAdLayout);

       adChoicesContainer.removeAllViews();

       adChoicesContainer.addView(adOptionsView, 0);

       // Create native UI using the ad metadata.

       MediaView nativeAdIcon = adView.findViewById(R.id.native_ad_icon);

       TextView nativeAdTitle = adView.findViewById(R.id.native_ad_title);

       MediaView nativeAdMedia = adView.findViewById(R.id.native_ad_media);

       TextView nativeAdSocialContext = adView.findViewById(R.id.native_ad_social_context);

       TextView nativeAdBody = adView.findViewById(R.id.native_ad_body);

       TextView sponsoredLabel = adView.findViewById(R.id.native_ad_sponsored_label);

       Button nativeAdCallToAction = adView.findViewById(R.id.native_ad_call_to_action);

       // Setting  the Text.

       nativeAdTitle.setText(nativeAd.getAdvertiserName());

       nativeAdBody.setText(nativeAd.getAdBodyText());

       nativeAdSocialContext.setText(nativeAd.getAdSocialContext());

       nativeAdCallToAction.setVisibility(nativeAd.hasCallToAction() ? View.VISIBLE : View.INVISIBLE);

       nativeAdCallToAction.setText(nativeAd.getAdCallToAction());

       sponsoredLabel.setText(nativeAd.getSponsoredTranslation());

       // Create a list of clickable views

       List<View> clickableViews = new ArrayList<>();

       clickableViews.add(nativeAdTitle);

       clickableViews.add(nativeAdCallToAction);

       // Register the Title and  button to listen for clicks.

       nativeAd.registerViewForInteraction(

               adView, nativeAdMedia, nativeAdIcon, clickableViews);

   }

  • Below is the complete code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.

Output:

Remember the point again: Facebook does not provide any test ids, so you have to create a FAN account and then create a new placement id and then add your device test AD id in the FAN to get ads in your app. 

Comment
Article Tags:
Article Tags:

Explore