Categories

Follow Us @web_learners

Saturday, April 25, 2020

Facebook Banner Ads Integration in Android App Part 2 | Admob Alternative Ad network

Hi Everyone, Today I am going to show you how to integrate Facebook SDK and Banner ads in the android app in the android studio.

In the Last post, I have shown how to create a Facebook business account for monetization android app. Also, we have added our app on Facebook, Added payment method, and created a banner ad unit. If you have not followed my Facebook Banner Ads integration in Android App Part 1 yet, then first complete part 1 then come to follow this post part 2. So let's start.


First, open your project with Android studio, Then go to your "build.gradle(Module: App)" file, then paste below facebook ad network dependency in the dependencies section.


Code:


Now in the "MainActivity.xml" file initialize the Facebook Ad network SDK.

Code:



Then move to the layout file "activity_main.xml" file and paste the below code, if you want to show your ads in the top of the page then paste this code above, and if you want to show your ads in the bottom of the page then paste below code bottom in the "activity_main.xml" file.



Code:


Now go to the "MainActivity.java" file, then take an AdView private variable
Code:


Then you need to paste the below code after initializing SDK code below:


Code:


To get test ads, add "IMG_16_9_APP_INSTALL#" to your placement id. Remove this when your app is ready to serve real ads.

Now you can run your project on Emulator or on your real devices. You can see there will be test ads.

if you want to run your project on Emulator, then add the below line. You will get the hashed ID that is printed to logcat when you first make a request to load an ad on a device.

Code:


After testing done, you need to remove that line also. So, before going to live your final code will be


That's it, We have successfully integrated Facebook Ads in our App. If you face any problem in coding, you can comment below. We will try our best to help you. Thank you.

1 comment:

  1. i have been trying to implement this but ... it steel show some error...with this code... please please help me........
    // // Initialize the Audience Network SDK
    AudienceNetworkAds.initialize(this);

    adView = new AdView(this, "IMG_16_9_APP_INSTALL#YOUR_PLACEMENT_ID", BANNER_HEIGHT_50);

    // Find the Ad Container
    LinearLayout adContainer = (LinearLayout) findViewById(R.id.banner_container);

    // Add the ad view to your activity layout
    adContainer.addView(adView);

    // Request an ad
    adView.loadAd();

    ReplyDelete