We’ve already shared how you can get ads up and running along the bottom or top of your app using the ListView layout in Android applications. This post will show you how to embed AdMob ads within the ListView as list items.


For this scenario, let’s assume that we want ads to occur every k items within the list.



Embedding ads within a ListView requires writing a custom adapter that extends BaseAdapter. This adapter will add functionality to allow ads to behave as list items within a ListView. Your class declaration will look something like:

public class ListViewExampleListAdapter extends BaseAdapter 
    implements AdListener{

  // Constructor takes in a BaseAdapter also
  public ListViewExampleListAdapter(Activity activity, BaseAdapter delegate) {
 …

Notice how we use the decorator pattern by passing in a BaseAdapter into the constructor of the custom adapter class. Using this pattern, we can accept any type of adapter (including a custom adapter) instead of having to hard-code the types of acceptable list items. We only have to implement functionality relevant to AdMob ads within our list adapter, and defer to the underlying adapter class for functionality related to any other list items.

The most important step is to override getView() to return ads for every k items. We can use delegate.getView() to return the view for any items that aren’t ads. The code for this looks like:

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    if ((position % k) == 0) {
      if (convertView instanceof AdView) {
        // Don’t instantiate new AdView, reuse old one
        return convertView;
      } else {
        // Create a new AdView
        AdView adView = new AdView(activity, AdSize.BANNER,
                                   INSERT_ADMOB_ID_HERE);

        // Convert the default layout parameters so that they play nice with
        // ListView.

        float density = activity.getResources().getDisplayMetrics().density;
        int height = Math.round(AdSize.BANNER.getHeight() * density);
        AbsListView.LayoutParams params = new AbsListView.LayoutParams(
            AbsListView.LayoutParams.FILL_PARENT,
            height);
        adView.setLayoutParams(params);

        adView.loadAd(new AdRequest());
        return adView;
      }
    } else {
      // Offload displaying other items to the delegate
      return delegate.getView(position - (int) Math.ceil(position / k) - 1,
          convertView, parent);
    }
  }

The bolded code makes sure that the adView list item is large enough to fit the ad.

The other methods that we need to override are pretty trivial. They require adding information specific to our ad implementation and deferring to the delegate for everything else. These methods are:

If you have any questions about embedding ads within a ListView, or even any questions about the SDK in general, feel free to post them in our forum come join us in our upcoming developer hangout. Take a look at AdCatalog v2 for Android for a working example of everything demonstrated here.