XML or Java Keywords in Android for Admob
I can't seem to find any rock solid documentation on开发者_StackOverflow setting keywords for adMob ads via XML or Java. I've tried the methods seen in this thread, but have failed in both respects. My failures for each case are below.
When trying to use methods setKeywords(), setSearchQuery(), or requestFreshAd() from the AdView class, it's like Eclipse doesn't recognize that those methods are part of the AdView class. Could I be instantiating this class improperly? My java code is below:
import com.google.ads.AdRequest;
import com.google.ads.AdView;
AdView adView = (AdView)this.findViewById(R.id.adView);
adView.loadAd(new AdRequest());
If I try to call the aforementioned methods, (eg. adView.setKeywords() ), Eclipse doesn't recognize the methods. What am I doing wrong?
When I attempt to set the keywords via XML, the code seems to run ok, but I don't get any valid requests for an add. The log returns with a "No ad to display" message. In this case, I don't know if I'm just not getting a result from my keywords or if I'm not settings the keyword string correctly.
The following depicts how I'm setting the keywords via XML:
...in attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="com.google.ads.AdView">
<attr name="adSize">
<enum name="BANNER" value="1"/>
<enum name="IAB_MRECT" value="2"/>
<enum name="IAB_BANNER" value="3"/>
<enum name="IAB_LEADERBOARD" value="4"/>
</attr>
<attr name="adUnitId" format="string"/>
<attr name="test" format="boolean"/>
<attr name="keywords" format="string"/>
</declare-styleable>
</resources>
in layout.xml....
<com.google.ads.AdView android:id="@+id/adView"
android:layout_width="fill_parent"
android:layout_gravity="bottom"
android:layout_height="wrap_content"
ads:adUnitId="blahblahblhal"
ads:adSize="BANNER"
ads:test="true"
ads:keywords="farts cheese stink pooper"/>
Obviously, these are not the keywords I'm using, but the syntax by which I set the keywords is the same as I've been using.
If there is no ad to display is it because I'm using too many keywords, improper syntax, and what if I want to use a phrase instead of a keyword? How would I go about doing that?
If there is documentation on the AdMob site pertaining to this I apologize. But if so, they made it pretty damn hard to find.
Thanks
setKeywords
is a method of the AdRequest
class, not AdView
.
AdRequest request = new AdRequest();
Set<String> keywords = new HashSet<String>();
keywords.add("keyword1");
keywords.add("keyword2");
request.setKeywords(keywords);
adView.loadAd(request);
Google added the ability to specify keywords as an XML attribute in version 4.1.1 of the Admob SDK (see Release Notes in [1]). Unfortunately it is not documented, but in [2] you can see a response from Google that clarifies the name of the XML attribute. The correct attribute is "ads:keywords", and it accepts a comma-separated list of keywords. Here is an example:
<com.google.ads.AdView
android:id="@+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adUnitId="YOUR_PUBLISHER_ID"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"
ads:keywords="keyword1,keyword2,keyword3"
/>
[1] http://code.google.com/mobile/ads/docs/rel-notes.html
[2] http://groups.google.com/group/google-admob-ads-sdk/browse_thread/thread/b9b0b81858234adb
精彩评论