I have two advertisement on the same page but i want to create one advertisement.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background1"
tools:context=".Search">
<RelativeLayout
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<com.google.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="MY - ID"
ads:loadAdOnCreate="true"/>
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scrollingCache="false"
android:layout_above="@+id/adView">
</ListView>
</RelativeLayout>
</RelativeLayout>
I added the advertisement this way but i guess i have to add java code for this advertisement.However,if i add java code i have two advertisement top and bottom. xml code provides bottom advertisement to show advertisement.xml code is enough to add advertisement ?
Java code is here :
RelativeLayout RelativeLayout1 = (RelativeLayout)findViewById(R.id.RelativeLayout1);
AdView adView=new AdView(this,AdSize.SMART_BANNER,"MY - ID");
RelativeLayout1.addView(adView);
AdRequest request = new AdRequest();
adView.loadAd(request);
Based on the comments you replied,
Yes, you only need one or the other.
You do not need both .
To explain the code below I have added comments:
//Obtains your main layout.
RelativeLayout RelativeLayout1 = (RelativeLayout)findViewById(R.id.RelativeLayout1);
//Creates a new AdView
AdView adView=new AdView(this,AdSize.SMART_BANNER,"MY - ID");
//Adds this new adView, to your page
RelativeLayout1.addView(adView);
//Gets a new ad request
AdRequest request = new AdRequest();
//Tells the new adView to show the ad.
adView.loadAd(request);
From this, we can see that it is creating a new AdView
, adding it to the page, then loading an ad.
Where-as, using just the xml, an Adview
already exists.. So there is no reason to create a new one. and having ads:loadAdOnCreate="true"
in xml, loads the ad for you.
They are two ways, of achieving the same thing.
ps I prefer the xml way of doing it.
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.