Banner Ads
Banner ads are rectangular ads that occupy a portion of your app's layout. They can refresh automatically and stay on screen while users interact with your app.
Each ad format uses a Zone ID to identify the ad placement. Banners are served in the standard sizes; the size of each zone is configured in the Empower dashboard.
XML Layout Setup
Add a container for the banner in your layout:
<FrameLayout
android:id="@+id/bannerContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Loading a Banner Ad
- Kotlin
- Java
import net.empower.mobile.ads.api.EMAManager
class MainActivity : AppCompatActivity() {
private lateinit var bannerContainer: FrameLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
bannerContainer = findViewById(R.id.bannerContainer)
EMAManager.loadBannerAd(
descriptor = this,
zoneId = "YOUR_BANNER_ZONE_ID",
bannerContainer = bannerContainer
)
}
}
import net.empower.mobile.ads.api.EMAManager;
public class MainActivity extends AppCompatActivity {
private FrameLayout bannerContainer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bannerContainer = findViewById(R.id.bannerContainer);
EMAManager.INSTANCE.loadBannerAd(this, "YOUR_BANNER_ZONE_ID", bannerContainer);
}
}
When the banner lives in a Fragment, pass the Fragment as the descriptor instead of the Activity. The SDK follows the descriptor's lifecycle to manage the banner correctly.
Listening to Banner Status
All ad status listeners are optional. The SDK loads and displays ads automatically, retries failed loads, and refreshes banners on schedule based on per-zone settings from the Empower dashboard. Do not build manual reload loops, for example calling loadBannerAd again when a load fails. Use listeners only if you need to track ad states for analytics, UI updates.
| Status | Description |
|---|---|
READY | Banner is loaded and displayed |
FAILED | Banner failed to load |
WILL_LEAVE | User clicked the ad (leaving app) |
If you need to track banner status for analytics or UI updates:
- Kotlin
- Java
EMAManager.loadBannerAd(
descriptor = this,
zoneId = "YOUR_BANNER_ZONE_ID",
bannerContainer = bannerContainer,
listener = object : AdStatusListener {
override fun bannerStatusChanged(status: AdStatus, zoneId: String, adUnitId: String) {
when (status) {
AdStatus.READY -> { }
AdStatus.FAILED -> { }
AdStatus.WILL_LEAVE -> { }
else -> {}
}
}
}
)
EMAManager.INSTANCE.loadBannerAd(
this,
"YOUR_BANNER_ZONE_ID",
bannerContainer,
new AdStatusListener() {
@Override
public void bannerStatusChanged(AdStatus status, String zoneId, String adUnitId) {
if (status == AdStatus.READY) {
} else if (status == AdStatus.FAILED) {
} else if (status == AdStatus.WILL_LEAVE) {
}
}
}
);
Jetpack Compose
For apps using Jetpack Compose, use the EmpowerBannerAd composable:
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner
import net.empower.mobile.ads.compose.EmpowerBannerAd
@Composable
fun BannerAdSection(modifier: Modifier = Modifier) {
val context = LocalContext.current
val lifecycleOwner = LocalLifecycleOwner.current
EmpowerBannerAd(
modifier = modifier,
zoneId = "YOUR_BANNER_ZONE_ID",
context = context,
lifecycleOwner = lifecycleOwner
)
}
RecyclerView Usage (Pause & Resume)
When a banner row scrolls off screen inside a RecyclerView, pause its refresh timer and resume it when the row comes back. Use your adapter's attach and detach callbacks:
- Kotlin
- Java
override fun onViewAttachedToWindow(holder: RecyclerView.ViewHolder) {
super.onViewAttachedToWindow(holder)
if (holder.itemViewType == AD_VIEW_TYPE) {
EMAManager.resumeBannerZone(activity, "YOUR_BANNER_ZONE_ID")
}
}
override fun onViewDetachedFromWindow(holder: RecyclerView.ViewHolder) {
super.onViewDetachedFromWindow(holder)
if (holder.itemViewType == AD_VIEW_TYPE) {
EMAManager.pauseBannerZone(activity, "YOUR_BANNER_ZONE_ID")
}
}
@Override
public void onViewAttachedToWindow(@NonNull RecyclerView.ViewHolder holder) {
super.onViewAttachedToWindow(holder);
if (holder.getItemViewType() == AD_VIEW_TYPE) {
EMAManager.INSTANCE.resumeBannerZone(activity, "YOUR_BANNER_ZONE_ID");
}
}
@Override
public void onViewDetachedFromWindow(@NonNull RecyclerView.ViewHolder holder) {
super.onViewDetachedFromWindow(holder);
if (holder.getItemViewType() == AD_VIEW_TYPE) {
EMAManager.INSTANCE.pauseBannerZone(activity, "YOUR_BANNER_ZONE_ID");
}
}
The descriptor passed to pauseBannerZone / resumeBannerZone must match the one used in loadBannerAd. Passing an Activity for a banner that was loaded with a Fragment (or vice-versa) is a silent no-op.