![]() |
VOOZH | about |
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.
There are mainly five types of flexible, high-performing format available in Facebook Audience Network
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 gifStep 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
implementation 'com.facebook.android:audience-network-sdk:5.+'
<uses-permission android:name="android.permission.INTERNET" />
Step 3: Designing the UI
Step 4: Working with MainActivity.java file
// creating NativeAdLayout object
private NativeAdLayout nativeAdLayout;
// creating LinearLayout object
private LinearLayout adView;
// creating NativeAd object
private NativeAd nativeAd;
// initializing the Audience Network SDK
AudienceNetworkAds.initialize(this);
// 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.
// 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);
}
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.