Tapjoy Android Plugin - Closing an ad banner

Hey guys,

I managed to get the Tapjoy plugin up and working and managed to do a couple of things with it to make it more how I need it to work. However, I haven’t been able to get the banner ads from stopping from showing up.

I’ve tried to create some functionality in the plugin and building it into the Tapjoy.jar file but how I tried to implement this seems to be causing some problems.

Basically, I created a function inside of TapjoyDisplayAd.java that should make the adView (the banner) invisible. This however, causes my application to break if I call it between showing a banner add, and then trying to close it to show the featured ad.

public void hideBannerAd() {
if(adView != null) {
autoRefresh = false;
adView.setVisibility(View.GONE);
}
}

So, my order of operations is:

  • App Launches
  • Main menu → Start Game → Shows banner Ad
  • User presses Start Game → Stop/Hide Banner Ad → Show featured Ad
  • Close feature ad → Game Starts
  • Game Ends → Show featured ad
  • Featured ad closes → Show game over screen + Show banner ad

Can anyone point me in the right direction of what I’m supposed to do to be able to close or temporarily hide the banner ads so I can let my players enjoy the game without showing a banner ad?

Thanks.

I’m not sure what your actual implementation is, but you mean the small banners right (300x50 ones, or whatever it was)?

usually you call them like this in onCreate or

        TapjoyConnect.requestTapjoyConnect(this, APP_ID, SEC_KEY);
        tapjoy = TapjoyConnect.getTapjoyConnectInstance(); // This is a member variable of your activity class: private TapjoyConnect tapjoy;

        // This will request one Ad. 
        tapjoy.getDisplayAd(new TapjoyDisplayAdNotifier() {
			@Override
			public void getDisplayAdResponseFailed(String error) {
				// TODO Auto-generated method stub
			}
			@Override
			public void getDisplayAdResponse(View adView) {
				// Add the adView here to your view hierarchy, i.e. if you have a LinearLayout with the id "adView" you do it like
				LinearLayout adLayout = (LinearLayout)findViewById(R.id.adView);
				adLayout.removeAllViews(); // removes all child views
				adLayout.addView(adView); // ads your banner to it
			}
		});

This usually creates one tapjoy banner per start. you shouldn’t call this multiple times, unless you want to “rotate” the banners. You can of course place this code in a “requestNewAd” method in your Activity and call it via JNI from Unity whenever you want a new ad view. You can also add a flag in the code above, to check if it’s true or not before adding the adView.

You really shouldn’t mess around with the tapjoy sdk files

I think this is what I was looking for:

LinearLayout adLayout = (LinearLayout)findViewById(R.id.adView);
adLayout.removeAllViews(); // removes all child views

I understand your cautions in regards to the tapjoy plugins, however certain things just aren’t working and I deem them that they need to get adjusted:

If I don’t modify the plugin, I don’t see how I can remove the banner ad while in the game since that functionality doesn’t seem to come with the plugin. Once you get that banner running, it’s stuck up there. I need to change the plugin in order to get that functionality in place.

Here is one of the things, it’s basic and trivial, but with a few lines, now Tapjoy’s ads center themselves automatically so that they fit in every device, even if the device is bigger than the available banner.

Line 93 TapjoyPluginActivity.java
Inside of:
private void updateResultsInUi()

// after - Resize banner if it’s too big.
// center logo after any adjustments
ad_width = bannerView.getLayoutParams().width;
int xPos = (screenWidth-ad_width)/2;
TapjoyConnect.getTapjoyConnectInstance().setBannerAdPosition(xPos, TapjoyConnect.getTapjoyConnectInstance().getBannerY());

This causes them to always be centered on different devices. This just looks a lot better and it’s a pretty easy addition.

If you don’t want your Tapjoy ads to “rotate”, you can also of course just hide the LinearLayout

if(showAds) { 
   adLayout.setVisibility(View.VISIBLE);
} else {
   adLayout.setVisibility(View.GONE); // There are INVISIBLE and GONE as options. Invisible just hides the view, but keeps the place reserved/empty, while GONE will collapse the view and make the space available for other views
}

The xml files are like prefabs in Unity. You can design the layout in the XML file and inflate it via code in a single line of code

As for the above one, you don’t fondle around in TapJoy code at all, and even worse, you don’t use absolute positions XD

Android GUI is already designed to be flexible.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center_horizontal"
     >
        <Button
            android:id="@+id/button1"
            android:layout_width="300px"
            android:layout_height="50px"
            android:text="Button" />
</LinearLayout>

The first LinearLayout is uses full width of your phone. Gravity alligns all of it’s contents in the center. The button is just an example to simulate your ad. This one could be saved as “ad_layout.xml” for example.

Inside your onCreate Method you can add this to your current content view. TapJoy lets you also set the adsize

public class MyGame extends UnityPlayerActivity {
    private LinearLayout adsLayout;

    protected void onCreate(Bundle savedInstanceState) {
        // inflates the view from the xml layout file
        adsLayout = (LinearLayout)LinearLayout.inflate(this, R.layout.ad_layout,null); // R.layout.ad_layout is a reference to the ad_layout.xml file in the "res/layout" folder 

        // tapjoy initialization
        ... 
        tapjoy.setBannerAdSize(TapjoyDisplayAdSize.TJC_AD_BANNERSIZE_320X50);
        // TapjoyDisplayAdSize.TJC_AD_BANNERSIZE_640X100 for tablets or high resolution phones, double of the standard size
        // TapjoyDisplayAdSize.TJC_AD_BANNERSIZE_768X90 for tablets too, it's the classical leaderboard know from websites

        tapjoy.getDisplayAd(new TapjoyDisplayAdNotifier() {
            @Override
            public void getDisplayAdResponseFailed(String error) {
                // TODO Auto-generated method stub
            }
            @Override
            public void getDisplayAdResponse(View adView) {
                // Add the adView here to your view hierarchy, i.e. if you have a LinearLayout with the id "adView" you do it like
                adsLayout.removeAllViews(); // removes all child views
                adsLayout.addView(adView); // ads your banner to it
            }
        });
    }

    public void showAds() {
        adsLayout.setVisibilit(View.VISIBLE);
    }
    public void hideAds() {
        adsLayout.setVisibilit(View.GONE);
    }
}

And it should be centered automatically, because the LinearLayout is set that way. You just need to get familiar with the design philosophy. It’s quite similar to WPF/WAML, which both avoid fixed positions (pixels, mm, cm etc.) and use stuff like “Auto”, “fill_parent”, “match_content”, margins, paddings etc.

Hey Tseng,

Thanks a ton for the information. This is all super useful (i’m not too familiar with android development) and it’s especially nice since I can reference your code for more information.

Extra thanks for how layout.xml works in conjunction with the java, I’ll have to sink my teeth more into this stuff to hopefully figure out how to do the rest of the things that I need to.

Cheers!