I recently migrated my project from AdMob SDK to Google service Ads, however I got a tiny problem with the Google service Ads.
I have a banner ad view in the activity, and I would dynamically adjust the buttons in the layout depending on whether or not an ad is loaded. It worked fine when I used AdMob SDK, but now with Google service Ads, the banner is always reserved there with blank before the ad is loaded. And if the ad cannot be loaded (say without network), the blank view is there, which is rather ugly! This is also why I would like to adjust the layout dynamically...
Did I missed anything while I changed the code? Thank you for help!
Here is an excerpt of the java code and layout file:
Java:
import com.google.android.gms.ads.*;
...
public class MyActivity extends Activity {
...
@Override
public void onCreate(Bundle savedInstanceState) {
...
AdView adView = (AdView)findViewById(R.id.ad);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
}
Layout XML:
...
<com.google.android.gms.ads.AdView
android:id="@+id/ad"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adUnitId="AD_PUB_ID"
ads:adSize="BANNER"/>
</RelativeLayout>
AdMob helps you earn revenues by having other ads that show within your app on the mobile. Google Ads on the other hand promotes products and services on Google Search, YouTube, and other platforms across the web.
Test devices You can configure your device as a test device and use your own ad unit IDs that you've created in your AdMob account. When you enable a test device, the AdMob Network sends production ads in test mode to your device using the ad unit IDs you've created in your AdMob account.
Ads won't show if you haven't integrated the Google Mobile Ads SDK correctly. Is your ad implementation code working properly? Test your implementation code to check that ads can show. You can also use ad inspector to test your app's ad serving.
Your apps may experience less demand from Google while we evaluate your traffic quality. This can occur for up to one week after a new app or ad unit is created. If you still have low match rate after one week, use the following guide to understand common reasons for low match rate.
You can add an AdListener, and make the ad visible only when it receives an ad.
The code looks like this:
final AdView ad = new AdView(context);
ad.setAdUnitId(publisherId);
ad.setAdSize(AdSize.SMART_BANNER);
final AdListener listener = new AdListener() {
@Override
public void onAdLoaded() {
ad.setVisibility(View.VISIBLE);
super.onAdLoaded();
}
};
ad.setAdListener(listener);
ad.setVisibility(View.GONE);
adParent.addView(ad);
ad.loadAd(new AdRequest.Builder().build());
Since there is/was an issue, that an AdMob ad generates an ANR, when it is destroyed when still loading. I normally also check on the visibility of the ad, before calling pause()
or destroy()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With