> For the complete documentation index, see [llms.txt](https://docs.blueseasx.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.blueseasx.com/android/banner-ads.md).

# Banner Ads

### **Step1. Load**

Before requesting ads, please ensure that the SDK has been initialized.

To request banner ads, declare a `MediationInfo` object. This object should include the ad mediation platform, the mediation version, and the BlueSea Adapter version used when requesting the ads. If you are integrating BlueSea SDK through your own mediation platform, it is recommended to declare the `MediationInfo` object and pass in the mediation information to facilitate monetization data management and channel monetization optimization. If you are integrating BlueSea SDK through third-party mediations like MAX, LevelPlay, etc., you can skip this step.

```
MediationInfo mediationInfo = new MediationInfo("<in house or mediation name>","<mediation-version-name>", "<adapter-version-code>");
```

* `<in house or mediation name>`: Pass the value as "in-house" or the specific mediation platform name.
* `<mediation-version-name>`: Pass the version number of the mediation SDK.
* `<adapter-version-code>`: Pass the version number of the adapter.

To request an ad, create a `BSXAdView` with the specified Placement and call the `loadAd()` method. Add the `BSXAdView` as a child of your parent layout when you’re going to display the ad. You should also set a `BSXAdViewListener` to track ad load status and other related events. The following example shows how to request an ad:

```
public class ExampleActivity extends Activity
        implements BSXAdViewAdListener
{
  private BSXAdView adView;

  void createBannerAd()
  {
    adView = new BSXAdView(this, "«BlueSea-pid»" );
    adView.setAdViewListener( this );
    adView.setMediationInfo( mediationInfo );

    // Load the ad
    adView.loadAd();
  }

  // Ad Listener
  @Override
  public void onAdLoaded() {}
        // the ad is loaded successfully

  @Override
  public void onAdLoadError(AdError error) {}
        // the ad fails to load

  @Override
  public void onAdDisplayFailed(AdError error) {}
        // an error occurs when displaying the ad

  @Override
  public void onAdClicked() {}
        // the ad is clicked by user

  @Override
  public void onAdDisplayed() {}
        // an ad impression occurs
}
```

`BSXAdViewListener` callback:

* `onAdLoadError(AdError error)`: Indicates that no ads were filled. This may be due to the advertiser not bidding or other issues. You can check the error code and message using `AdError.getCode()` and `AdError.getMessage()`. For details on error codes, refer to xxx
* `onAdLoaded()`: Indicates that the ad was successfully filled

### **Step2. Show**

Once you receive the `onAdLoaded()` callback, use the addView() method to add the `adView` to your window, and set a `BSXAdViewListener` to listen for ad events.

```
public class ExampleActivity extends Activity
        implements BSXAdViewAdListener
{
  private BSXAdView adView;

  void showBannerAd()
  {


    ViewGroup rootView = findViewById( android.R.id.content );
    AdSize adSize = mBannerAdView.getAdSize();
    FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
       AdSize.dp2px(this, adSize.width),
       AdSize.dp2px(this, adSize.height),
       Gravity.CENTER_HORIZONTA | Gravity.BOTTOM
     );

    rootView.addView( adView );
  }

  // Ad Listener
  @Override
  public void onAdLoaded() {}
        // the ad is loaded successfully

  @Override
  public void onAdLoadError(AdError error) {}
        // the ad fails to load

  @Override
  public void onAdDisplayFailed(AdError error) {}
        // an error occurs when displaying the ad

  @Override
  public void onAdClicked() {}
        // the ad is clicked by user

  @Override
  public void onAdDisplayed() {}
        // an ad impression occurs
}
```

Before displaying the ad, call `BSXAdView.isExpired()` to check whether the ad has expired (the typical validity period is 1 hour). If the ad has expired, request a new one.

### **Step3. Destroy**

If you no longer need the ad, you can destroy it using the following method:

```
public class ExampleActivity extends Activity
        implements BSXAdViewAdListener
{
  private BSXAdView adView;

  void destroyBannerAd()
  {
    ViewGroup rootView = findViewById( android.R.id.content );
    rootView.removeView( adView );
    adView.destroy();
    adView = null;
  }
```
