The section below illustrates the simplest way to add Banner Ads to your application
A – Without XML layout
Add the following snippet of code to your activity to add, initialize and load ad view.
import java.text.SimpleDateFormat; import java.util.Date; import java.util.Vector; import android.widget.LinearLayout; import com.noqoush.adfalcon.android.sdk.ADFListener; import com.noqoush.adfalcon.android.sdk.ADFTargetingParams; import com.noqoush.adfalcon.android.sdk.ADFView; import com.noqoush.adfalcon.android.sdk.constant.ADFAdSize; import com.noqoush.adfalcon.android.sdk.constant.ADFErrorCode; . . . // Add private member variable of type ADFView private ADFView adFalconView; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); try { // create new instance of ADFView adFalconView = new ADFView(this); //Ensure test mode is set to false before your app is released adFalconView.setTestMode(false); // initialize the view by passing a site id, ad unit size and enable auto refresh. // then load first ad adFalconView.initialize("your site ID", ADFAdSize.AD_UNIT_320x50, null, null, true); // Add ADFView to this activity. // we consider there is a layout naming linearLayout and // you want to add ADFView in this layout ((LinearLayout) findViewById(R.id.linearLayout)).addView(adFalconView); } catch (Exception ex) { } } @Override protected void onDestroy() { adFalconView.destroy(); super.onDestroy(); }
Note: Refer to the appendix A ADFAdSize for the available Ad Unit sizes.
Note: Ensure test mode is set to true only during development. Before shipping to production set it to false.
B – With XML layout
Follow the steps below to add AdFalcon Banner Ad view:
- Add ADFView to your activity XML layout
<com.noqoush.adfalcon.android.sdk.ADFView android:id="@+id/adFalconView" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
- Add the following snippet of code to your activity class to initilize, load and render the ad view
import java.text.SimpleDateFormat; import java.util.Date; import java.util.Vector; import android.widget.LinearLayout; import com.noqoush.adfalcon.android.sdk.ADFListener; import com.noqoush.adfalcon.android.sdk.ADFTargetingParams; import com.noqoush.adfalcon.android.sdk.ADFView; import com.noqoush.adfalcon.android.sdk.constant.ADFAdSize; import com.noqoush.adfalcon.android.sdk.constant.ADFErrorCode; . . . // Add private member variable of type ADFView private ADFView adFalconView; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); try { // create new instance of ADFView adFalconView = ((ADFView)findViewById(R.id.adFalconView)); //Ensure test mode is set to false before your app is released adFalconView.setTestMode(true); // initialize the view by passing a publisher id, ad unit size, params, // listener and enable auto refresh. // then load first ad adFalconView.initialize("your site ID", ADFAdSize.AD_UNIT_320x50, null, null, true); } catch (Exception ex) { } } @Override protected void onDestroy() { adFalconView.destroy(); super.onDestroy(); }
Note: Refer to the appendix A ADFAdSize for the available Ad Unit sizes.
Note: Ensure test mode is set to true only during development. Before shipping to production set it to false.
Advanced Banner Ads Options
Tracking Ad Lifecycle Events – ADFlistener
ADFListener provides ability to track Ad lifecycle events. Implement the following abstract methods.
public class MainActivity extends Activity implements ADFListener{ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); try { // create new instance of ADFView adFalconView = ((ADFView)findViewById(R.id.adFalconView)); //Ensure test mode is set to false before your app is released adFalconView.setTestMode(true); // initialize the view by passing a publisher id, ad unit size, params, // listener and enable auto refresh. // then load first ad adFalconView.initialize("your site ID", ADFAdSize.AD_UNIT_320x50, null, this, true); // set the listener to ADFView adFalconView.setListener(this); } catch (Exception ex) { } } public void onLoadAd(ADFAd ad) { } public void onError(ADFAd ad, ADFErrorCode code, String message) { } public void onPresentAdScreen(ADFAd ad) { } public void onDismissAdScreen(ADFAd ad) { } public void onLeaveApplication() { } . .
ADFAdSize
ADFAdSize Enum defines the ad units supported by AdFalcon.
Enum | Name | Size | Devices |
AD_UNIT_AUTO_BANNER | Auto Banner | The server will detect the best ad unit size based on the screen size and orientation. | All |
AD_UNIT_320x50 | Standard | 320 x 50 | All |
AD_UNIT_300x250 | Medium Rectangle | 300 x 250 | All |
AD_UNIT_468x60 | Full Banner | 468 x 60 | Tablet |
AD_UNIT_728x90 | Leaderboard | 728 x 90 | Tablet |
AD_UNIT_120x600 | Skyscraper | 120 x 600 | Tablet |
Refresh Ad
Use the below methods to configure ad refresh parameters.
Method | Description |
public void refreshAd() | Used to get a new ad programmatically. |
public void setRefreshDuration(
int duration) |
Determine the refresh duration in seconds among the receivedAds.
Note: The duration range starts from 30 to 120 seconds. |
public void setEnableAutoRefresh(
boolean enable) |
Used to enable/disable the auto refresh for the received ads. |
Disable Hardware Acceleration
If you noticed flickering in the ad view or any other user interface element in your activity, you can disable hardware acceleration by implementing the below snippet of code. However, this is not recommended because HTML 5 video ads will not play correctly.
ADFView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);