Banner Ads

Banner ads are small advertisements usually appear at the top or bottom of the app's screen.

Prerequisites

Before integrating banner ads in your app, you’ll need to go through the steps in our Getting Started Guide to integrate the SDK into your project.

Load Banner Ads

To retrieve and display a banner ad, you will use an instance of YaftaMobileBannerView, which can be created either in code or XML. To load an ad, call the requestAd() method, which uses a background thread to request an ad from the YaftaMobile Network. Only one ad can be loading or displayed by a given YaftaMobileBannerView at a given time.

Method 1, using layout xml file

First, use this code, in the layout XML file of the relevant activity, in order to place a banner at the location you’ve chosen:

<com.mustamara.yaftamobile.sdk.ads.banner.YaftaMobileBannerView
android:id="@+id/bannerView"
android:layout_width="320dp"
android:layout_height="50dp"
yaftamobile:adSize="S320X50"
/>

Then, add the following code in your Activity class:

YaftaMobileBannerView bannerView;
bannerView= (YaftaMobileBannerView)findViewById(R.id.BannerView);
bannerView.requestAd(new AdRequest());

Method 2, using only code

Alternatively, you can use pure code only:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

/* Programmatically create the YaftaMobileBannerView */
YaftaMobileBannerView bannerView;
bannerView = new YaftaMobileBannerView(this,AdSize.S320X50);

RelativeLayout layout;
layout = (RelativeLayout) findViewById(R.id.bannerLayout); 
/* Set the correct width and height of the ad */
RelativeLayout.LayoutParams lp;
lp = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
/* Add view to layout */
layout.addView(this.bannerView, lp);

bannerView.requestAd(new AdRequest());
}

Banner Sizes

The YaftaMobile Android SDK supports both flexible and standard banner sizes. The flexible banner size ensure that the size is optimal and adapts to different screen sizes. With the flexible banner size the SDK will automatically choose the best size fit for the user's device. The size chosen is based on the screen dimensions and pixel density of the device loading the ad, in addition developers can manually choose the appropriate banner size. 

To specify the banner size pass the size to setAdSize() method or through XML when defining your YaftaMobileWebView in either Java or XML.  

The following ad sizes are supported:

Size (WxH) Availability AdSize Constant
300x50 SmartPhones and Tablets S300X50
320x50 SmartPhones and Tablets S320X50
468x60 SmartPhones and Tablets S468X60
728x90 Tablets S728X90

 

Important: If your YaftaMobileBannerView is too small then no ad will be returned and ad loading will fail. You can ensure that your YaftaMobileBannerView is large enough for most scenarios by creating a screen-wide YaftaMobileBannerView with its width set to MATCH_PARENT and its height to WRAP_CONTENT.

Example of how you would set the flexible size within Java code:

bannerView.setAdSize(AdSize.FLEXIBLE);

or within an XML layout file:

<com.mustamara.yaftamobile.sdk.ads.banner.YaftaMobileBannerView
...
yaftamobile:adSize="FLEXIBLE"
...
/>

To manually specify the ad size use the following:

bannerView.setAdSize(AdSize.S320X50);


Alternatively, you can also specify the size within an XML layout file:

<com.mustamara.yaftamobile.sdk.ads.banner.YaftaMobileBannerView
...
yaftamobile:adSize="S320X50"
...
/>

Event Tracking

 

During the process of serving the ads, You may need to know when an ad has shown, has failed, or the user clicked on an ad (and is about to leave your app). You can attach an AdListener to the YaftaMobileBannerView to receive callbacks for when these events occur.


AdListener includes the following methods:

public void onAdLoaded()

Called when an ad is successfully loaded.

public void onError(AdError adError)

Called when a failure occurred during ad loading.

public void onAdClicked()

Called when an ad is clicked.


To listen for events, you’ll need to implement the listener interface:

public class MainActivity extends Activity implements AdListener {
@Override
public void onAdLoaded(){
Toast.makeText(getApplicationContext(),
"Banner successfully loaded.", Toast.LENGTH_SHORT).show();
}

// ... other AdListener methods ...
}


and pass your Activity to the YaftaMobileBannerView

bannerView.setAdListener(this);

Advanced Settings

 

Refresh Rate

Refresh rate is the interval (in seconds) until the YaftaMobileBannerView refreshes and displays a new ad. The refresh rate must be between 30 and 120 seconds and will default to 60 seconds if not set. The default refresh rate can be changed using the setRefreshRate() method, which takes seconds as an argument.

Example of how to set the refresh rate to 45 seconds within Java code:

bannerView.setRefreshRate(45); //45 seconds

 


or alternatively within an XML layout file:

<com.mustamara.yaftamobile.sdk.ads.banner.YaftaMobileBannerView
...
yaftamobile:refreshRate="45"
...
/>