How to add facebook ads in android studio

How to add facebook ads in android studio

Add the following dependence in build.gradle (Module app) level file.
compile 'com.facebook.android:audience-network-sdk:6.+'
<uses-permission android:name=”android.permission.INTERNET”></uses-permission>
 
 
 
create java class
 
extent aplication
 
@Override
public void onCreate() {
    super.onCreate();
    AudienceNetworkAds.initialize(this);
}
 
 
 
add your java class in mnifest under aplications
android:name=".MyFasbookads"
add layout
<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"
        />
mainactivity
private AdView adView;
adView = new AdView(this, "IMG_16_9_APP_INSTALL#YOUR_PLACEMENT_ID", AdSize.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();
@Override
protected void onDestroy() {
    if (adView != null) {
        adView.destroy();
    }
    super.onDestroy();
}

Leave a Comment

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