New admob Express Native ads fail with IllegalStateException
Asked Answered
S

2

6

I've been trying to integrate express native ads to my application. In the express native ads documentation I've read that they work best when displayed with FULL_WIDTH ad size. I tried to set my ad size to FULL_WIDTH but it fails with IllegalStateException:

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

Here is my xml code:

<com.google.android.gms.ads.NativeExpressAdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="FULL_WIDTH"
ads:adUnitId="@string/native_ad_unit"/>

The ad unit is correct, it works with other ad sizes like 320x150 etc.

Is there something wrong with my implementation?

Cheers

Sarita answered 20/5, 2016 at 21:1 Comment(0)
S
19

I found a solution, it will work if you use it like this:

<com.google.android.gms.ads.NativeExpressAdView
 android:id="@+id/adView"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 ads:adSize="FULL_WIDTHx80"
 ads:adUnitId="@string/native_ad_unit"/>
Sarita answered 26/5, 2016 at 20:19 Comment(3)
Thank you very much, you save me a lot of time, the adSize was the problem.Westmoreland
Hi, I am using Native ad and its showing Internal Error. it's been two days and nothing worked for me. My xml tag <com.google.android.gms.ads.NativeExpressAdView android:id="@+id/adViewNative" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" ads:adUnitId="@string/admob_id_native" ads:adSize="320x200" />Duteous
NOTE: ads:adSize="FULL_WIDTHxsome_height" won't work if NativeExpressAdView has any Horizontal margins. Because it tries to occupy entire width of device.Anthropologist
L
1

You can only set it programmatically. Here is an excerpt from the documentation:

Publishers can also use the FULL_WIDTH constant when programmatically creating an AdSize for a NativeExpressAdView. In this case, the ad will occupy the entire width of the device screen.

Source: https://firebase.google.com/docs/admob/android/native-express#nativeexpressadview

Lepus answered 21/5, 2016 at 23:17 Comment(2)
NativeExpressAdView adView = (NativeExpressAdView) v.findViewById(R.id.adView); adView.setAdSize(AdSize.FULL_WIDTH); this isn't working...Ubangishari
You will be able to set AdSize only if you are creating the NativeExpressAdView programmatically. I was able to find an example here. Just use FULL_WIDTH instead of the 400 width as given in the example.Lepus

© 2022 - 2024 — McMap. All rights reserved.