When including ads in an Android app, there are a few options for how to display advertisements to users. Beyond the choices of which ad networks to use, there are options for how the app retrieves and displays the ads.
You can choose to set up the ad request in the layout files (XML), or you can implement the ads entirely in code. Each has advantages and disadvantages, so it might be appropriate to use one or the other in different situations.
For the purposes of this post, I compare these two options for banner ads with AdMob, the most well-known ad solution for Android apps. Keep in mind that there are many other ad options, and not all of them support both of these choices.
Two different methods to get the same result.
Advantages of implementing ads in layout
If you are comfortable with XML layouts for Android, it won’t be too difficult to insert ads into the layout. The biggest advantage is simplicity–there is no code maintenance to do, just a few lines of XML in your layout file. It can be copied into any layout, and there is now an ad there.
If you will use this ad a few times, you can isolate this code into a single ad layout that you can reuse anywhere using the include mechanism. For example:
Most of this is boilerplate–the only real unique code is “MY_AD_UNIT_ID”, which you can hard-code in this layout file or pull from a project resource. No code needs to be changed–all of the ad retrieval and display is handled by the ad library called directly from this layout file.
Please note that the code above is simplified–see the AdMob instructions for the full details when you go to implement. In addition, I used the XML-based ad integration to allow easy creation of multiple “flavors” of an app. For example, if you want two versions of an app with two different ad IDs (or one with no ads at all), you can use library projects to put all of your code and assets into a library project, with two child projects. The only files that the child projects need to contain are the differing layouts with the respective ad IDs. No code differs between the two apps.
I also used layout-based setup for ease of swapping out two different XML-based ad options for split-testing.
Advantages of implementing ads in code
The other option is to create the space for your ads in the layout, but handle the ad loading in the code itself. Some developers coming into the Android world may be more comfortable with the code-based option rather than the XML-based setup.
The advantages to this approach include:
- More control–logic in the code can control how and when the ad is loaded;
- The ability to set metadata (some ad networks allow including demographic information);
- The ability to do split-testing by app store (separate IDs for each market);
- Access to ad networks that can’t be set up in a layout file.
Some of these options are used for more advanced strategies and aren’t required for the initial implementation. The basic AdMob instructions include adding only a small amount of code:
AdView adView = new AdView(this, AdSize.BANNER, MY_AD_UNIT_ID);
LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);
As before, most of this is boilerplate, except for “MY_AD_UNIT_ID”. This same code needs to be added to every activity that will display ads. Beyond that, more complicated setups are optional, and can be pursued when they are appropriate. The main advantage of the code-based implementation is the flexibility.
Reasons for choosing one over the other
Over time, my preference has migrated from including the ads just in the layout (XML-based) to implementing a code-based ad solution. This is in part due to my ongoing research into ad networks for the Android Income Series books, but also due to pursuing multiple Android app stores.
While I lost the simplicity of having no ad code in my activities, I gained the flexibility to track my ad revenue by app store, helping me understand which markets are most profitable. In addition, this allows my to use ad mediators–my current choice is MoPub, which requires instantiating the ad in code, not from the layout. I had to make the switch and add a little code in all of my apps, but now I can manage my ads from a web interface without making further changes to my apps.
Which one is right for you?
It depends in part on your ad strategy, but for many Android developers the most important thing is to get the initial ads integrated easily. Whichever method you are more comfortable with–XML-based or code-based–is the right one for you now. It’s not really that much work to make the switch later if a new strategy requires ads in code.