Friday 31 January 2020

How to implement Facebook audience network banner ad in android app using Android Studio

Its very simple to implement Facebook audience network banner ad in android app using Android Studio. We will complete it in 4 Steps.

1. Creating Android Studio Project and adding Facebook Audience network dependency in the project
2. Adding layout for banner add in layout xml file
3. Initializing Facebook Audience network SDK and finally showing banner ad
4. Adding internet permission in Manifest file

1. Creating Android Studio Project and adding Facebook Audience network dependency in the project

Create an android studio project for creating an android app and wait for project build completion.
Now open build.gradle (App level) file and add the following dependency between dependencies section.
(At the time of writing this article this is the latest version)
implementation 'com.facebook.android:audience-network-sdk:5.6.1'

2. Adding layout for banner add in layout xml file

To show banner ad in our application, it needs any container which can hold the banner ad. So here we are using LinearLayout as a container. and we are giving its id as bannerAdContainerLinearLayout.

<LinearLayout  
   android:layout_width="match_parent" 
   android:orientation="vertical"    
   android:layout_alignParentBottom="true"    
   android:id="@+id/bannerAdContainerLinearLayout" 
   android:layout_height="wrap_content">
</LinearLayout>

3. Initializing Facebook Audience network SDK and finally showing banner ad

To show ads using Facebook Audience network in our application it is necessary that it should be initialized correctly. To initialize the Facebook Audience network SDK write the below code into your MainActivity.java OnCreate method.

AudienceNetworkAds.initialize(this);

//below statements are optional and will be used when you are testing your app and its not in release
//for testing only
AdSettings.setTestAdType(AdSettings.TestAdType.IMG_16_9_APP_INSTALL);
//for testing only
AdSettings.getTestAdType();
//for testing only
dSettings.addTestDevice("Device hash id");
Now we initialized the SDK, its time to load banner ad and show the ad
// creating adView object with our placement ID
AdView adView = new AdView(this, "IMG_16_9_APP_INSTALL#YOUR_PLACEMENT_ID", AdSize.BANNER_HEIGHT_50);
// finding linear layout container for banner which we have defined in our layout file
LinearLayout bannerAdContainer = findViewById(R.id.bannerAdContainerLinearLayout);
//adding adView to container
bannerAdContainer.addView(adView);
// loading our ad into adview and showing it to the user
adView.loadAd();

4. Adding internet permission in Manifest file

We will be able to show any ad to the user if our app has internet access permission.
So we have it add this permission into out AndroidManifest.xml file.
Add this line between <manifest> starting and closing tag
<uses-permission android:name="android.permission.INTERNET"/>




Now launch the application and we will see a test banner ad from facebook


















No comments:

Post a Comment

How to implement Facebook audience network banner ad in android app using Android Studio

Its very simple to implement Facebook audience network banner ad in android app using Android Studio. We will complete it in 4 Steps. 1. ...