The "Mute This Ad" feature provides users with the ability to close or to stop seeing ads and signal which ads aren't interesting to them. Here's what the default, non-custom version looks like:
With NativeAd
, you have the option to implement your own UI to enable a
user to mute the native ad. Here's how it's done.
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())
Check if custom Mute This Ad is available
Once the native ad is loaded, check the value returned from the
isCustomMuteThisAdEnabled()
method on the NativeAd
object. If it's true, add your custom mute
button / gesture and configure your custom mute interface with the
MuteThisAdReason
s
available from the
getMuteThisAdReasons()
method on the NativeAd
object.
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. }
The implementation of the custom mute interface is entirely up to you. You can place a small close button on the ad, or you can provide some other interface for the user to opt to mute the ad.
Show the Mute This Ad reasons
If custom Mute This Ad is available, there is an array of MuteThisAdReason
objects available in the muteThisAdReasons
property on the NativeAd
.
MuteThisAdReason
has a
getDescription()
method which 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 may also choose not to display these reasons when the user clicks the close button, and report the mute action directly with no reason.
Mute the ad
Muting the ad should involve two actions:
Report the reason for the mute to the native ad using the
muteThisAd
method onNativeAd
.On your UI, mute / 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 the MuteThisAdListener
, which is set
using the setMuteThisAdListener
method on the NativeAd
. The onAdMuted()
method will be called only if
the ad was muted successfully.
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() }