7

I am trying to display an advertisement using Greystrip in AndEngine.

I cannot figure out how this is done because it doesnt use a Layout to inflate views, but yet sprites.

i use BaseGameActivity to create my application for each scene i would like to display adds on.

In GreyStrip this is how they tell you to integrate ads in your application..

Before adding calls in your application to GSSDK, you need to incorporate the SDK into your AndroidManifest.xml. Add the following in the section, replacing with a package identifier that is unique to your application. This Content Provider manages local storage of ad content, while the Activity manages ad display.

 <provider android:name="com.greystripe.android.sdk.AdContentProvider"
    android:authorities="<YOUR_APPLICATION_PACKAGE>.AdContentProvider"
android:multiprocess="true"
android:exported="false" />
<activity android:name="com.greystripe.android.sdk.AdView"
android:configChanges="keyboard|keyboardHidden|orientation" >
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

To initialize the Greystripe SDK, call the initialize method at startup. This should be done within your application’s onCreate() method. This call will spawn a background thread to initialize our activity, and then return control to your app. In this background, the Greystripe activity will download ads as well as any SDK updates. Parameters: ctx: Your application Context instance appId: Use the appId provided during app registration. Providing an invalid appId will cause the SDK to display error notification ads.

 public static GSSDK initialize(Context ctx, String appId)

To use a banner, place the following in your main.xml file:

<view class="com.greystripe.android.sdk.BannerView"
android:id="@+id/gsBanner"
android:layout_width="320dp"
android:layout_height="48dp"/>

To reference the banner view in code, use findViewById, as with any main.xml element:

BannerView myBanner = (BannerView) findViewById(R.id.gsBanner);

To request adds call

myBanner.refresh();

Now the problem is since i dont have an xml layout i cant figure out how i inflate the layout for the ad view?

Anyone have any ideas?

EDIT:

Ive seen someone do it like this in a tutorial online, but how can i inflate this in andengine?

try {
    String applicationId = Utils.scrapeIgnoreCase(externalParams, "<param name=\"id\">", "</param>");           
    GSSDK.initialize(context, applicationId);

    BannerView myBanner = new BannerView(context);          
    myBanner.setLayoutParams(view.getLayoutParams());
    myBanner.addListener(new GreyStripeBannerListener());           
    view.addView(myBanner);
    myBanner.refresh();
    myBanner.setOnClickListener(new  View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Click();
        }
    });
coder_For_Life22
  • 26,645
  • 20
  • 86
  • 118
  • What kind of layout you have? At least you have to have some sort of Linear/Relative Layouts to arrange your components on the screen. If so just create LayoutParams lp ... and then mymainview.addView(myBanner,lp); and get on with it. – Sergey Benner Jan 20 '12 at 11:29
  • With GreyStripe you use BannerView. As I have in my question. Check my update..The problem is i cant figure out how to integrate this with AndEngine – coder_For_Life22 Jan 20 '12 at 11:47
  • what is you onCreate() in AdView and how you set your setContentView() in there? – Sergey Benner Jan 20 '12 at 11:53
  • 1
    I think this one could be your case http://stackoverflow.com/questions/8237346/showing-adds-in-gameplay-in-andengine-android – Sergey Benner Jan 20 '12 at 12:01
  • The last answer on the page looks like somethings i would need. How would it look as far as BannerView? You could just give me the pseudo code that would be great. – coder_For_Life22 Jan 20 '12 at 13:23

1 Answers1

6

I'm using AdMob but it should be similar.

Like @Sergey Benner referenced, you have to override onSetContentView in your activity, then create the RenderSurfaceView and your ad view manually.

First of all, create a FrameLayout to contain AndEngine's view and the ad view. Add AndEngine's view and create your ad view, then set the frame layout as the content view.

