简体   繁体   中英

How to display google ads in Android using XML only?

I want to place a google ad in my project. But for some reason, I don't want to use Java language for this purpose. I have already tried the ads:loadAdOnCreate="true" attribute, but I think it is not working now. Is there any idea by which I can display ads by using XML only ?

loadAdOnCreate is no longer available in new SDK, you need to write java code to display your ads.

For ads integration you can check Harshad's answer

From official Doc by Amy Quispe (AdMob SDK Team)

loadAdOnCreate was deprecated in the Google Play Services version, so you'll have to load the ad from the code.

您应该使用旧的google ads库,以便loadAdOnCreate可用。

Use below code in xml say xml name is adview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/adView"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="50dip"/>

Now in your Activity load the ad.

AdView adView = new AdView(this,AdSize.BANNER,"yourid");
 LinearLayout ll= (LinearLayout) findViewById(R.id.adView);
 ll.addView(adView);
 AdRequest ar = new AdRequest();
 ar.setGender(AdRequest.FEMALE);
 adView.loadAd(ar);

You can include this view(xml) in any of your activity from xml like this..

     <include layout="@layout/adview" />     // your xml

more detail visit here. Admob integration with simple-xml in Android?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM