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="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout android:id="@+id/adLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/your_image"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>
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;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
adView = new AdView(this, AdSize.BANNER, "YOUR_AD_UNIT_ID");
adView.setAdListener(this);
LinearLayout layout = (LinearLayout) findViewById(R.id.adLayout);
layout.addView(adView);
adView.loadAd(new AdRequest());
imageView = (ImageView) findViewById(R.id.image);
imageView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Intent intent = new Intent(
Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(intent);
}
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;
@Override
public void onReceiveAd(Ad ad) {
firstAdReceived = true;
// Hide the custom image and show the AdView.
imageView.setVisibility(View.GONE);
adView.setVisibility(View.VISIBLE);
}
@Override
public void onFailedToReceiveAd(Ad ad, ErrorCode code) {
if (!firstAdReceived) {
// Hide the AdView and show the custom image.
adView.setVisibility(View.GONE);
imageView.setVisibility(View.VISIBLE);
// Turn off the click listener if there is a network error.
if (code == ErrorCode.NETWORK_ERROR) {
imageView.setOnClickListener(null);
} else {
imageView.setOnClickListener(this);
}
}
}
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.
- Eric Leichtenschlag , AdMob Team