All CategoriesAndroid App Development

Best Way to Monetize Android Apps using AdMob

What is AdMob?

AdMob by Google is a mobile Advertising platform. It is an easy way to monetize our app by advertisement. We can use it to generate the revenue from our app by publishing it.
We can show various types of ads in our app by using AdMob. Like Banner Ads, Interstitial Ads, Video Ads and Native Ads.
It is very easy to integrate AdMob in our app by creating AdMob account and generating Ad Unit Id. We can create one or more Ad Unit Id, depending upon the type and number of Ads.
So, let’s see how we will integrate AdMob in our Application and what are the steps for it.

How to Create Ad Unit Id’s

Before creating this application, we must generate a unique identifier id for our ad.
Here we will use Banner Ad and Interstitial Ad in our application.
Banner Ad: It takes a portion of our screen. We can place it at the top, bottom or middle anywhere, depending upon the requirement of the application.
Interstitial Ad: It takes the full screen of our device which means it will block the UI of application and comes over it.
So, there are some steps to generate the Ad Unit Id i.e.
⦁ Sign In into your AdMob account.
⦁ After successfully creating your account click on Monetize apps.
⦁ Then add your application manually. Give the application name and platform Android.
⦁ After this, it will generate the App Id.
See the below figure.

Here, we can see very well that it generates the App id. After this, we must select an Ad format which is the type of ad you want to show in your application.
Here we fill all the details ad unit name etc. in Banner Ad and then in Interstitial Ad. Both having different Ad Unit Id’s


Like this, we can see the Ad unit id of Interstitial Ad. After generates, it looks like this.
Now, we have two different Ad id’s one for Banner and another for Interstitial.
Let us see how we use both in this application.

Android Programming

Example with code

⦁ Create an Application AdMobExampleAndroid.
⦁ Add the dependency in build.gradle file.
build.gradle:

dependencies {
compile 'com.google.android.gms:play-services-ads:9.2.1'
}

⦁ Now Add the permissions in AndroidManifest.xml file and other properties like google play services version etc.
AndroidManifest.xml:

<manifest ….
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
…..
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
…....
<!-- Include the AdActivity configChanges and theme. -->
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
</manifest>

Requirements

MainActivity.java, activity_main.xml, InterstitialAdActivity, strings.xml.
So, these are the files where we have to work. Firstly, we will add the Ad Unit id’s in strings.xml file.
strings.xml:
Add the code here for both Banner and Interstitial Ad.

<string name="banner_id">ca-app-pub-2474599530544069/3159981630</string>
<string name="interstitial_id">ca-app-pub-2474599530544069/6113448034 </string>

activity_main.xml:
This is our main layout file. Here we will show the welcome message, a button for launching InterstialAdActivity and AdView to display a Banner.
Add the code here:

<TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="@string/msg_welcome"
     android:textColor="@color/colorAccent"
    android:textSize="20dp"
    android:textStyle="bold"/>
 <Button android:id="@+id/btn_fullscreen_ad"
     android:layout_width="200dp"
     android:layout_height="wrap_content"
     android:layout_centerInParent="true"
     android:text="Show FullScreen Ad"
     android:background="@color/colorPrimary"
     android:textColor="#ffffff"
     />
 <com.google.android.gms.ads.AdView
     android:id="@+id/adView"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_centerHorizontal="true"
     android:layout_alignParentBottom="true"
     ads:adSize="BANNER"
     ads:adUnitId="@string/banner_id">
 </com.google.android.gms.ads.AdView>

Here, we can see that we added the banner at the bottom side of the screen.
MainActivity.java:
This is our Main Activity here we will take the reference of AdView and by creating instance of AdRequest load the ad into AdView also we set adListener on them to let the user know about the events.
Initialize:

private AdView mAdView;
private Button btnFullscreenAd;

Add the code in onCreate(){..}:

mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.build();
mAdView.loadAd(adRequest);
mAdView.setAdListener(new AdListener() {
    @Override
    public void onAdLoaded() {
        Toast.makeText(getApplicationContext(), "Ad is loaded!", Toast.LENGTH_SHORT).show();
    }
    @Override
    public void onAdClosed() {
        Toast.makeText(getApplicationContext(), "Ad is closed!", Toast.LENGTH_SHORT).show();
    }
    @Override
    public void onAdFailedToLoad(int errorCode) {
        Toast.makeText(getApplicationContext(), "Ad failed to load! error code: " + errorCode, Toast.LENGTH_SHORT).show();
    }
    @Override
    public void onAdLeftApplication() {
        Toast.makeText(getApplicationContext(), "Ad left application!", Toast.LENGTH_SHORT).show();
    }
    @Override
    public void onAdOpened() {
        Toast.makeText(getApplicationContext(), "Ad is opened!", Toast.LENGTH_SHORT).show();
    }
});
btnFullscreenAd = (Button) findViewById(R.id.btn_fullscreen_ad);
btnFullscreenAd.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        startActivity(new Intent(MainActivity.this, InterstitialAdActivity.class));
    }
});

Here it shows the live ads, but as a testing purpose during development, we can test it by using device id. We can get this with the help of logCat just take your device id and add it. Like this

AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("50DD11D9DB84856E155BB685C74A3D2B")
.build();

InterstitialAdActivity:
Here we will display the full-screen ad when the user comes in this activity. It will directly show the ad.
Initialize:

InterstitialAd mInterstitialAd;

Add the code here:

mInterstitialAd = new InterstitialAd(this);
 // set the ad unit ID
 mInterstitialAd.setAdUnitId(getString(R.string.interstitial_id));
 AdRequest adRequest = new AdRequest.Builder()
 .build();
 // Load ads into Interstitial Ads
 mInterstitialAd.loadAd(adRequest);
 mInterstitialAd.setAdListener(new AdListener() {
 public void onAdLoaded() {
 showInterstitial();
 }
 });
}
private void showInterstitial() {
 if (mInterstitialAd.isLoaded()) {
 mInterstitialAd.show();
 }
}

Same we can do for InterstialAd also. By adding device id instead of live ads.

Output

                                                                                        Banner Ad (Live ad)

                                                                                     Banner Ad (Test ad)


Interstitial Ad (Live ad)

  Interstitial Ad (Test ad)

Download Code: GitHub

Conclusion

This is a simple example of AdMob on how to implement it in our application. As we discussed we can show the different type of ads in our app and place it accordingly. So by using this we can also earn money by publishing the apps. It is new for a mobile advertising platform. There are some simple steps to generate the Ad Unit id for an application. we can monetize as many apps as we discussed and generate one or more Ad unit id’s for the same application.

Keep visiting www.acadgild.com for more updates on the courses.

Android Programming

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Articles

Close