Facebook ads integration into Android app

myads mail picture myads mail · Jan 14, 2018 · Viewed 8.4k times · Source

I have problem adding a banner ad into this layout of my Android app. But this ad is not showing. The implementation in ActivityMain.java has no problem because when i add it other layouts, the banner starts showing.

screenshot

Answer

Najaf Ali picture Najaf Ali · Sep 30, 2018

How to implement Facebook ads in android, i will guide you how to impenitent Facebook banner ad, Interstitial Ad, native Ad am using data binding for view so if you wants to do the manually by using findviewbyid its up to you

1. Facebook Banner ad:

very first ad facebook audience network dependency in your gradle file:

 implementation 'com.facebook.android:audience-network-sdk:6.2.0'


 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActivityFamousPlacesBinding binding = ActivityFamousPlacesBinding.inflate(getLayoutInflater());
    View view = binding.getRoot();
    setContentView(view);

    com.facebook.ads.AdView adView = new com.facebook.ads.AdView(FamousPlacesActivity.this, getString(R.string.fb_placement_banner), AdSize.BANNER_HEIGHT_50);
    LinearLayout bannerContainer = findViewById(R.id.banner_container);
    /// here is am getting the banner view by enabling databinding you can 
    /// dobygetting the view like
    //  LinearLayout banner_container= findViewById(R.id.banner_container);
    binding.banner_container.addView(adView);
    adView.loadAd(adView.buildLoadAdConfig().withAdListener(new com.facebook.ads.AdListener() {
        @Override
        public void onError(Ad ad, AdError adError) {


        }

        @Override
        public void onAdLoaded(Ad ad) {


        }

        @Override
        public void onAdClicked(Ad ad) {

        }

        @Override
        public void onLoggingImpression(Ad ad) {


        }
    }).build());
}

after syncing dependency add this tag in mainfest file under application tag:

<activity android:name="com.facebook.ads.AudienceNetworkActivity"
          android:configChanges="keyboardHidden|orientation|screenSize"/>

for xml code write this :

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

for java class : keep in mind when you declare Adview Import should be of Facebook network:

For Facebook Interstitial ad ,

declare these variables first :

 ///in concreate method laod the ads first
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_multi_page);
    // load the interstitial ads
    loadfanads();
}

///Normally we show Interstitial on backpressed it up to you where you wants to show
/// on button click or on backpressed button of any activity
private InterstitialAd interstitialAd;
private void loadfanads() {

    interstitialAd = new InterstitialAd(this, getString(R.string.fb_placement_Interstitial));
    InterstitialAdListener madlistner = new InterstitialAdListener() {
        @Override
        public void onInterstitialDisplayed(Ad ad) {

        }

        @Override
        public void onInterstitialDismissed(Ad ad) {

            //// on Interstitial dismissed
            Intent out = new Intent();
            out.putExtra(ScanConstants.SAVE_PDF, Boolean.TRUE);
            setResult(RESULT_OK, out);
            finish();
        }

        @Override
        public void onError(Ad ad, AdError adError) {

            /// on error ad loading

        }

        @Override
        public void onAdLoaded(Ad ad) {

        }

        @Override
        public void onAdClicked(Ad ad) {
            //// on ad clicked
            Intent out = new Intent();
            out.putExtra(ScanConstants.SAVE_PDF, Boolean.TRUE);
            setResult(RESULT_OK, out);
            finish();
        }

        @Override
        public void onLoggingImpression(Ad ad) {

        }
    };
    interstitialAd.loadAd(interstitialAd.buildLoadAdConfig().withAdListener(madlistner).build());


}

to show ad you can implement this method i have show ad in after back pressed but you can show as your choice:

     //// on button click view
public void saveNow(View view) {

     if (interstitialAd.isAdLoaded() && interstitialAd!=null && !interstitialAd.isAdInvalidated()){
         interstitialAd.show();
     }
     else {

         Intent out = new Intent();
         out.putExtra(ScanConstants.SAVE_PDF, Boolean.TRUE);
         setResult(RESULT_OK, out);
         finish();
     }
}

for Facebook Native banner ADS :

XML CODE JUST ADD THIS LAYOUT:

<RelativeLayout
    android:id="@+id/templateContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" />

 for jave code :

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    nativeBannerAd = new NativeBannerAd(this, getString(R.string.fb_placement_native_banner));
    NativeAdListener adlistner = new NativeAdListener() {
        @Override
        public void onMediaDownloaded(Ad ad) {

        }

        @Override
        public void onError(Ad ad, AdError adError) {

        }

        @Override
        public void onAdLoaded(Ad ad) {

            View adView = NativeBannerAdView.render(MainActivity.this, nativeBannerAd, NativeBannerAdView.Type.HEIGHT_120);
            scarymainBinding.templateContainer.addView(adView);


        }

        @Override
        public void onAdClicked(Ad ad) {

        }

        @Override
        public void onLoggingImpression(Ad ad) {

        }
    };

    nativeBannerAd.loadAd(
            nativeBannerAd.buildLoadAdConfig()
                    .withAdListener(adlistner)
                    .build());

}

  

if you have any query about this you can ask me! hope you will enjoy this.