Facebook Banner Ads Integrate in App

In this article we will learn about the integration of Facebook banner ads in our app. First of all create an account on Facebook audience network 

After create an account generate ads and get unit id of ad and then integrate it in android app.

There are following steps. 

  1. Create unit ids of Facebook ads.
  2. Add permission in Manifest.xml
  3. Add repository
  4. Add Dependency
  5. Add meta data in Manifest.xml
  6. Paste layout Code.
  7. Paste java code.

Manifest.xml

Permission

This permission allow to connect your app with internet.

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

 

Meta Data

In meta data add your facebook app id. Without app id ads not shown in your app.

<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>

 

Build.Gradule

Repository 

mavenCentral()

Dependency

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

 

activity.xml

 <LinearLayout
android:id="@+id/banner_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
/>

Activity.java

 

private AdView adView;


        //ads

        AudienceNetworkAds.initialize(this);

        adView = new AdView(this, "BANNER AD ID", AdSize.BANNER_HEIGHT_50);
        LinearLayout adContainer = (LinearLayout) findViewById(R.id.banner_container);
        adContainer.addView(adView);


       

For Testing Paste this code

// EMulator testing remove this 
if (BuildConfig.DEBUG) 
{ AdSettings.setTestMode(true); } 
adView.loadAd();

Enjoy!!

Leave a Reply

Your email address will not be published. Required fields are marked *