Admob integration with simple-xml in Android?

北战南征 提交于 2019-12-06 12:27:47

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

I have 2 possible ideas for you:

  1. Delete the "zzzzzzzzzz" from the xml.

or

  1. Make sure you are using SDK 3.2 or above. I recently solved a similar problem with this method.

Try this:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

    <com.google.ads.AdView
        android:id="@+id/ad"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/admob_publisher_id"
        ads:loadAdOnCreate="true" >
    </com.google.ads.AdView>

</RelativeLayout>

AdView adView = (AdView) findViewById(R.id.ad);
AdRequest adRequest = new AdRequest();
adView.loadAd(adRequest);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!