---
agentTools:
  projectIndex: https://developers.appnext.com/llms.txt
---

# Banners

Appnext Android SDK - Banners ad unit

[block:api-header]
{
  "title": "Ad Unit Integration"
}
[/block]

[block:callout]
{
  "type": "info",
  "title": "SDK Installation",
  "body": "Make sure to Install the SDK in your Android Studio project as described [here](https://developers.appnext.com/docs/android-sdk-installation)"
}
[/block]

\##Step 1 – Initialize the SDK
Initialize the SDK whenever your app is loaded. Add the following code to your app's main activity

[block:code]
{
  "codes": [
    {
      "code": "Appnext.init(context);",
      "language": "java"
    }
  ]
}
[/block]

[block:callout]
{
  "type": "info",
  "body": "New! Get the predicted ECPM value before loading the ad. Check out Appnext solution for in-app header bidding [here](https://developers.appnext.com/docs/appnext-in-app-header-bidding)."
}
[/block]

\##Step 2: Add the BannerView to the layout XML

[block:code]
{
  "codes": [
    {
      "code": " <com.appnext.banners.BannerView\n       xmlns:ads=\"http://schemas.android.com/apk/res-auto\"\n       android:id=\"@+id/banner\"\n       android:layout_width=\"wrap_content\"\n       android:layout_height=\"wrap_content\"\n       ads:bannerSize=\"BANNER\"\n       ads:placementId=\"YOUR_APPNEXT_PLACEMENT_ID\"/>\n",
      "language": "xml"
    }
  ]
}
[/block]

Available sizes for the `bannerSize`

[block:parameters]
{
  "data": {
    "h-0": "Banner Name",
    "h-1": "Banner Size",
    "0-0": "\"BANNER\"",
    "0-1": "320x50",
    "1-0": "\"LARGE_BANNER\"",
    "1-1": "320x100",
    "2-0": "\"MEDIUM_RECTANGLE\"",
    "2-1": "300x250"
  },
  "cols": 2,
  "rows": 3
}
[/block]

[block:image]
{
  "images": [
    {
      "image": [
        "https://files.readme.io/91f9d34-banners_1.png",
        "banners_1.png",
        900,
        599,
        "#c8c9c7"
      ],
      "caption": "Banner Sizes"
    }
  ]
}
[/block]

Make sure to provide a valid Appnext placement id for the `placementId` field

You can also create the view via the code itself:

[block:code]
{
  "codes": [
    {
      "code": "BannerView banner = new BannerView(this);\nbanner.setPlacementId(\"YOUR_APPNEXT_PLACEMENT_ID\");\nbanner.setBannerSize(BannerSize.BANNER);",
      "language": "java"
    }
  ]
}
[/block]

* Make sure to make the banner available in the view hierarchy
* Make sure to set the Appnext placement id once, per each banner, using either the layout xml or by using the code (but not both)

\##Step 3: Load the Banner

[block:code]
{
  "codes": [
    {
      "code": "BannerView bannerView = (BannerView) findViewById(R.id.banner);\nbannerView.loadAd(new BannerAdRequest());",
      "language": "java"
    }
  ]
}
[/block]

\##Step 4: Configure the BannerAdRequest (Optional)

By default, the banner settings will be loaded with pre-defined settings. You can change these settings by defining the `BannerAdRequest` object

Setters that are relevant to all banner sizes:

* `setCategories` - String. Set preferred ad categories
* `setSpecificCategories` - Set specific ad categories
* `setPostback` - String. Postback parameters that will be posted to your server after user installed an app (make sure to encode the values)

Setters that are relevant only to the MEDIUM\_RECTANGLE banner size:

* `setCreativeType` - The MEDIUM\_RECTANGLE size can show video in addition to static creative. When video is in use, the file will be streamed (default value is `ALL` - managed by Appnext)
  * `BannerAdRequest.TYPE_ALL`
  * `BannerAdRequest.TYPE_VIDEO`
  * `BannerAdRequest.TYPE_STATIC`
* `setAutoPlay` - Boolean. When a video is set as the creative, this setter defining if the video will auto play or not (default value is false)
* `setMute` - Boolean. When a video is set as the creative, this setter is defining if the video will start on muted volume or not. Un-mute/Mute button is located on the video creative. (default value is true - video is muted)
* `setVideoLength` - When a video is set as the creative, this setter is defining the preferred length of the video. (default value is SHORT)
  * `BannerAdRequest.VIDEO_LENGTH_SHORT` - Up to 15 seconds long videos
  * `BannerAdRequest.VIDEO_LENGTH_LONG` - 20-60 seconds long videos

