0

As in the Fireflies live wallpaper (http://www.livewallpapers.org/fireflies-free-1543/), I don't know how to put an ads banner in the Preview screen (not Settings screen).

Additional info: when I install the Fireflies wallpaper on my HTC which is on Android 2.2.1 the banner shows up in both Preview and Settings screens, but on my two other Sony Ericsson (on 2.3.3) I can only see the banner in the Setting screens. Does that have something to do with Sony Ericsson or the version of Android?

Thank you.

P.S I did look at this link but found no answer to my question:

How do I put an admob adview in the settings screen for a live wallpaper?

Community
  • 1
  • 1
Tran
  • 135
  • 1
  • 2
  • 7
  • Re: your additional info: It's possible an ad doesn't appear, because they just didn't have an advertisement to show, rather than a difference between phones. I've been using admob and only about 70-80% of requests actually result in an ad display (fill rate %), otherwise the ad view just doesn't appear. Don't know if that helps :) – Jay Sep 17 '11 at 04:34

1 Answers1

0

Create a new class AdPreference.java and include this code:

public class AdPreference extends Preference {

    public AdPreference(Context context, AttributeSet attrs, int defStyle) {super    (context, attrs, defStyle);}
    public AdPreference(Context context, AttributeSet attrs) {super(context, attrs);}
    public AdPreference(Context context) {super(context);}

    @Override
    protected View onCreateView(ViewGroup parent) {
        // this will create the linear layout defined in ads_layout.xml
        View view = super.onCreateView(parent);

        // the context is a PreferenceActivity
        Activity activity = (Activity)getContext();

        // Create the adView
        AdView adView = new AdView(activity, AdSize.BANNER, "YOUR_ADMOB_ID_HERE");

        ((LinearLayout)view).addView(adView);

        // Initiate a generic request to load it with an ad
        AdRequest request = new AdRequest();
        adView.loadAd(request);     

        return view;    
    }
}

In your res/layout folder create a new xml layout titled ad_layout.xml has to be exact. Then include this code:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical">  
</LinearLayout>

Then in your "settings" xml add this right after the xmlns line:

<com.yourpackagename.AdPreference android:layout="@layout/ad_layout"/>

In your AndroidManifest.xml add this before

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

 <meta-data android:name="ADMOB_ALLOW_LOCATION_FOR_ADS" android:value="false" />

Also add this in your manifest:

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

And that's it. It'll work perfectly.

Flexo
  • 87,323
  • 22
  • 191
  • 272
Steve C.
  • 1,333
  • 3
  • 19
  • 50