Để thảo luận và đưa ra ý kiến phản hồi về các sản phẩm của chúng tôi, hãy tham gia kênh Discord chính thức của Ad Manager trong máy chủ Cộng đồng quảng cáo và đo lường của Google.
Nếu bạn vẫn cần triển khai IMA SDK trong ứng dụng của mình, hãy xem hướng dẫn Bắt đầu của chúng tôi.
Thêm quảng cáo đi kèm vào ứng dụng
Tạo một ViewGroup để hiển thị thành phần đi kèm
Trước khi yêu cầu một thành phần đi kèm, bạn cần tạo một không gian cho thành phần đó trong bố cục của mình. Trong tệp XML bố cục, hãy thêm một phần tử ViewGroup; ví dụ này sử dụng LinearLayout. Trong bước sau, bạn sẽ truyền một tham chiếu đến phần tử này vào AdDisplayContainer.
Nếu bạn đang tích hợp vào ứng dụng BasicExample, hãy thêm phần này vào activity_my.xml bên dưới videoPlayerContainer.
Bước tiếp theo là tạo một đối tượng CompanionAdSlot, sau đó đối tượng này sẽ được thêm vào một ArrayList<CompanionAdSlot>.
AdDisplayContainer lấy danh sách các vị trí quảng cáo đồng hành để bạn có thể hiển thị nhiều quảng cáo đồng hành cùng một lúc. Bạn sẽ cần tạo một thực thể của ImaSdkFactory để tạo CompanionAdSlot.
Tạo một vùng quảng cáo đồng hành cho mỗi kích thước quảng cáo đồng hành mà bạn dự định hiển thị trong ứng dụng của mình.
IMA SDK sẽ điền vào vùng quảng cáo đồng hành mọi quảng cáo đồng hành trong phản hồi VAST có kích thước phù hợp với chiều cao và chiều rộng của khung hiển thị. IMA SDK cũng hỗ trợ việc sử dụng quảng cáo đồng hành có kích thước linh hoạt.
Sau khi bạn tạo companionAdSlots, bạn cần thêm chúng vào AdsLoader. Các ví dụ sau đây cho thấy cách thực hiện việc này tuỳ thuộc vào việc bạn đang sử dụng IMA Android BasicExample (sử dụng tiện ích Exoplayer-IMA) hay các cách triển khai IMA khác không sử dụng tiện ích này.
BasicExample
adsLoader = new ImaAdsLoader.Builder(this).setCompanionAdSlots(companionAdSlots).build();
Chỉ vậy thôi! Giờ đây, ứng dụng của bạn đang hiển thị quảng cáo đi kèm.
Hiển thị quảng cáo đi kèm linh hoạt
IMA hiện hỗ trợ quảng cáo đồng hành linh hoạt. Các quảng cáo đi kèm này có thể thay đổi kích thước để phù hợp với kích thước của vùng quảng cáo. Các mẫu này sẽ lấp đầy 100% chiều rộng của khung hiển thị gốc, sau đó thay đổi kích thước chiều cao để phù hợp với nội dung của quảng cáo đi kèm. Bạn có thể đặt các kích thước này bằng cách sử dụng kích thước quảng cáo đồng hành Fluid trong Ad Manager. Hãy xem hình ảnh sau đây để biết vị trí đặt giá trị này.
Cập nhật ứng dụng Android để có các thiết bị đồng hành mượt mà
Tôi đã làm theo hướng dẫn nhưng không thấy quảng cáo đồng hành. Tôi nên làm gì?
Trước tiên, hãy kiểm tra để đảm bảo rằng thẻ của bạn thực sự đang trả về quảng cáo đi kèm. Để thực hiện việc này, hãy mở thẻ trong trình duyệt web và tìm thẻ CompanionAds. Nếu bạn thấy thông báo đó, hãy kiểm tra để đảm bảo kích thước của quảng cáo đi kèm được trả về có cùng kích thước với các phương diện mà bạn đang truyền vào đối tượng CompanionAdSlot.
Vị trí quảng cáo đi kèm của tôi sẽ trông như thế nào khi làm theo hướng dẫn này?
Hình ảnh bên dưới được tạo từ
BasicExample
và có video nội dung đang phát ở trên cùng với quảng cáo đi kèm ở bên dưới.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-09-06 UTC."],[[["\u003cp\u003eThis guide helps publishers integrate companion ads into their Android applications using the IMA SDK.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers need an Android app with the IMA SDK and an ad tag configured to return companion ads to begin.\u003c/p\u003e\n"],["\u003cp\u003eThe process involves creating a ViewGroup in the layout, a CompanionAdSlot object, and adding it to the AdsLoader.\u003c/p\u003e\n"],["\u003cp\u003eThe IMA SDK supports fluid companion ads that resize to fit the ad slot using \u003ccode\u003eCompanionAdSlot.FLUID_SIZE\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eTroubleshooting tips are provided for common issues, such as not seeing companion ads after implementation.\u003c/p\u003e\n"]]],[],null,["Select platform: [HTML5](/interactive-media-ads/docs/sdks/html5/client-side/companions \"View this page for the HTML5 platform docs.\") [Android](/interactive-media-ads/docs/sdks/android/client-side/companions \"View this page for the Android platform docs.\") [iOS](/interactive-media-ads/docs/sdks/ios/client-side/companions \"View this page for the iOS platform docs.\")\n\nThis guide is intended for publishers interested in adding companion ads to\ntheir Android IMA implementation.\n\nPrerequisites\n\n- Android application with the IMA SDK integrated. See the [BasicExample](https://github.com/googleads/googleads-ima-android/blob/main/basicexample/) if you don't already have an app with the SDK integrated.\n- An ad tag configured to return a companion ad.\n - If you need a sample, check out our [FAQ](/interactive-media-ads/faq#4).\n\nHelpful primers\n\nIf you still need to implement the IMA SDK in your app, check out our [Get\nStarted guide](/interactive-media-ads/docs/sdks/android/client-side/quickstart).\n\nAdd companion ads to your app\n\nCreate a ViewGroup to display your companion\n\nBefore requesting a companion, you need to create a space for it in your\nlayout. In your layout XML, add a `ViewGroup` element; this\nexample uses a `LinearLayout`. In a later step you'll pass a\nreference to this element to your `AdDisplayContainer`.\n\nIf you're\nintegrating into the BasicExample app, add this to [`activity_my.xml`](https://github.com/googleads/googleads-ima-android/blob/main/basicexample/app/src/main/res/layout/activity_my.xml) below the `videoPlayerContainer`.\n\nactivity_my.xml \n\n```scdoc\n\u003cLinearLayout\n android:id=\"@+id/companionAdSlot\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"250dp\"\n android:layout_gravity=\"center_horizontal\"\n android:gravity=\"center\"\n android:orientation=\"vertical\"\n android:textAlignment=\"center\" /\u003e\n```\n\nCreate a CompanionAdSlot\n\nThe next step is to build a `CompanionAdSlot` object, which is\nthen added to an `ArrayList\u003cCompanionAdSlot\u003e`.\n`AdDisplayContainer` takes a list of companion ad slots so you can\ndisplay multiple companion ads at once. You will need to create an instance of\n`ImaSdkFactory` to create the `CompanionAdSlot`. \n\n```text\n ImaSdkFactory sdkFactory = ImaSdkFactory.getInstance();\n ViewGroup companionViewGroup = (ViewGroup) findViewById(R.id.companionAdSlot);\n\n CompanionAdSlot companionAdSlot = sdkFactory.createCompanionAdSlot();\n companionAdSlot.setContainer(companionViewGroup);\n companionAdSlot.setSize(300, 250);\n ArrayList\u003cCompanionAdSlot\u003e companionAdSlots = new ArrayList\u003cCompanionAdSlot\u003e();\n companionAdSlots.add(companionAdSlot);\n```\n\nCreate a companion ad slot for each size of companion you intend to show in your app.\nThe IMA SDK populates the companion ad slot with any companions from the VAST response that\nhave dimensions matching the view's height and width. The IMA SDK also supports using\n[fluid sized companions](/interactive-media-ads/docs/sdks/android/client-side/companions#display-fluid-companion-ads).\nOnce you have created the `companionAdSlots` they needed to be added to the\n`AdsLoader`. The following examples show how to do this depending on whether you\nare using the\n[IMA Android BasicExample](https://github.com/googleads/googleads-ima-android/blob/main/basicexample/) which uses the\n[Exoplayer-IMA extension](//github.com/google/ExoPlayer/tree/release-v2/extensions/ima),\nor other IMA implementations that do not use the extension. \n\nBasicExample \n\n```text\nadsLoader = new ImaAdsLoader.Builder(this).setCompanionAdSlots(companionAdSlots).build();\n```\n\nOther implementations \n\n```text\nadsLoader.getAdDisplayContainer().setCompanionSlots(companionAdSlots);\n```\n\nThat's all there is to it! Your application is now displaying companion\nads.\n\nDisplay fluid companion ads\n\nIMA now supports fluid companion ads. These companions ads can resize to match the size of the ad\nslot. They fill 100% of the width of parent view, then resize their height to fit the companion's\ncontent. They're set by using the `Fluid` companion size in Ad Manager. See the\nfollowing image for where to set this value.\n\nUpdate Android apps for fluid companions\n\nYou can declare a fluid companion slot by updating the\n[`CompanionAdSlot.setSize()`](/interactive-media-ads/docs/sdks/android/client-side/api/reference/com/google/ads/interactivemedia/v3/api/CompanionAdSlot#public-abstract-void-setsize-int-width,-int-height)\nmethod to take [`CompanionAdSlot.FLUID_SIZE`](/interactive-media-ads/docs/sdks/android/client-side/api/reference/com/google/ads/interactivemedia/v3/api/CompanionAdSlot#public-static-final-int-fluid_size)\nas both parameters. \n\n```scdoc\n ImaSdkFactory sdkFactory = ImaSdkFactory.getInstance();\n ViewGroup companionViewGroup = (ViewGroup) findViewById(R.id.companionAdSlot);\n\n CompanionAdSlot companionAdSlot = sdkFactory.createCompanionAdSlot();\n companionAdSlot.setContainer(companionViewGroup);\n companionAdSlot.setSize(CompanionAdSlot.FLUID_SIZE, CompanionAdSlot.FLUID_SIZE);\n ArrayList\u003cCompanionAdSlot\u003e companionAdSlots = new ArrayList\u003cCompanionAdSlot\u003e();\n companionAdSlots.add(companionAdSlot);\n```\n\nFAQ\n\nI followed the guide, but I'm not seeing companion ads. What should I do?\n: First, check to make sure your tag really is returning companions. To do\n this, open the tag in a web browser and look for a CompanionAds tag. If you see\n that, check to make sure the size of the companion being returned is the same\n size as the dimensions you're passing into the `CompanionAdSlot`\n object.\n\nWhat will my companion ad slot look like when following this guide?\n:\n The image below was created from the [BasicExample](https://github.com/googleads/googleads-ima-android/blob/main/basicexample/) and has the content video playing above with the companion ad below.\n\n \u003cbr /\u003e"]]