0

I am trying to use include layout for the adView with data binding. But, it throws me an error:

java.lang.IllegalStateException: The ad size and ad unit ID must be set before loadAd is called.

Now what I am doing is, in my main activity layout, I have included the layout with variable adId for the adUnitId like this:

       <include
            android:id="@+id/adViewInclude"
            layout="@layout/include_ads"
            app:adId="@{@string/main_activity_banner_ad_unit_id}" />

My include_ads.xml is like this:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

        <variable
            name="adId"
            type="String" />

    </data>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="vertical">

        <com.google.android.gms.ads.AdView
            android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:adSize="SMART_BANNER"
            app:adUnitId="@{adId}" />

    </FrameLayout>
</layout>

Now in my Activity's onCreate method I am calling this method at last:

private void loadAds() {
    mBinding.executePendingBindings();
    AdRequest adRequest = new AdRequest.Builder().build();
    mBinding.mainContent.adViewInclude.adView.loadAd(adRequest);
}

But, it is throwing an error. What's going wrong here?

kirtan403
  • 7,293
  • 6
  • 54
  • 97
  • (As far as I know) Unfortunately, it is not possible to bind the `ad ID`, the cause is described in your error. (`ad unit ID must be set before loadAd is called`). I got the same error some time ago, but maybe I will try to find a work around :) – yennsarah Nov 03 '16 at 07:38
  • 1
    @Amylinn Yes, it just doesn't work. I have found a work around. Will post the code soon. – kirtan403 Nov 04 '16 at 02:39

1 Answers1

0

You should use app:adSize="BANNER" instead of SMART_BANNER

Sergey Nikitin
  • 807
  • 8
  • 23