Xamarin, Android: Using the AdListener right - android

I'm trying to use the AdListener in C#.
I have an interstitial ad loading when the app is first started, but sometimes my ad gets skipped because it isn't fully loaded yet. I think an Adlistener should do the trick.
Unfortunately, I have NO CLUE on how to implement it. Also, there is no tutorial on how to do it in C# only in Java and I couldn't find a translation for it :(
Add:
adListener.OnAdLoaded() += (o, e) =>
{
mInterstitialAd.Show();
};
This doesn't work :(
Any help would be awesome!

You can create a class which inherits from Android.Gms.Ads.AdListener, then use the instance of this class as the Listener for your mInterstitialAd, for example:
mInterstitialAd.AdListener = new AdListener(this);
AdListener:
private class AdListener : Android.Gms.Ads.AdListener
{
private MainActivity that;
public AdListener(MainActivity t)
{
that = t;
}
public override void OnAdLoaded()
{
base.OnAdLoaded();
}
public override void OnAdClosed()
{
that.RequestNewInterstitial();
that.BeginSecondActivity();
}
}
You can also checked the official demo for xamarin android ad: AdMobExample Sample.

Related

How to implement Native Ads without MediaView

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.

Android: Amazon ads never retrieved, AdListener never called

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?

MoPub android sdk onInterstitialLoaded never called

I tried to implement MoPub interstitials in my libgdx android application.
When I pass a wrong ID in the MoPubInterstitial() constructor, the onInterstitialFailed() method in the listener is called correctly, but when I pass my correct ID nothing happens.
onInterstitialLoaded() should be called eventually, but it isn't.
MoPubInterstitial.isReady() never is true.
Am I missing something?
I don't know, which part of my code I should paste. I'm out of ideas, how to debug this.
Any suggestions?
private static final String MoPubInterstitialID = "xxxxxxxxxxxxx";
private MoPubInterstitial moPubInterstitial;
moPubInterstitial = new MoPubInterstitial(this, MoPubInterstitialID);
moPubInterstitial.setInterstitialAdListener(this);
moPubInterstitial.load();
moPubInterstitialButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.d("MoPub", "Interstitial button clicked");
if ( moPubInterstitial.isReady() )
{
Log.d("MoPub", "Interstitial is Ready");
moPubInterstitial.show();
Log.d("MoPub", "Interstial Shown");
moPubInterstitial.forceRefresh();
}
}
});
These are all the steps that i took to make it work. Let me know if you have any questions.

Revmob fullscreen ad preload on Unity?

Is there any way to preload fullscreen ad on Unity? Right now when we call it using
revmob.ShowFullscreen();
when we create end game screen. But most of the time it loads after 5/10 secs later which is in-game most probably if you press restart, so it shows a full screen ad during gameplay.
I've found some ways to preload it on native android and tried same function to see if they exists in Unity but no luck.
Thanks.
Yes! You can use the following code:
private RevMobFullscreen fullscreen;
fullscreen = revmob.CreateFullscreen();
fullscreen.show();
If you need more information, you can access RevMob mobile ad network website: https://www.revmobmobileadnetwork.com
It will be better to add this code to the Create statement:
private RevMobFullscreen fullscreen;
fullscreen = revmob.CreateFullscreen();
...and then also this code to the listener:
RevMobAdsListener revmobListener = new RevMobAdsListener() {
// Required
#Override
public void onRevMobSessionIsStarted() {
fullscreen.show();
}
(...)
}
This will show the fullscreen ad.
You can do like this to preload revmob videos in unity. But there are memory leaks in revmob unity videos and they might fix that in 9.2.x...
REVMOB_APP_IDS = new Dictionary<string, string>() {
{ "Android", androidMediaId},
{ "IOS", iosMediaId }
};
revmob = RevMob.Start (REVMOB_APP_IDS, gameObject.name);
public void SessionIsStarted ()
{
CacheVideoInterstitial("Bootup");
}
public void CacheVideoInterstitial(string location) {
DestroyVideo();
StartCoroutine(CacheAfterEndofFrame(location));
}
IEnumerator CacheAfterEndofFrame(string location) {
yield return null;
fullscreenVideo = revmob.CreateVideo(location);
}
void DestroyVideo() {
if( fullscreenVideo != null ) {
fullscreenVideo.Hide();
//fullscreenVideo.Release();
//fullscreenVideo = null;
}
}
// revmob ad closing delegate
public void UserClosedTheAd (string revMobAdType)
{
DestroyVideo();
CacheVideoInterstitial(this.location);
}

How to know when someone has clicked on an Adview?

I'm using an adview in my application and want to know when someone has clicked that Adview!
How I can do that?
The onclicklistener does not seem to work.
You can set the listener this way:
adView.setAdListener(this);
And then implement this:
#Override
public void onPresentScreen(Ad arg0) {
//OnClick here
}
The click is handled inside of the AdMob API, and I don't think there is a way to do what you are asking with the API as its against the TOS to use a "unlock feature by clicking X number of ads" model. For that reason I don't believe it is available in the API where the onclicklistener is.
Try this
mAdView.setAdListener(new AdListener() {
#Override
public void onAdOpened() {
super.onAdOpened();
}
});

Categories

Resources