2
votes

I am trying to use NativeExpressAdView but instead of the ad it shows

Required XML attribute 'adSize' was missing

in a black background, red-bordered box. I am adding adUnit and adSize to the adView programmatically like this

    <com.google.android.gms.ads.NativeExpressAdView
    android:id="@+id/nativeAdView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"/>

Java Code:

nativeAd = (NativeExpressAdView) itemView.findViewById(R.id.nativeAdView);
        int widthInDP = context.getResources().getConfiguration().screenWidthDp;
        widthInDP -= context.getResources().getDimension(R.dimen.activity_horizontal_margin);
        nativeAd.setAdUnitId(context.getString(R.string.dummy_ad_unit_id));
        nativeAd.setAdSize(new AdSize(widthInDP, 100));
        nativeAd.loadAd(adRequest);

and adRequest was initially declared as follows:

        adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .addTestDevice("DD8112531D2B704F33C3B0314744E92B")
            .addTestDevice("62F4961171DF9F253BC3639F7B9A686B")
            .build();

I followed the tutorial as explained in the official docs of Firebase AdMob. Why cannot I show ads in NativeExpressAdView while setting ad size dynamically?

2

2 Answers

3
votes

add this line of code and your ad should be good

xmlns:ads="http://schemas.android.com/apk/res-auto"

so it become like this and decleare the adSize

    <com.google.android.gms.ads.NativeExpressAdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/nativeAdView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="320x150"
android:layout_gravity="center"/>
1
votes

It has been solved when I added the NativeExpressAdView programmatically and removed it from XML as follows.

nativeAd = new NativeExpressAdView(this);
int widthInDP = context.getResources().getConfiguration().screenWidthDp;
    widthInDP -= context.getResources().getDimension(R.dimen.activity_horizontal_margin);
nativeAd.setAdSize(new AdSize(widthInDP, 250)); 
nativeAd.setAdUnitId("myAdUnitId");

// Create an ad request.
AdRequest.Builder adRequestBuilder = new AdRequest.Builder();

// Optionally populate the ad request builder.
adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);

// Add the NativeExpressAdView to the view hierarchy.
layout.addView(nativeAd);

// Start loading the ad.
nativeAd.loadAd(adRequestBuilder.build());