![]() |
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 Banner ads in the Android app. Banner ad is a rectangular image or text ad which occupies a small space in the app layout. Banner ad is easy to implement and it doesn't affect user interface and increases the revenue gradually.
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.
implementation 'com.facebook.android:audience-network-sdk:5.+'
<uses-permission android:name="android.permission.INTERNET"/>
In the activity_main.xml file, there are simply three Buttons is used. So whenever the user clicked on that button the desired Banner Ad will pop up. To contain the Banner Ad a LinearLayout is added inside the XML file. Here is the code for the activity_main.xml file.
// Creating objects of Button class
Button fbBanner_50, fbBanner_90, fbBanner_250;
// link those objects with their respective id's that we have given in activity_main.xml file
fbBanner_50 = (Button)findViewById(R.id.banner_50);
fbBanner_90 = (Button)findViewById(R.id.banner_90);
fbBanner_250 = (Button)findViewById(R.id.banner_250);
// initializing the Audience Network SDK
AudienceNetworkAds.initialize(this);
private void showBanner(AdSize adSize)
{
// creating object of AdView
AdView bannerAd;
// initializing AdView Object
// AdView Constructor Takes 3 Arguments
// 1)Context
// 2)Placement Id
// 3)AdSize
bannerAd = new AdView(this, "IMG_16_9_APP_INSTALL#YOUR_PLACEMENT_ID",adSize);
// Creating and initializing LinearLayout which contains the ads
LinearLayout adLinearContainer = (LinearLayout) findViewById(R.id.fb_banner_ad_container);
// removing the views inside linearLayout
adLinearContainer.removeAllViewsInLayout();
// adding ad to the linearLayoutContainer
adLinearContainer.addView(bannerAd);
// loading Ad
bannerAd.loadAd();
}
Note: Replace "IMG_16_9_APP_INSTALL#YOUR_PLACEMENT_ID" with its own placement id to show real ads.
// click listener to show Banner_50 Ad
fbBanner_50.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showBanner(AdSize.BANNER_HEIGHT_50);
}
});
// click listener to show Banner_90 Ad
fbBanner_90.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showBanner(AdSize.BANNER_HEIGHT_90);
}
});
// click listener to show Banner_250 Ad
fbBanner_250.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showBanner(AdSize.RECTANGLE_HEIGHT_250);
}
});
// banner AdListener
bannerAd.setAdListener(new AdListener() {
@Override
public void onError(Ad ad, AdError adError) {
// Showing a toast message
Toast.makeText(MainActivity.this, "onError", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdLoaded(Ad ad) {
// Showing a toast message
Toast.makeText(MainActivity.this, "onAdLoaded", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdClicked(Ad ad) {
// Showing a toast message
Toast.makeText(MainActivity.this, "onAdClicked", Toast.LENGTH_SHORT).show();
}
@Override
public void onLoggingImpression(Ad ad) {
// Showing a toast message
Toast.makeText(MainActivity.this, "onLoggingImpression", Toast.LENGTH_SHORT).show();
}
});