12

I am trying to show AdMob ads in my activity, but it is always giving error that "not enough space to show ad".

My XML file is:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <com.google.ads.AdView
        android:id="@+id/adMobadView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        ads:adSize="BANNER"
        ads:adUnitId="@string/admobid"
        android:layout_alignParentBottom="true"
        android:gravity="bottom|center_horizontal"
        ads:loadAdOnCreate="true" />
</RelativeLayout>

My Java file:

    AdView ad=(AdView)findViewById(R.id.adMobadView);

    AdRequest re = new AdRequest();
    re.setTesting(true);
    re.setGender(AdRequest.Gender.FEMALE);
    ad.loadAd(re);

LogCat:

   02-18 14:16:17.869: W/webcore(813): Can't get the viewWidth after the first layout
   02-18 14:16:17.979: I/Ads(813): onReceiveAd()
   02-18 14:16:17.979: W/Ads(813): Not enough space to show ad! Wants: <320, 50>, Has: <288, 430>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ravi
  • 34,851
  • 21
  • 122
  • 183

5 Answers5

30

The left and right padding of your RelativeLayout is consuming space required by your AdView.

Change RelativeLayout config to :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 ...
 android:paddingLeft="0dp"
 android:paddingRight="0dp"
 ...
>

You probably also want to change your AdView config to:

<com.google.ads.AdView
        android:id="@+id/adMobadView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ...
        />

as you don't really want your AdView to consume the entire screen.

Android
  • 8,995
  • 9
  • 67
  • 108
William
  • 20,150
  • 8
  • 49
  • 91
0

Check the dimensions of the screen and the paddings, and see if it actualy fits.

Also, try to change the Adview to this:

    <com.google.ads.AdView
        android:id="@+id/adMobadView"
        android:layout_width="320dp"
        android:layout_height="50dp"
        ads:adSize="BANNER"
        ads:adUnitId="@string/admobid"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        ads:loadAdOnCreate="true" />
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Carlos Robles
  • 10,828
  • 3
  • 41
  • 60
0
to show ads inside relative layout you can follow this design

<RelativeLayout
        android:id="@+id/relativeLayoutpopular"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_above="@+id/bottomBar"
        android:layout_below="@+id/relativeLayoutadd"
        android:layout_marginTop="5dp"
        >
            <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:paddingTop="3dp">
         <com.google.ads.AdView
             xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
             android:id="@+id/adView"
             android:layout_width="fill_parent"
            android:layout_weight="0.9"
            android:layout_height="wrap_content"
            ads:adSize="SMART_BANNER"
            android:background="#2d2f2f"
            ads:adUnitId="admobId" />
        </LinearLayout>
    </RelativeLayout>
0

i had the same problem with NativeExpressAdView. If you add padding or margin to your layout, I think, it's not a good idea to fix the ad's size width. I sugger to do in programmatically

  1. First step : get the dp size of screen witdh

    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); //float dpHeight = displayMetrics.heightPixels / displayMetrics.density; dpWidth = displayMetrics.widthPixels / displayMetrics.density;

2. Second step:

mAdView.setAdSize(new AdSize((int) dpWidth, 150));

Hoshouns
  • 2,420
  • 23
  • 24
-1

Simply set the margins to 0dp in res->values->dimens.xml

<dimen name="activity_horizontal_margin">0dp</dimen>
<dimen name="activity_vertical_margin">0dp</dimen>
Gao
  • 3
  • 1
  • 3
  • Well spotted sir! In my case this was the error as well. It does not like margins the piece of crap! – Radu Jul 11 '14 at 12:55