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.