@Override
protected void onSetContentView() {
    //Creating the parent frame layout:
    final FrameLayout frameLayout = new FrameLayout(this);
    //Creating its layout params, making it fill the screen.
    final FrameLayout.LayoutParams frameLayoutLayoutParams =
            new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,
                    FrameLayout.LayoutParams.FILL_PARENT);

    //Creating the banner view.
    BannerView bannerView = new BannerView(this);

    //....
    //Do any initiallizations on the banner view here.
    //....

    //Creating the banner layout params. With this params, the ad will be placed in the top of the screen, middle horizontally.
    final FrameLayout.LayoutParams bannerViewLayoutParams =
            new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
                    FrameLayout.LayoutParams.WRAP_CONTENT,
                    Gravity.TOP | Gravity.CENTER_HORIZONTAL);

    //Creating AndEngine's view.
    this.mRenderSurfaceView = new RenderSurfaceView(this);
    mRenderSurfaceView.setRenderer(mEngine, this);

    //createSurfaceViewLayoutParams is an AndEngine method for creating the params for its view.
    final android.widget.FrameLayout.LayoutParams surfaceViewLayoutParams =
            new FrameLayout.LayoutParams(super.createSurfaceViewLayoutParams());

    //Adding the views to the frame layout.
    frameLayout.addView(this.mRenderSurfaceView, surfaceViewLayoutParams);
    frameLayout.addView(bannerView, bannerViewLayoutParams);

    //Setting content view
    this.setContentView(frameLayout, frameLayoutLayoutParams);
}

Place this method in your BaseGameActivity class.

Crawler
  • 1,988
  • 3
  • 21
  • 42
Jong
  • 9,045
  • 3
  • 34
  • 66
  • 1
    Jong Saves The Day..AGAIN! (= Dude your awesome. You really help me out alot. Cant thank you enough. Also in frameLayout.addView(this.mAdView, bannerViewLayoutParams); should be frameLayout.addView(this.bannerView, bannerViewLayoutParams); correct? – coder_For_Life22 Jan 20 '12 at 13:41
  • 1
    Yea, I just forgot changing it (It's from a method in my game, but I used AdMob `AdView`. I'll fix it now :)) – Jong Jan 20 '12 at 13:42
  • Is Your Game On The Market? I Want To Try It Out. – coder_For_Life22 Jan 20 '12 at 13:43
  • Here it is: https://market.android.com/details?id=com.jong.angryballs. I didn't put too much effort in it (My first AndEngine game... maybe 2 weeks), but I did try to use many SDKs to learn different add-ons for games (So here we got AdMob, Facebook and Open Feint working). You should add them too, Open Feint is real cool and easy to add :) – Jong Jan 20 '12 at 13:47
  • Yeah I Actually Am In The Process of Integrating Open Feint As We Speak. Just Waiting For The Review Process To Be Complete. So I Can Test It Out In My Game. I Think it makes games WAY more competitive and drives more user interactivity. – coder_For_Life22 Jan 20 '12 at 13:53
  • Do you design your own graphics? – coder_For_Life22 Jan 20 '12 at 13:55
  • 1
    Trying to :) My cousin helped a bit with these. The graphics in that game are simple and pretty lame so every device can run it. Gameplay is far more important than graphics, imo. – Jong Jan 20 '12 at 14:02
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/6892/discussion-between-coder-for-life22-and-jong) – coder_For_Life22 Jan 20 '12 at 14:04
  • Is there anyway i can make he BannerView layout bigger as in height and width? based on the screen size? – coder_For_Life22 Jan 20 '12 at 16:39
  • The banner view usually has a constant size, which is determined by the type of ad you are displaying. – Jong Jan 20 '12 at 21:09
  • //Creating AndEngine's view. this.mRenderSurfaceView = new RenderSurfaceView(this); mRenderSurfaceView.setRenderer(mEngine, //set render listener); its looking for another parameter RenderListener? – coder_For_Life22 Jan 20 '12 at 21:18
  • Lets talk on the chat. (The chat room you created earlier) – Jong Jan 21 '12 at 09:31
  • I am facing same issue, I posted my code here can you please suggest some solution. http://stackoverflow.com/questions/29607706/unable-to-display-game-screen-while-admob-integrated-with-andengine-google-pla – Tushar Kanani Apr 13 '15 at 14:27
  • I tried your code, it compiled successfully but there is no adview when i deploy my game on device. Is there other stuff that need to be done to make adView appear apart frm above code. – Crawler Aug 21 '15 at 12:37