[block:code]
{
  "codes": [
    {
      "code": "// All sizes example\nBannerAdRequest banner_request = new BannerAdRequest();\nbanner_request\n       .setCategories(\"category1, category2\")\n       .setPostback(\"Postback string\");\n// ...\nbannerView.loadAd(banner_request);",
      "language": "java"
    }
  ]
}
[/block]

[block:code]
{
  "codes": [
    {
      "code": "// MEDIUM_RECTANGLE size example\nBannerAdRequest banner_request = new BannerAdRequest();\nbanner_request\n  .setCategories(\"category1, category2\")\n  .setSpecificCategories(\"category1, category2\")\n  .setPostback(\"Postback string\")\n  .setCreativeType(BannerAdRequest.TYPE_VIDEO)\n  .setAutoPlay(true)\n  .setMute(false)\n  .setVideoLength(BannerAdRequest.VIDEO_LENGTH_SHORT);\n// ...",
      "language": "java"
    }
  ]
}
[/block]

\##Step 5: Callbacks (Optional)
You can receive client-side callback from the banner by using the “BannerListener” object

[block:code]
{
  "codes": [
    {
      "code": "bannerView.setBannerListener(new BannerListener() {\n   @Override\n   public void onError(AppnextError error) {\n       super.onError(error);\n   }\n\n   @Override\n   public void onAdLoaded(String s,AppnextAdCreativeType creativeType) {\n       super.onAdLoaded(s,creativeType);\n   }\n\n   @Override\n   public void adImpression() {\n       super.adImpression();\n   }\n\n   @Override\n   public void onAdClicked() {\n       super.onAdClicked();\n   }\n});\n",
      "language": "java"
    }
  ]
}
[/block]

* `onAdLoaded(String BannerID)` - The banner was successfully loaded, and its ready to be shown to the user (String s = banner ID)
* `AdImpression` - The banner was viewed by the user, and an impression was reported to Appnext
* `onAdClicked` - The banner was clicked by the user
* `onError` -  An error has occurred. The following errors codes are passed in the onError callback.
  (Using the `BannersError` object); `NO_ADS`, `CONNECTION_ERROR`, `TIMEOUT`, `NO_MARKET`, `INTERNAL`

[block:code]
{
  "codes": [
    {
      "code": "@Override\npublic void onError(AppnextError error) {\n   switch (error) {\n       case BannersError.CONNECTION_ERROR:\n           Log.v(\"appnext\", \"client connection error\");\n       case BannersError.NO_MARKET:\n           Log.v(\"appnext\", \"couldn't open a valid market\")\n       case BannersError.TIMEOUT:\n           Log.v(\"appnext\", \"connection time out client\")\n       case BannersError.NO_ADS:\n           Log.v(\"appnext\", \"server - no ads\");\n\t\t\tcase BannersError.INTERNAL_ERROR:\n\t    \t\t Log.v(\"appnext\", \"Internal error\");\n     \tdefault:\n           Log.v(\"appnext\", \"other error\");\n   }\n   super.onError(error);\n}\n",
      "language": "java"
    }
  ]
}
[/block]

\##Step 6: Finalize Appnext
Add the following code when you destroy your Activity

[block:code]
{
  "codes": [
    {
      "code": "@Override\npublic void onDestroy() {\n\tsuper.onDestroy();\n\tbanner.destroy();\n}",
      "language": "java"
    }
  ]
}
[/block]

[block:api-header]
{
  "title": "Example Project"
}
[/block]

You can download an example project (Android Studio) from our Github page [here](https://github.com/appnext/appnext_android_sdk_examples).

[block:api-header]
{
  "title": "App Categories"
}
[/block]

[block:parameters]
{
  "data": {
    "0-0": "1. Action\n2. Adventure\n3. Arcade\n4. Arcade & Action\n5. Board\n6. Books & Reference\n7. Brain & Puzzle\n8. Business\n9. Card\n10. Cards & Casino\n11. Casino\n12. Casual\n13. Comics\n14. Communications\n15. Education\n16. Educational\n17. Entertainment\n18. Family\n19. Finance\n20. Health & Fitness\n21. Libraries & Demo\n22. Lifestyle",
    "0-1": "23. Live Wallpaper\n24. Media & Video\n25. Medical\n26. Music\n27. Music & Audio\n28. News & Magazines\n29. Personalization\t\n30. Photography\n31. Productivity\n32. Puzzle\n33. Racing\n35. Shopping\n36. Simulation\n37. Social\n38. Sports\n39. Sports Games\n40. Strategy\n41. Tools\n42. Travel & Local\n43. Trivia\n44. Weather\n45. Word"
  },
  "cols": 2,
  "rows": 1
}
[/block]

Make sure to encode (%20) categories with more than 1 word, example: **Travel**%20%26%20**Local**

[block:api-header]
{
  "title": "Integration Support"
}
[/block]

Should you have any problems integrating the product, log a ticket with us by emailing <support@appnext.com>