Introduction
Ads are an effective and easy way to earn revenue from your apps. AdMob is a smart monetization platform for apps that help you to acquire revenue from ads. This is the perfect solution if you don’t want to make your app “paid” or “freemium” and still gain revenue.
All you need to do is sign up for AdMob, and then use the Google Mobile Ads SDK to place ads in your app with just a few lines of code. You get paid quickly in your local currency (where available), with no wire fees charged by AdMob and minimum of $100 limit for checkout.
In this simple tutorial, I want to show you how to add an AdMob Page Banner and AdMob Interstitial Banner to your android project. Maximize your earnings with the new AdMob improved tools to help app developers build their business. So, let’s get started with a new Android studio project.
Click Tools > Firebase in Android studio window
Add Firebase and AdMob SDK to project
Click Tools > Firebase in Android studio window in the Android studio window and you should see a similar window slide from the left.
Click on "Connect to Firebase" button to add new or existing firebase project to your android studio project. You can create a new Firebase project if you don’t have one. This will take care of all the things you’ll need to do to link an app to a firebase project automatically.
After that, Click Add AdMob to your app. This will automatically add all the dependencies to the Gradle file for the Google AdMob library.
Also make sure you update the ‘com.google.firebase:firebase-ads’ version to the latest one, and sync the Gradle.
Create AdMob Ad units
Now, that we’ve set up our project to use Google AdMob, you can now sign in to the AdMob website and create ad units to display in the app.
After signing in, you can create a new app from the left menu (Apps > Add app)
Fill in the details of your app and after completion, click on “Create an ad unit”
At first, we’re going to create a banner ad, so create one, the ad unit name can be anything but to make sure we can differentiate it later for analytics, name it accordingly, and click done. Also, create an interstitial ad accordingly.
The ad units should be visible under Ad units section.
Add adView to layout
Now, back to Android studio, we create an adView on our layout. Here's a sample layout :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="@string/main_banner1" />
<Button
android:id="@+id/showInterstitialButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Show Interstitial" />
</RelativeLayout>
The button will be used to show the interstitial Ad later, now add the Java code accordingly to load the ad.
public class MainActivity extends AppCompatActivity {
AdView adView;
AdRequest adRequest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adView = findViewById(R.id.adView);
adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
}
After adding this to your code run the app. Make sure you add INTERNET permissions on the manifest
Get Device ID
After running the app, see the
Note: As per AdMob Policies you are not allowed to click on your own live ads. In order to protect your AdMob account from getting suspended, use test ads during development as you might click the ads accidentally.
Add interstitial ads
Now also add the interstitial ad to your code, Remember that interstitial id is used in this case to build the ad, and button click shows the ad.
public class MainActivity extends AppCompatActivity {
AdView adView;
AdRequest adRequest;
InterstitialAd interstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adView = findViewById(R.id.adView);
adRequest = new AdRequest.Builder().addTestDevice("{device-id-here}").build();//new code including test device Id
adView.loadAd(adRequest);
interstitialAd = new InterstitialAd(this);
interstitialAd.setAdUnitId(getString(R.string.main_interstitial));
interstitialAd.loadAd(adRequest);
findViewById(R.id.showInterstitialButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (interstitialAd.isLoaded()) interstitialAd.show();
}
});
}
}
Now, run the app, ads should appear on your app.
If there are no ads displaying, take a look at the
Clicking the button will load a full-screen ad (Interstitial Ad). These ads will increase your revenue, but make sure you don't annoy the users with too many of these ;)
Output
Here's a sample of the how the app will look like on the device:
Integrating AdMob in Android app
Mobile Application admob android app tutorial