Search code examples
androidvideoviewgroup

Close reward video ad in Android


I am designing an app that includes a rewarded video before giving the player another life. For user experience and engagement, I want to ensure that this rewarded video is closed after 30 seconds and the user is taken back to the app. We are using a client-side mediation solution for 4 of the largest video networks (Chartboost, Vungle, App Lovin, and Ad Colony).

Many ad networks provide this close functionality, such as MoPub's MoPubView.destroy(). However, not all networks provide a programmatic method to close their ads.

All the ad networks I've come across extend the View class to show their ads. This means that the ad itself must be in the app's View stack.

Is there a way to cycle through Views and close the one that matches an ad? It seems this should be possible, since MoPub's SDK uses the following code:

public void destroy() {
    unregisterScreenStateBroadcastReceiver();
    removeAllViews();
    ....
}

And removeAllViews() is a ViewGroup method composed of:

public void removeAllViews() {
    removeAllViewsInLayout();
    requestLayout();
    invalidate(true);
}

I'm stumped with combining this ViewGroup code with a View iterator like in Enumerate/Iterate all Views in Activity?. Any thoughts?


Solution

  • If the ad view is spawned by your app why don't you keep a reference to the spawned view and close that one after 30 seconds? Or if they supply XML configuration, give the XML view an ID and do a findViewById(...) and close that view.