I have read everything about how to implement the Custom Events in the Admob Mediation.
I have added the full packaged class name and everything is set in the Admob portal.
This is the class implementation done
public class CustomEvent implements CustomEventBanner, AdListener{
private CustomEventBannerListener bannerListener;
private AdView adView;
#Override
public void requestBannerAd(final CustomEventBannerListener listener,
final Activity activity,
String label,
String serverParameter,
AdSize adSize,
MediationAdRequest mediationAdRequest) {
// Keep the custom event listener for use later.
this.bannerListener = listener;
// Determine the best ad format to use given the adSize. If the adSize
// isn't appropriate for any format, an ad will not fill.
AdSize bestAdSize = adSize = adSize.findBestSize(
AdSize.BANNER,
AdSize.IAB_BANNER,
AdSize.IAB_LEADERBOARD,
AdSize.IAB_MRECT,
AdSize.IAB_WIDE_SKYSCRAPER);
if (bestAdSize == null) {
listener.onFailedToReceiveAd();
return;
}
// Initialize an AdView with the bestAdSize and the publisher ID.
// The publisher ID is the server parameter that you gave when creating
// the custom event.
this.adView = new AdView(activity, bestAdSize, serverParameter);
// Set the listener to register for events.
this.adView.setAdListener(this);
// Generate an ad request using custom targeting values provided in the
// MediationAdRequest.
AdRequest adRequest = new AdRequest()
.setBirthday(mediationAdRequest.getBirthday())
.setGender(mediationAdRequest.getGender())
.setKeywords(mediationAdRequest.getKeywords())
.setLocation(mediationAdRequest.getLocation());
if (mediationAdRequest.isTesting()) {
adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
}
// Load the ad with the ad request.
this.adView.loadAd(adRequest);
}
#Override
public void onReceiveAd(Ad ad) {
this.bannerListener.onReceivedAd(this.adView);
}
#Override
public void onFailedToReceiveAd(Ad ad, ErrorCode errorCode) {
this.bannerListener.onFailedToReceiveAd();
}
#Override
public void onPresentScreen(Ad ad) {
this.bannerListener.onClick();
this.bannerListener.onPresentScreen();
}
#Override
public void onDismissScreen(Ad ad) {
this.bannerListener.onDismissScreen();
}
#Override
public void onLeaveApplication(Ad ad) {
this.bannerListener.onLeaveApplication();
}
}
The problem is that I really dont know how to add use my layout.add(adview) in the onReceivedAd(),
Any inputs would be helpful.
Custom Events are a little different compared to normal AdMob implementation. In requestBannerAd you create your ad network's adview, and request an ad. Once an ad is received (in this the onReceiveAd callback), you invoke:
this.bannerListener.onReceivedAd(this.adView);
You're already doing this in your code. When invoking this, you're telling the AdMob Mediation layer "Hey, I successfully loaded an ad, and here is my view for you to show." The Mediation layer takes in your adview and essentially calls layout.addView(adView) on your behalf (it adds it as a child of the main AdView you defined in your app).
So in your case, this code should just work.
Related
I want to implement Native Ads in RecyclerView without MediaView, So referred to this tutorial after following this I successfully implemented native ads in RecyclerView but my need is to load only icon, heading, advertiser and call to action button not the MediaView, etc.
So I removed the MediView and other elements Code from the implementation which I made after which the code looks like this
UnifiedNativeAdViewHolder.java
package com.mishracodes.myapplication;
import android.view.View;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.gms.ads.formats.UnifiedNativeAdView;
public class UnifiedNativeAdViewHolder extends RecyclerView.ViewHolder {
private UnifiedNativeAdView adView;
public UnifiedNativeAdView getAdView() {
return adView;
}
UnifiedNativeAdViewHolder(View view) {
super(view);
adView = view.findViewById(R.id.ad_view);
adView.setHeadlineView(adView.findViewById(R.id.ad_headline));
adView.setCallToActionView(adView.findViewById(R.id.ad_call_to_action));
adView.setIconView(adView.findViewById(R.id.ad_icon));
adView.setAdvertiserView(adView.findViewById(R.id.ad_advertiser));
}
}
and inside the RecyclerViewAdapter.java the part of code which manages NativeAdView
private void populateNativeAdView(UnifiedNativeAd nativeAd,
UnifiedNativeAdView adView) {
((TextView) adView.getHeadlineView()).setText(nativeAd.getHeadline());
((Button) adView.getCallToActionView()).setText(nativeAd.getCallToAction());
NativeAd.Image icon = nativeAd.getIcon();
if (icon == null) {
adView.getIconView().setVisibility(View.INVISIBLE);
} else {
((ImageView) adView.getIconView()).setImageDrawable(icon.getDrawable());
adView.getIconView().setVisibility(View.VISIBLE);
}
if (nativeAd.getAdvertiser() == null) {
adView.getAdvertiserView().setVisibility(View.INVISIBLE);
} else {
((TextView) adView.getAdvertiserView()).setText(nativeAd.getAdvertiser());
adView.getAdvertiserView().setVisibility(View.VISIBLE);
}
adView.setNativeAd(nativeAd);
}
loading Native Ad function in MainActivity.java
private void loadNativeAds() {
AdLoader.Builder builder = new AdLoader.Builder(this, getString(R.string.ad_unit_id));
adLoader = builder.forUnifiedNativeAd(
new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
#Override
public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
mNativeAds.add(unifiedNativeAd);
if (!adLoader.isLoading()) {
insertAdsInMenuItems();
}
}
}).withAdListener(
new AdListener() {
#Override
public void onAdFailedToLoad(int errorCode) {
Log.d("MainActivity", "The previous native ad failed to load. Attempting to"
+ " load another.");
if (!adLoader.isLoading()) {
insertAdsInMenuItems();
}
}
}).build();
adLoader.loadAds(new AdRequest.Builder().build(), NUMBER_OF_ADS);
}
Now after doing this I used Native Ad Validator to validate if the ads are implemented correctly
Result:
But After testing it for sometime I got this error
Warning in detail
So Is there any way to Call for Loading Native Ad so that only those ads are called which do not have Media View. As I have Seen Many Apps which uses native ads like this without Media View.
We can but it was possible before as Google made it mandatory to add MediaView. Also it's a good idea as your revenue would be increased but it may affect the user experience. Finally, you can't do this because if you upload it to the play store then Google will send you a warning notice saying that you didn't add MediaView. It may affect your earnings.
I'm too late, but it helps someone. I found a way to implement native ads without that MediaView.
The logic is Google has provided some native templates. These are for beginners who want to get started with native ads without implementing them from scratch. There are two templates - gnt_small_template_view and gnt_medium_template_view. gnt_small_template_view doesn't have MediaView. You can happily implement it in recycler view and also you can change the layout as per your requirement (but don't change the view ids). The original layout doesn't look good. You mostly change it.
Here is the documentation.
I have added these templates to my app, I didn't face any issue.
I'm having issues with ADs in my Android App.
Sometimes the AD is not shown, probably depending on the DPI or Size of the screen's device.
In my code, I implemented a check to set View.GONE in case the AD fails to load, but despite that on some devices I still have blank spaces where it's supposed to be the AD
For example, here one of my ADs
if(holder.nativeExpressAdView != null) {
AdRequest request = new AdRequest.Builder()
.build();
holder.nativeExpressAdView.loadAd(request);
Utilities.setAdListenerNative(holder.nativeExpressAdView);
}
//ADs listener to remove view if it fails load
//NATIVE ADS
public static void setAdListenerNative(final NativeExpressAdView nativeExpressAdView) {
nativeExpressAdView.setAdListener(new AdListener() {
#Override
public void onAdFailedToLoad(int i) {
super.onAdFailedToLoad(i);
nativeExpressAdView.setVisibility(View.GONE);
}
});
}
First show me your log
Second Dont use "Gone" because this will change the position of the adView if you are using other sizes of Android scale you will see that.
Third use a debugger.
Use "invisible"
I am trying to implement Amazon Ads in my Android app, but have not been able to get any ads to display. The message in my logs is always:
D/AmazonMobileAds DefaultAdListener: Default ad listener called - Ad Failed to Load. Error code: NO_FILL, Error Message: Server Message: no results
I have set up my account, verified my tax information, I have an application key. I have tried both with testing enable and disabled with the same results. I have followed the guide for loading ads from both Amazon and Admob for backfill, but the AdMob ads are then not retrieved either. If I retrieve just AdMob ads, I have no problem with them. The relevant code is below:
in onCreate():
AdRegistration.setAppKey(getString(R.string.amazon_ad_id));
AdRegistration.enableTesting(true);
AdRegistration.enableLogging(true);
// Initialize ad views
amazonAdView = new com.amazon.device.ads.AdLayout(this);
amazonAdView.setListener(new MyAdListener());
admobAdView = new com.google.android.gms.ads.AdView(this);
admobAdView.setAdSize(com.google.android.gms.ads.AdSize.SMART_BANNER);
admobAdView.setAdUnitId(getString(R.string.banner_ad_unit_id));
Method called to retrieve an ad:
void getAd(){
// Initialize view container
adViewContainer = (ViewGroup)findViewById(R.id.adView);
amazonAdEnabled = true;
adViewContainer.removeAllViews();
adViewContainer.addView(amazonAdView);
AdTargetingOptions adOptions = new AdTargetingOptions();
adOptions.enableGeoLocation(true);
amazonAdView.loadAd(adOptions);
}
Inner AdListener class
class MyAdListener extends DefaultAdListener {
public void onAdLoaded(com.amazon.device.ads.AdLayout view, AdProperties adProperties) {
Log.d(LOG_TAG, "Ad Loaded");
if (!amazonAdEnabled) {
amazonAdEnabled = true;
adViewContainer.removeView(admobAdView);
adViewContainer.addView(amazonAdView);
}
}
public void onAdFailedToLoad(com.amazon.device.ads.AdLayout view, AdError error) {
Log.d(LOG_TAG, "Ad Failed to load");
// Call AdMob SDK for backfill
if (amazonAdEnabled) {
amazonAdEnabled = false;
adViewContainer.removeView(amazonAdView);
adViewContainer.addView(admobAdView);
}
admobAdView.loadAd((new com.google.android.gms.ads.AdRequest.Builder()).build());
}
}
In my logging, neither of the statements from the AdListener shows, so it appears that the listener is never being called. So, I have two issues here:
Why am I always receiving a NO_FILL response, even when testing?
Why is the listener never called?
I have started integrating Airpushes banner ad "AdView" into my application and have it working well, however I know want to be able to disable the ads if the user has bought my donation key, I have this already set up and just set if the user has the key via a shared preference.
However when I do the below the ad still displays, in my oncreate I have:
AdView ad = (AdView) findViewById(R.id.guideAdView);
if (AppPreferences.getPrefs().getBoolean("full", false)){
ad.setVisibility(View.GONE);
}
AdCallbackListener adCallbackListener = new AdCallbackListener() {
#Override
public void onSDKIntegrationError(String message) {
// Here you will receive message from SDK if it detects any
// integration issue.
}
public void onSmartWallAdShowing() {
// This will be called by SDK when it’s showing any of the
// SmartWall ad.
}
#Override
public void onSmartWallAdClosed() {
// This will be called by SDK when the SmartWall ad is closed.
}
#Override
public void onAdError(String message) {
// This will get called if any error occurred during ad serving.
}
#Override
public void onAdCached(AdType arg0) {
// This will get called when an ad is cached.
}
#Override
public void onVideoAdFinished() {
// TODO Auto-generated method stub
}
#Override
public void onVideoAdShowing() {
// TODO Auto-generated method stub
}
};
if (airPlay == null)
airPlay = new AirPlay(this, adCallbackListener, false);
I know the if (AppPreferences.getPrefs().getBoolean("full", false)) works fine because this is used else where in the activity to display other information if the user does have the full key.
So the question is why goes the above not work for the adView?
I have never used airpush but why don't you just not run the ad code if the user has a key.
There is no point setting up all of the listeners and the new AirPlay object if the ads are not to be displayed.
e.g:
if (AppPreferences.getPrefs().getBoolean("full", false)){
// Do not show ads
AdView ad = (AdView) findViewById(R.id.guideAdView);
ad.setVisibility(View.GONE);
} else {
// Show Ads
// Set up listener (omitted from this example for clarity)
if (airPlay == null) airPlay = new AirPlay(this, adCallbackListener, false);
}
Following this code (https://developers.google.com/mobile-ads-sdk/docs/admob/advanced):
import com.google.ads.*;
public class BannerExample extends Activity implements AdListener {
private InterstitialAd interstitial;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create the interstitial
interstitial = new InterstitialAd(this, MY_INTERSTITIAL_UNIT_ID);
// Create ad request
AdRequest adRequest = new AdRequest();
// Begin loading your interstitial
interstitial.loadAd(adRequest);
// Set Ad Listener to use the callbacks below
interstitial.setAdListener(this);
}
#Override
public void onReceiveAd(Ad ad) {
Log.d("OK", "Received ad");
if (ad == interstitial) {
interstitial.show();
}
}
}
I have an image (my advertise) smaller than my phone screen.
I want to know if it's possible to resize the view created into the java code. (in onCreate() or onReceiveAd()) for adapting the size of the advertise.
LayoutParam, in CenterCrop for exemple, or simple resizing of the view !
If you want to serve an interstitial from DFP to a phone, you can set up an image in DFP, and override the size to be 320x480. Then that creative can serve, and be smaller if you want.
Or, you may be looking for a banner instead of an interstitial overlay that takes over the entire screen. In that case, you can specify a custom size for your AdView, that should match the size you set up in DFP.