Lukas R. Lukas R. - 2 months ago 23
C# Question

FragmentActivity and Admob Ads dont show up?

Hello i have a problem.
My Ad doent show up.
It always say that my ad isnt loaded with my Toast i declared.

Here is my Code:

Here my Button to show the ad in my optionmenu
This optionmenu load a custom alertdialog and in this alertdialog is a button and if you click this button the Ad should show up but it doesnt:

//Optionsmen├╝
public override bool OnPrepareOptionsMenu(IMenu menu)
{
MenuInflater.Inflate(Resource.Menu.profile_menu, menu);
RelativeLayout badgeLayout = (RelativeLayout)menu.FindItem(Resource.Id.diamonds).ActionView;
TextView mCounter = badgeLayout.FindViewById<TextView>(Resource.Id.diamondcounter);
mCounter.Text = diamonds;
Button startdiamonds = badgeLayout.FindViewById<Button>(Resource.Id.startdiamonds);
startdiamonds.Click += delegate
{
View content = LayoutInflater.Inflate(Resource.Layout.GetDiamond, null);
AlertDialog.Builder alert = new AlertDialog.Builder(this, Resource.Style.MyAlertDialogStyle);
alert.SetView(content);
ImageButton freegem = content.FindViewById<ImageButton>(Resource.Id.ButtonGemAd);
freegem.Click += delegate
{
//Interstitial Ad
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.AdUnitId = AD_UNIT_ID1;
mInterstitialAd.AdListener = new adlistener(this);
var adRequest = new AdRequest.Builder().Build();
mInterstitialAd.LoadAd(adRequest);
if (mInterstitialAd.IsLoaded)
{
mInterstitialAd.Show();
}
else
{
Toast.MakeText(this, "Ad not ready yet! Please wait a few seconds!", ToastLength.Long).Show();
}
};
alert.SetPositiveButton(" Cancle", (senderAlert, args) => { });
Dialog dialog = alert.Create();
dialog.Show();
};

return base.OnPrepareOptionsMenu(menu);
}


And here my AdAdpater:

//AdListener f├╝r AdClosed
class adlistener : AdListener
{
private ProfileActivity profileActivity;
public delegate void AdLoadedEvent();
public delegate void AdClosedEvent();
public delegate void AdOpenedEvent();
public event AdLoadedEvent AdLoaded;
public event AdClosedEvent AdClosed;
public event AdOpenedEvent AdOpened;

public adlistener(ProfileActivity profileActivity)
{
this.profileActivity = profileActivity;
}

public override void OnAdOpened()
{
if (AdOpened != null) this.AdOpened();
base.OnAdOpened();
}

public override void OnAdClosed()
{
if (AdClosed != null) this.AdClosed();
base.OnAdClosed();

}

public override void OnAdLoaded()
{
if (AdLoaded != null) this.AdLoaded();
base.OnAdLoaded();

}
}

Answer

Change code in freegem.Click += delegate to LoadAd instead of ShowAd()

interstitialAd.LoadAd(adRequest);

You may invoke LoadAd at any time, however, you must wait for InterstitialAd's ReceivedAd event to be raised before displaying the creative. If the FailedToReceiveAd event is raised, gracefully handle the error condition.

Once the ad is received, you can show the ad using the ShowAd method. The interstitial then takes over the screen until the user dismisses it, at which point control returns to your app.

private void OnAdReceived(object sender, AdEventArgs e)
       {
           System.Diagnostics.Debug.WriteLine("Ad received successfully");
           interstitialAd.ShowAd();
       }