Tuesday, 17 April 2012

How to add AdWhirl to an Android app (part 2)

After my earlier post, I found another way of doing this which relies on setting the ad placement in a layout.  This also is pretty easy

As I was getting poor results from AdMob I decided to add AddWhirl to my android app so I could use multiple ad providers.  Little did I know that the documentation for AdWhirl was going to be so bad!  A google search didn't help much, just lots of other people wanting to do the same and getting stuck or confused.  The official instructions didn't help much (https://www.adwhirl.com/doc/android/AdWhirlAndroidSDKSetup.html) - as soon as I had started I got a compile error because of something ommitted from the instructions.

So I set out to solve the problem on my own.

1. Set your app up on your chosen Ad provider website (or multiple, but for this explanation I'll go with Admob).  You'll need the ID

2. Set your app up on the AdWhirl website.  Add the ad provider (in this case AdMob) and enter the ID.  Switch it on!  You'll need the AdWhirl ID later, but not the AdMob one as AdWhirl hadles that for you

3. Download the AdMob (or whoever) SDK and add it to the libs folder of your Android app

4. Do the same for AdWhirl

5. Add the jars to the build path of your app (I'm using Eclipse so right click > properties > Java build path > Add Jars

6. Create a layout that includes the following element (or add to an existing layout)

    <com.adwhirl.AdWhirlLayout
        android:id="@+id/adwhirl_layout"
        android:layout_width="fill_parent"
        android:layout_height="match_parent" />

7. Alter your activity to implement AdWhirlInterface.  This will cause a compile error as there is method to implement.  I have no idea what it is for, so I added this

@Override
public void adWhirlGeneric() {
// TODO Auto-generated method stub
}

8. If it doesn't already, you need to set the activity to use the layout.  You do this in the onCreate method

/*
* (non-Javadoc)
*
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.<name of layout>);
}

9. Then you need to load the app, which will be placed in the relevant place in the layout


AdWhirlManager.setConfigExpireTimeout(1000 * 60 * 5);
AdWhirlTargeting.setKeywords("some keywords delimited by strings");
// you can add other criteria here such as gender or age
AdWhirlTargeting.setTestMode(true); // change to false before deployment
// this is the layout element we added earlier
AdWhirlLayout adWhirlLayout = (AdWhirlLayout) findViewById(R.id.adwhirl_layout);
adWhirlLayout.setAdWhirlInterface(this);

You can just stick this after the setContentView() in the onCreate() method

7. In your AndroidManifest.xml add any permissions requored by the ad vendor (eg AdMob).  AdMob requires:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

8. Also in AndroidManifest.xml add anything required by the ad provider you are using (so in my case AdMob).

        <activity
            android:name="com.google.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" >
        </activity>

This is necessary because AdWhirl simply mediates the providers, you still need to set the provider-specific activity etc

9. Add your AdWhirl ID to the activity you are loading the ad in (or you cna add it at application level) in AndroidManifest.xml
For example
<activity android:name="your.app.Activity" android:label="@string/app_name"
android:theme="@style/themeBase"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:value="<your adwhirl key" android:name="ADWHIRL_KEY"/>
</activity>

9. Try it (Note that it seems to take AdWhirl some time to update settings so if you get no ads, check everything is set right, then wait a few minutes)

Yes that is it.  The above displays ads from AdMob using AdWhirl in my app.  It took me ages to work out, so hopefully this will help some others out there!

No comments:

Post a Comment