The "Mute This Ad" feature lets users close or stop seeing a particular ad, and signal which ads they aren't interested in. Here's how the feature appears on a typical ad:
With NativeAd
, you can implement a custom UI to mute
native ads. This guide shows you how.
Request custom Mute This Ad
The first step is to enable the custom Mute This Ad feature using
setRequestCustomMuteThisAd()
on the NativeAdOptions.Builder
class when making an ad request.
Java
AdLoader adLoader = new AdLoader.Builder(context, "AD_UNIT_ID") .forNativeAd(new NativeAd.OnNativeAdLoadedListener() { @Override public void onNativeAdLoaded(NativeAd nativeAd) { // Show the ad. } }) .withAdListener(new AdListener() { @Override public void onAdFailedToLoad(LoadAdError adError) { // Handle the failure by logging, altering the UI, and so on. } }) .withNativeAdOptions(new NativeAdOptions.Builder() .setRequestCustomMuteThisAd(true) .build()) .build(); adLoader.loadAd(new AdRequest.Builder().build());
Kotlin
val adLoader = AdLoader.Builder(context, "AD_UNIT_ID") .forNativeAd { ad -> // Show the ad } .withAdListener(object : AdListener() { override fun onAdFailedToLoad(adError: LoadAdError) { // Handle the failure by logging, altering the UI, and so on. } }) .withNativeAdOptions(NativeAdOptions.Builder() .setRequestCustomMuteThisAd(true) .build()) .build() adLoader.loadAd(AdRequest.Builder().build())
Show the Mute This Ad reasons
Once the native ad is loaded, check the value returned from the isCustomMuteThisAdEnabled()
method of the NativeAd
object.
If Mute This Ad is available, you can proceed to implement a custom mute
interface, such as a button or gesture for the user to mute or close the ad.
You also have access to an array of MuteThisAdReason
objects from the getMuteThisAdReasons()
method of the NativeAd
object. MuteThisAdReason
has a getDescription()
method that provides a displayable string.
As a best practice, we recommend displaying these reasons to the user and allowing them to select their reason for muting the ad. When the user clicks on one of the reasons, you should report the ad mute with the selected reason.
You can also choose to not display these reasons when the user clicks the close button, and report the mute action directly with no reason.
Java
// In OnNativeAdLoadedListener @Override public void onNativeAdLoaded(NativeAd nativeAd) { // Show the ad. ... this.nativeAdView.setNativeAd(nativeAd); if (nativeAd.isCustomMuteThisAdEnabled()) { enableCustomMuteWithReasons(nativeAd.getMuteThisAdReasons()); } else { hideCustomMute(); } } ... private void enableCustomMuteWithReasons(List<MuteThisAdReason> reasons) { //TODO: This method should show your custom mute button and provide the list // of reasons to the interface that are to be displayed when the user mutes // the ad. } private void hideCustomMute() { //TODO: Remove / hide the custom mute button from your user interface. }
Kotlin
// When constructing the ad loader. builder.forNativeAd { nativeAd -> // Show the ad. ... this.nativeAdView.nativeAd = nativeAd if (nativeAd.isCustomMuteThisAdEnabled) { enableCustomMuteWithReasons(nativeAd.muteThisAdReasons); } else { hideCustomMute(); } } ... private fun enableCustomMuteWithReasons(reasons: List<MuteThisAdReason>) { //TODO: This method should show your custom mute button and provide the list // of reasons to the interface that are to be displayed when the user mutes // the ad. } private fun hideCustomMute() { //TODO: Remove / hide the custom mute button from your user interface. }
Mute the ad
Muting the ad should involve two actions:
- Report the reason for the mute to the native ad.
- On your UI, mute or hide the ad yourself in your preferred manner.
Java
private void muteAdDialogDidSelectReason(MuteThisAdReason reason) { // Report the mute action and reason to the ad. nativeAd.muteThisAd(reason); muteAd(); } private void muteAd { //TODO: Mute / hide the ad in your preferred manner. }
Kotlin
private fun muteAdDialogDidSelectReason(reason: MuteThisAdReason) { // Report the mute action and reason to the ad. nativeAd.muteThisAd(reason) muteAd() } private fun muteAd() { //TODO: Mute / hide the ad in your preferred manner. }
Receive confirmation of ad mute (optional)
If you want to be notified that reporting the ad mute was successful, you can
implement MuteThisAdListener
. This method is called only after
an ad is successfully muted.
Java
nativeAd.setMuteThisAdListener(new MuteThisAdListener() { @Override public void onAdMuted() { Toast.makeText(getActivity(), "Ad muted", Toast.LENGTH_SHORT).show(); } });
Kotlin
nativeAd.setMuteThisAdListener { Toast.makeText(activity, "Ad muted", Toast.LENGTH_SHORT).show() }