Set AdMob banner to match parent width by XML

David picture David · Sep 3, 2013 · Viewed 12.3k times · Source

I'm trying to set AdMob banner ad to match it's parent width.

I tried as sample:

<com.google.ads.AdView
        android:id="@+id/ad"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        ads:adSize="BANNER"
        ads:adUnitId="app-pub-id"
        ads:loadAdOnCreate="true"/>

And got:

enter image description here

I also tried with:

ads:adSize="SMART_BANNER"

In the IDE xml preview, I got:

enter image description here

But in fact the ad didn't match his parent width.

Anyone?

Answer

David picture David · Sep 3, 2013

The right way to set AdMob banner to match his parent by XML is:

<com.google.ads.AdView
        android:id="@+id/ad"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="ca-app-pub_id"
        ads:loadAdOnCreate="true"/>

In addition, as mention before, the ability to match parent depends on the advertiser.

If the advertiser supplied the relevant resources, the ad will stretch to match his parent.

For example full size ad on Nexus 4:

enter image description here