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>
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
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:
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();
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With