My XML code for the ad is
<com.google.android.gms.ads.NativeExpressAdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:adUnitId="YOUR_AD_ID"/>
I am setting the adsize programmatically using the following code
mAdView = (NativeExpressAdView) cardView.findViewById(R.id.adView);
int width = screenwidth - 16;
mAdView.setAdSize(new AdSize(width, 250));
AdRequest request = new AdRequest.Builder()
.addTestDevice("YOUR_DEVICE")
.build();
mAdView.loadAd(request);
When I run, the app crashes with the error
java.lang.IllegalStateException: The ad size and ad unit ID must be set before loadAd is called.
It works fine when I try like this
<com.google.android.gms.ads.NativeExpressAdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:adUnitId="ca-app-pub-3940256099942544/1072772517"
app:adSize="320x250"/>
But I want to set the ad width dynamically