Friday, 27 May 2011

Why is my AdMob ad not showing up?

Last weekend, I was working on Process Widget and also updating the ad code to the latest version from our overlords at Google.
Strangely, most of my weekend was spent figuring out how to make my ads show where I want them to show! I could make them work in an ampty activity, but not in the header of my listview. I could add them to the very top of my activity hierarchy, but they would not show up in dialogs.

I finally found some obscure paragraph in some obscure document in deep bowels of Googles AdMob documentaion, briefly mentioning that if the adview does not have enough space around it to show it in the size it wants to display, it will not show at all and will not warn you about this.

Well, fuck me with a rake! That solves a lot of problems. See, the ad width is 320dp, which is exactly the width of a portrait screen theme. Which means, that if you add padding or borders, the ad will not fit!
See, it is common to add a bit of padding to tabhost or listviews to make them look nicetr and give a bit of depth effect. However, after this it will not fit into the tab containers or listview headers/footers.

So! Just make sure your ad is in a container with no padding or borders at all unless you want it to autohide :P

I would actually imagine, that you could hide effectively most of the AdMob ads in your device with a Theme that would add 1px/dp padding to LinearLayout component. Anyone care to try this? Please comment on this article and tell the results if you do. I haven't yet researched how to make my own themes.

Another thing I would like to discuss. I made kinda clever "Message from our sponsors" dialog. Here's how it looks like:

What it does, is it creates an adview and request in the background and lets the activity implement the adlistener interface. If/When you get a "tablet" sized ad like above, it will show a dialog to view it.

Here's the code:

protected void onPostResume() {

private Dialog adDialog;
private void initAd() {
 adDialog = new Dialog(this);
 adDialog.setTitle("A message from our sponsor...");

private void loadAd() {
 // Banner
 LinearLayout ll = (LinearLayout) adDialog.findViewById(;
 AdView ad = new AdView(this, AdSize.IAB_MRECT, "a14dd77743f0b3a");
 AdRequest req = new AdRequest();

public void onReceiveAd(Ad arg0) {;

I'm sure you can figure out the rest!
Have fun!

However, there is a problem sometimes. I'm not quite sure why, but I get an InvalidTokexException sometimes.
It might be that I dismiss the Activity just before an ad is being loaded. It will crash the app when it tries to show the dialog after its container activity is finished.
This needs more debugging.
I should probably save the state on onPause of the activity and not try to show it if the activity is paused.