I am implementing code for native ads that is given by google. That is working okay with google
ad unit id ADMOB_AD_UNIT_ID = "ca-app-pub-3940256099942544/2247696110"
But when I replace with my id its not showing ad and execute onAdFailedToLoad method and got Failed to load native ad: 0 error .
I tried two different ad unit id but it didn't load ad.
so what should I do??please help me
I tried below code
final LayoutInflater li = (LayoutInflater) appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
AdLoader.Builder builder = new AdLoader.Builder(appContext, Global.ADMOB_AD_UNIT_ID);
builder.forContentAd(new NativeContentAd.OnContentAdLoadedListener() {
#Override
public void onContentAdLoaded(NativeContentAd ad) {
//viewHolder.rlnativead.setVisibility(View.VISIBLE);
NativeContentAdView adView = (NativeContentAdView)li
.inflate(R.layout.ad_content, null);
populateContentAdView(ad, adView);
viewHolder.fl_adplaceholder.removeAllViews();
viewHolder.fl_adplaceholder.addView(adView);
}
});
AdLoader adLoader = builder.withAdListener(new AdListener() {
#Override
public void onAdFailedToLoad(int errorCode) {
// Toast.makeText(appContext, "Failed to load native ad: "
// + errorCode, Toast.LENGTH_SHORT).show();
Log.e("Call List adapter","Failed to load native ad: "+errorCode);
}
}).build();
adLoader.loadAd(new AdRequest.Builder().build());
Please add at least one testing device and try, It may be help you like following,
AdRequest adRequest = new AdRequest.Builder() .addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID") .build();
this could be lately answered but expecting to help for others having similiar problem.One of the major cause for this is incorrect dimension
In my case before it was
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.ads.NativeExpressAdView android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="#string/native_ad_id"
ads:adSize="320x250"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto">
</com.google.android.gms.ads.NativeExpressAdView>
I was making the mistake in giving the adSize ,so i just changed the add size with the below dimension and got fixed. Thanks.
ads:adSize="320x250"
Related
I'm implementing google native ads on android.Most of the code I copy/pasted from this example
The only difference is I request bunch of ads instead of one ad.Here is load function:
public void loadAds(String unitId,int count) {
if (nativeAds.size()>0) {
for(UnifiedNativeAd ad: nativeAds)
ad.destroy();
}
nativeAds =new ArrayList();
//load
AdLoader.Builder builder = new AdLoader.Builder(context, unitId);
builder.forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
// OnUnifiedNativeAdLoadedListener implementation.
#Override
public void onUnifiedNativeAdLoaded(UnifiedNativeAd nativeAd) {
// You must call destroy on old ads when you are done with them,
// otherwise you will have a memory leak.
//nativeAd = unifiedNativeAd;
int numAds =nativeAds.size();
int insertTo=numAds==0? firstAdPosition : firstAdPosition +numAds*(AdStep +1);
CategoryData item = new CategoryData();
item.type = ItemData.TYPE_CATEGORIES_NATIVE_AD;
if (insertTo<data.size())
data.add(insertTo, item);
item = new CategoryData();
item.type = ItemData.TYPE_CATEGORIES_SEPARATOR;
if (insertTo<data.size())
data.add(insertTo+1, item);
nativeAds.add(nativeAd);
Log.d("nativead","got ad body "+nativeAds.size()+":"+nativeAd.getBody());
notifyItemRangeInserted(insertTo,2);
}
});
VideoOptions videoOptions = new VideoOptions.Builder()
.setStartMuted(true)
.build();
NativeAdOptions adOptions = new NativeAdOptions.Builder()
.setVideoOptions(videoOptions)
.build();
builder.withNativeAdOptions(adOptions);
AdLoader adLoader = builder.withAdListener(new AdListener() {
#Override
public void onAdFailedToLoad(int errorCode) {
Log.d("nativead","error code " +errorCode);
/*
Toast.makeText(context, "Failed to load native ad: "
+ errorCode, Toast.LENGTH_SHORT).show();
*/
}
}).build();
AdRequest adRequest;
// get unpersonalized ads if needed
if(AppModel.getAppData().gdpr.eea && AppModel.getAppData().gdpr.consentStatus!= ConsentStatus.PERSONALIZED) {
Bundle extras = new Bundle();
extras.putString("npa", "1");
adRequest = new AdRequest.Builder().addNetworkExtrasBundle(AdMobAdapter.class,extras).build();
} else {
adRequest = new AdRequest.Builder().build();
}
adLoader.loadAds(adRequest,count);
//adLoader.loadAd(adRequest);
}
First time it loads ok, but if I restart the app it throws a bunch of errors:
I/Ads: Received log message: The ad request returned a no fill for the particular slot. The error log below that says 'Malformed native JSON response' is a benign warning that will be removed in a future SDK release.
Also error code 0 is passed into onAdFailedToLoad.If I clear app's data banners are loaded again but again only for single run
As you mentioned, in the comment above, you're testing it on real ad units (which is quite dangerous as you might get blocked for that).
You're getting NO_FILL, which means there is no inventory left to serve in the AdMob backend.
You can find out more about this issue (not an issue, we can call it a "thing") here.
To get proper results, always test with test ads, so that you get maximum fill rate for the ads while testing.
You'll find the ad unit ID's for test ads on this page.
And, if you still get NO_FILL error a lot in production, try using mediation to get ads from multiple ad sources. You'll never get 100% fill rate for your ads, though, so make sure you code accordingly to handle the NO_FILL.
Also, one more reminder, do not use live ads to test the app, you might get blocked by Admob/Google for doing that.
i try everything in stackoverflow but ads not show. banner and other working but native not working please help me.
inal NativeExpressAdView mAdView2 = (NativeExpressAdView) findViewById(R.id.adView2);
// Set its video options.
mAdView2.setVideoOptions(new VideoOptions.Builder()
.setStartMuted(true)
.build());
// The VideoController can be used to get lifecycle events and info about an ad's video
// asset. One will always be returned by getVideoController, even if the ad has no video
// asset.
mVideoController = mAdView2.getVideoController();
mVideoController.setVideoLifecycleCallbacks(new VideoController.VideoLifecycleCallbacks() {
#Override
public void onVideoEnd() {
Log.d(LOG_TAG, "Video playback is finished.");
super.onVideoEnd();
}
});
// Set an AdListener for the AdView, so the Activity can take action when an ad has finished
// loading.
mAdView2.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
mAdView2.setVisibility(View.VISIBLE);
if (mVideoController.hasVideoContent()) {
Log.d(LOG_TAG, "Received an ad that contains a video asset.");
} else {
Log.d(LOG_TAG, "Received an ad that does not contain a video asset.");
}
}
});
mAdView2.loadAd(adRequest);
this activity code
this is xml code . please help me.
<com.google.android.gms.ads.NativeExpressAdView
android:id="#+id/adView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="ca-app-pub-000000"
ads:adSize="300x150"
android:visibility="gone"
>
</com.google.android.gms.ads.NativeExpressAdView>
Logcat report
You just need to wait a little. Your ad id is newly created so it will take some time to fetch ads from google servers.
It also happened to me that error "fail to load ad 0" but there is no problem just wait for sometimes like an hour or 2 or even 5 hours just wait you will get the ads.
I am using the following view in my activity xml.
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-6844894412719021~8904567900">
</com.google.android.gms.ads.AdView>
Your adUnitId seems to be incorrect. Did you get it via email?
You should be using following app id (replace ~ with /)
ads:adUnitId="ca-app-pub-6844894412719021/8904567900">
Try to add to your xml:
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
and to gradle:
compile 'com.google.android.gms:play-services-ads:9.8.0'
It looks like you are using app_id instead of banner_id.
<string name="app_id">ca-app-pub-xxxxxxxxxxxxxxxx~xxxxxxxxxx</string>
<string name="banner_id">ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx</string>
these two values are different from each other.
Check if you have latest versions of adMob and GooglePlayServices lib.
When you try to show banner inside of svg images then care about it are you using smart banner or banner it may effect, sometimes you implement the code right but when you try to show toast in ad loaded like this.
adView2.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
Toast.makeText(App_Mainpage.this, "add is loaded", Toast.LENGTH_SHORT).show();
adView2.setVisibility(View.VISIBLE);
}
#Override
public void onAdFailedToLoad(int errorCode) {
}
#Override
public void onAdOpened() {
}
#Override
public void onAdLeftApplication() {
}
#Override
public void onAdClosed() {
}
});
AdRequest adRequest = new AdRequest.Builder().build();
adView2.loadAd(adRequest);
if it shows you in toast the ad is loaded but not showing when you testing, then actually you are implementing Smart_banner that takes large space to show, so use Banner Just it will show then, i solve it by change this thing.
then i changed to smart banner .
I am using native Admob ad for android app but everytime i am getting
Failed to load ad : 0
when i used addTestDevice(deviceId) is working fine.
My Code:
private void refreshAd(boolean requestAppInstallAds, boolean requestContentAds, final LinearLayout mLayout) {
AdLoader adLoader = new AdLoader.Builder(mContext, Config.ADMOB_AD_UNIT_ID)
.forAppInstallAd(new NativeAppInstallAd.OnAppInstallAdLoadedListener() {
#Override
public void onAppInstallAdLoaded(NativeAppInstallAd appInstallAd) {
// Show the app install ad.
NativeAppInstallAdView adView = (NativeAppInstallAdView) ((AppCompatActivity) mContext).getLayoutInflater()
.inflate(R.layout.ad_app_install, null);
populateAppInstallAdView(appInstallAd, adView);
mLayout.removeAllViews();
mLayout.addView(adView);
}
})
.forContentAd(new NativeContentAd.OnContentAdLoadedListener() {
#Override
public void onContentAdLoaded(NativeContentAd contentAd) {
// Show the content ad.
}
})
.withAdListener(new AdListener() {
#Override
public void onAdFailedToLoad(int errorCode) {
// Handle the failure by logging, altering the UI, etc.
Log.d("Opennaukri","errorCode = "+errorCode);
}
})
.withNativeAdOptions(new NativeAdOptions.Builder()
// Methods in the NativeAdOptions.Builder class can be
// used here to specify individual options settings.
.build())
.build();
adLoader.loadAd(new AdRequest.Builder().build());
// .addTestDevice("E9990FA9258AD0601F492495AC3F15EB").build());
// // Check the LogCat to get your test device ID
// .addTestDevice("B7BEE8B7CDBAC269CBB598F9DB6C4769")
// Check the LogCat to get your test device ID
}
private void populateAppInstallAdView(NativeAppInstallAd nativeAppInstallAd,
NativeAppInstallAdView adView) {
adView.setHeadlineView(adView.findViewById(R.id.appinstall_headline));
// adView.setImageView(adView.findViewById(R.id.appinstall_image));
adView.setBodyView(adView.findViewById(R.id.appinstall_body));
adView.setCallToActionView(adView.findViewById(R.id.appinstall_call_to_action));
adView.setIconView(adView.findViewById(R.id.appinstall_app_icon));
adView.setPriceView(adView.findViewById(R.id.appinstall_price));
adView.setStarRatingView(adView.findViewById(R.id.appinstall_stars));
adView.setStoreView(adView.findViewById(R.id.appinstall_store));
// Some assets are guaranteed to be in every NativeAppInstallAd.
((TextView) adView.getHeadlineView()).setText(nativeAppInstallAd.getHeadline());
((TextView) adView.getBodyView()).setText(nativeAppInstallAd.getBody());
((TextView) adView.getCallToActionView()).setText(nativeAppInstallAd.getCallToAction());
((ImageView) adView.getIconView()).setImageDrawable(nativeAppInstallAd.getIcon()
.getDrawable());
// Some aren't guaranteed, however, and should be checked.
if (nativeAppInstallAd.getPrice() == null) {
adView.getPriceView().setVisibility(View.INVISIBLE);
} else {
adView.getPriceView().setVisibility(View.VISIBLE);
((TextView) adView.getPriceView()).setText(nativeAppInstallAd.getPrice());
}
if (nativeAppInstallAd.getStore() == null) {
adView.getStoreView().setVisibility(View.INVISIBLE);
} else {
adView.getStoreView().setVisibility(View.VISIBLE);
((TextView) adView.getStoreView()).setText(nativeAppInstallAd.getStore());
}
if (nativeAppInstallAd.getStarRating() == null) {
adView.getStarRatingView().setVisibility(View.INVISIBLE);
} else {
((RatingBar) adView.getStarRatingView())
.setRating(nativeAppInstallAd.getStarRating().floatValue());
adView.getStarRatingView().setVisibility(View.VISIBLE);
}
// Assign native ad object to the native view.
adView.setNativeAd(nativeAppInstallAd);
}
Steps to see ads in your app:
1) add internet permision in your manifest file
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
2) In the Gradle file insert this line within dependencies
compile 'com.google.android.gms:play-services-ads:7.5.0'
3) now we need to display the add so we add these few lines in the layout
anyLayout xmlns:android...
xmlns:ads = "http://schemas.android.com/apk/res-auto"
...
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-xxxxxxxxxxxxxxx/xxxxxxxxx.">
</com.google.android.gms.ads.AdView>
4) finally we need to load the ad in the layout, which only requires 3 lines:
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
If you run the app from the emulator or you check in the layout editor (in android studio) you will see how you ad will look like. If everythings goes right in the layout editor you will see a white banner with "ads by google" (or something like that) and if you run the app in the emulator a dummy banner will show up.
Once you build the apk (signed or unsigned) and install it on a device you won't see any ad yet because google admob need some time between you register a new app to deliver ads and when the ads start to appear (<12h).
Hope this helps!
I had success with the following code:
private LinearLayout mAdContainer;
private NativeExpressAdView mAdmobAdView;
private AdRequest mAdmobAdRequest;
[...]
private void loadAds() {
// find out the width of the device in dp
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
float deviceWidthInDp = displayMetrics.widthPixels / displayMetrics.density;
int adWidth = (int)(deviceWidthInDp);
mAdContainer.removeAllViews();
mAdmobAdView = new NativeExpressAdView(this);
mAdmobAdView.setAdSize(new AdSize(adWidth, 250)); // AdSize(width, height), height ranges from 80 to 1200
mAdContainer.addView(mAdmobAdView);
mAdmobAdView.setAdUnitId(getString(R.string.admob_id)); // set your admob id
mAdmobAdRequest = new AdRequest.Builder()
.build();
mAdmobAdView.loadAd(mAdmobAdRequest);
}
Some user intentionally try to click banner ads many times.Due to this we face problem of account suspension or termination. Does anyone know how to stop the ad being loading if it cross some limit(for example 3).
AdView adView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.setRequestAgent("android_studio:ad_template").build();
adView.loadAd(adRequest);
if(currentbannerclick>3)
{
// some code to not load the ad.
}
LinearLayout id=container
AdView id=adView
if(currentbannerclick>3)
container.removeView(adView);
Thank you everyone for your answer.
You can identify if Ad is clicked using Activity life-cycle callbacks. you can then find out how much time user clicked your Ad and call adView.loadAd(adRequest); only if the user clicked your Ad less than your threshold.
You can attach an AdListener to your AdView and increase your click counter in the onAdLoaded or onAdOpened methods. More info here: https://developers.google.com/android/reference/com/google/android/gms/ads/AdListener#public-methods
This should work:
private void loadAd() {
// This is a one element array because it needs to be declared final
// TODO: you should probably load the default value from somewhere because of activity restarts
final int[] currentBannerClick = {0};
final AdView adView = (AdView) findViewById(R.id.adView);
adView.setAdListener(new AdListener() {
#Override
public void onAdOpened() {
super.onAdOpened();
currentBannerClick[0]++;
if (currentBannerClick[0] > 3) {
adView.setVisibility(View.INVISIBLE);
adView.destroy();
}
// TODO: save currentBannerClick[0] somewhere, see previous TODO comment
}
});
if (currentBannerClick[0] <= 3) {
AdRequest adRequest = new AdRequest.Builder().addTestDevice(YOUR_DEVICE_ID).build();
adView.setVisibility(View.VISIBLE);
adView.loadAd(adRequest);
} else {
adView.setVisibility(View.INVISIBLE);
}
}
You can also limit number of Ads displayed for user in AdMob System. You can set limit of 3 ads per user per minut, hour or day.