Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook ads integration into Android app

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.

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:attribute="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".PlayerActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/gray_background"
        android:orientation="vertical">

// facebook banner ad

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


       <developer.shivam.library.CrescentoContainer 
            android:id="@+id/crescentoContainer"
            android:layout_width="match_parent"
            android:layout_height="440dp"
            android:elevation="20dp"
            android:scaleType="centerCrop"
            attribute:curvature="50dp">

            <com.flaviofaria.kenburnsview.KenBurnsView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/wallpaper" />

            <com.cleveroad.play_widget.PlayLayout
                android:layout_marginTop="?attr/actionBarSize"
                android:id="@+id/ivPhoto"
                android:layout_width="260dp"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true" />

        </developer.shivam.library.CrescentoContainer>


        <include layout="@layout/song_info" />


    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

screenshot

like image 742
MadMax Avatar asked Jan 14 '18 22:01

MadMax


1 Answers

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.

like image 160
Najaf Ali Avatar answered Oct 03 '22 06:10

Najaf Ali