YouTube

Got a YouTube account?

New: enable viewer-created translations and captions on your YouTube channel!

English subtitles

← Showing and Loading Interstitial Ad

Get Embed Code
5 Languages

Showing Revision 2 created 10/02/2015 by Udacity Robot.

  1. All right, before we go into the code
    of displaying an Interstitial Ad,
  2. let's discuss this a little bit.
  3. Because there are two important things
    to think about with Interstitial Ads.
  4. The first one, is about
    the format of an Interstitial Ad.
  5. As we know, the Interstitial Ad consumes
    the entire screen of the device.
  6. Therefore, it's important
    not to interrupt the user
  7. while they are engaging with your app.
  8. So the first important things to think
    about, is to display Interstitial Ads
  9. with our natural breaks in your app,
    for example, between levels in a game.
  10. The second important thing with
    Interstitial Ads is to prepare
  11. the loading of the ad before showing it.
  12. Why is this important?
  13. Well, loading an Interstitial Ad
    can take time, in particular,
  14. if the network is slow.
  15. When the time has come to display an
    Interstitial Ad, you don't want the user
  16. to sit there and wait for it to load,
    you want it to be displayed immediately.
  17. Therefore, we load it in advance.
  18. We prepare the loading
    before we're showing the ad.
  19. So how do we do that.
  20. Well, let's start by looking
    at the class Interstitial Ad.
  21. This is the class we use to load and
    show an Interstitial Ad.
  22. So it has a method called loadAd.
  23. But this method is just requesting
    the ad to be loaded and
  24. to avoid blocking the UI thread,
  25. it will return immediately
    before the ad has been loaded.
  26. So how do we know when
    the ad has been loaded?
  27. By setting an AdListener,
    we can overload to onAdLoaded method.
  28. So when the Interstitial Ad has finished
    loading, this method will be called, and
  29. we can then proceed to call
    show on the Interstitial Ad.
  30. So, this is the flow that we
    will implement in our app.
  31. You should also know that Interstitial
    Ad has a method called isLoaded.
  32. So if you're not interested in knowing
    exactly when the ad has been loaded,
  33. then you could simply call loadAd.
  34. And when the time has come to show the
    ad, you check if the ad has been loaded.
  35. If it has been loaded,
    then you call the show method.
  36. Otherwise, you do not
    show the ad at all.
  37. Now let's check out the code
    to implement this flow.
  38. Let's open up
    the InterstitialActivity Java file.
  39. The first thing we've done here is to
    add an InterstitialAd field called
  40. mInterstital, there are no
    changes to the onCreate method.
  41. So let's go and
    look at the loadInsterstitial method.
  42. This code should already be there
    because we set it in the previous
  43. exercise.
  44. So let's look at the other code.
  45. First, we're creating an instance
    of the InterstatialAd.
  46. Then, we're setting the AdUnitId.
  47. And we set it to this string that
    will return test InterstitialAds.
  48. And this string, we defined in our
    resource string file, so we have to get
  49. it using the getResources().getString
    and then the resource ID.
  50. And now comes the flow
    that we talked about.
  51. Here, you can see that we're setting
    the listener for the InterstitialAd.
  52. Let's talk about this in a little while.
  53. But we're setting a listener here and
    then the execution drops down here.
  54. So now, you can see, we're creating
    an AdRequest using the builder.
  55. And then we're calling the loadAd
    method on our InterstitialAd instance.
  56. Providing the AdRequest as an argument,
    and as we saw before,
  57. this method will start
    the ad loading process, but
  58. it will return immediately
    before the ad has been loaded.
  59. That's where our listener comes
    into play, and as you can see,
  60. we're creating an anonymous class here
    based on our ToastAdListener class.
  61. And as you can see,
    we're overloading the onAdLoaded method.
  62. So since we're setting
    the listener here,
  63. this method will be called when
    the ad has completed loading.
  64. So we call our parent method that
    will display the toast message,
  65. since that is what we defined
    in the ToastAdListener class.
  66. And then we set the text of our
    disable button to Show Interstitial.
  67. And we also enabled that button.
  68. That means that the user
    can now press this button.
  69. And when that happens,
  70. the showInterstitial callback
    method will be called.
  71. So what are we doing
    is showInterstitial.
  72. Well, with simple check if
    the Interstitial Ad has been loaded, and
  73. this may seem like an unnecessary check,
  74. since we know the ad has been
    loaded since onAdLoaded was called.
  75. But we're doing it any how.
  76. And then the time has come to actually
    show the Interstitial Ad, and
  77. when this happens, the ad will actually
    be displayed in full screen mode.
  78. And for the button,
    we go back to a previous state,
  79. setting the text Interstitial Not Ready
    and then disabling it.
  80. And finally, going back to our listener.
  81. If the ad could not be loaded, the
    onAdFailedToLoad method will be called.
  82. In that case, again,
    we show the toast message and
  83. then we set the ErrorReason
    to the text of the button.
  84. That's it, that's the code we have
    to implement the Interstitial Ad.