There are times when an ad request can’t be filled--for example, when the internet connection goes out on a mobile device. In part 1 of this 2-part series, we’ll demonstrate how to display a custom image using the Google AdMob SDK when AdMob can’t retrieve an ad.

First, create a layout file called main.xml. Your layout might look like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" >
    <LinearLayout android:id="@+id/adLayout"
        android:layout_height="wrap_content" >
        <ImageView android:id="@+id/image"
            android:visibility="gone" />

Note that in this layout:

  • The LinearLayout with identifier adLayout is the container for your custom image as well as the AdMob ad.
  • The ImageView assumes you have a custom image named your_image in the res/drawable directory.
  • The image visibility is set to gone because it doesn’t need to be shown unless AdMob fails to receive an ad.

Next, create and load an ad in your Activity’s onCreate method. Optionally, you can also add an onClick event to your custom image to launch a web page in case there’s still an internet connection.

private AdView adView;
private ImageView imageView;

public void onCreate(Bundle savedInstanceState) {

  adView = new AdView(this, AdSize.BANNER, "YOUR_AD_UNIT_ID");
  LinearLayout layout = (LinearLayout) findViewById(;
  adView.loadAd(new AdRequest());
  imageView = (ImageView) findViewById(;

public void onClick(View view) {
  Intent intent = new Intent(
      Intent.ACTION_VIEW, Uri.parse(""));

You can then implement AdMob’s AdListener callbacks to determine if you need to show a custom image. If AdMob invokes onReceiveAd, hide your custom image and display the ad. If onFailedToReceiveAd is invoked, hide the AdMob ad and display your custom image. The following code demonstrates these concepts:

private boolean firstAdReceived = false;

public void onReceiveAd(Ad ad) {
  firstAdReceived = true;
  // Hide the custom image and show the AdView.

public void onFailedToReceiveAd(Ad ad, ErrorCode code) {
  if (!firstAdReceived) {
    // Hide the AdView and show the custom image.

    // Turn off the click listener if there is a network error.
    if (code == ErrorCode.NETWORK_ERROR) {
    } else {

You only need to show your custom image if AdMob fails to receive an ad on the first request. You can determine if AdMob has previously received an ad by using the firstAdReceived flag.

These steps are sufficient to display a custom image if AdMob fails to return an ad. Tune in tomorrow for part 2 of this series, where we’ll discuss best practices on serving network ads when the user regains a network connection. Part 2 will also include a full code example demonstrating concepts from both posts.

Reach out to us know on the forum if you have any questions about the Google AdMob SDK, or join us during office hours.

Update: Added link to part 2 of this post.