AdMob - Open Ads In Android

Kerem Türker
3 min readJan 23, 2023

Introduction

Hello Dear Android Lovers, Welcome to my new AdMob article series. In this article we are going to learn how to implement AdMob Open Ads to our project. Let's do it :)

What is Open Ads ?

This is an Ad type one of AdMob that we can show to users when they start app or come back to our app.

When ad is open, It looks like that below screenshot. App Open Ad will have small branding area, the top of the screen. This way users will know which application they opened.

Why should we use Open Ads ?

In short, of course for earn more revenue :)

We learned some things about Open ads. So let’s start to coding.

Library Setup

First of all We need to add the dependencies for Google Mobile Ads SDK to our module's app-level.

Then, We have to add AdMob app ID to AndroidManifest.xml file. Add <meta-data> tags between <application> tags. We need to put this android:name=”com.google.android.gms.ads.APPLICATION_ID” to in meta-data tags. Then insert your own AdMob app ID to value property.

Finally It looks like that, (I used Google's test app id as an example)

  • Test Application ID is ca-app-pub-3940256099942544~3347511713
  • Test Ad Unit ID is ca-app-pub-3940256099942544/3419835294

Extend the Application class

We create a new class that extends the Application class. We’ll initialize to Google Mobile Ads Sdk in this class.

I named it CoreApplication you can give name whatever you want. It's doesn't matter. Anyways, after call onCreate function we initialize MobileAds in this function.

Next step, We need to add the below code to our the AndroidManifest.xml file. (Inside of the application tag)

We are ready now, Let's coding

Note: Ads should be shown quickly, best way is load it before we need to display it. This is why, We create a new class named AppOpenAdManager within CoreApplication class.

Then, We initialize MobileAds in the onCreate function.

We will declare 2 variables in AppOpenAdManager class. Those are below

  • appOpenAd variable is need for manage to ad process.
  • isLoading variable is need for know if ad is loaded.

AppOpenAdManager

Step 1 create load function in the AppOpenAdManager class.

We should check to isLoadingAd and isAdAvailable if one of both is true we shouldn't continue. Then create an adRequest object inside of the load function.

Step 2 Create show ad function

For check if ad is available, so we create function named showAdIfAvailable. We check in this function that if ad is available. If it not available we must call loadAd function then call return.

Next step, We need the Activity object to can call the show ad function. So we implement ActivityLifecycleCallbacks interface to our CoreApplication class.

Then, we call registerActivityLifecycleCallbacks function in the onCreate function. Also then we have to assign value to variable which one named currentActivity in the onActivityStarted function.

Finally, We need to call the showAdIfAvailable function in our Activity.

And That's it. You can find the example project this link.

Thanks for reading. Happy coding!

You can reach me: Linkedin

--

--