How to Monitize your Android App with Admob (Simple Way)

In this tutorial, you'll learn about how to integrate AdMob so you can make money from that awesome Android app you wrote—come on, who doesn't want that? AdMob is among the biggest mobile advertising platforms in the market, and it is owned by Google.
There are a few different ways to monetize your apps in Android: paid downloads, paid subscriptions, in-app purchases, and by displaying ads. You can combine them, but is recommended you pick a single model. In this tutorial, you'll learn about how to monetize your app by displaying ads. 
The kinds of ads you'll create in this tutorial are banner, interstitial, and native express ads. I'll explain each of them and show you how to implement them in your application. But before that, let's first of all see how to integrate the Mobile Ads SDK and initialize it. 
In Android Studio, create a new project named MainActivity.

To begin integration of AdMob with your app, you'll need to first include the Mobile Ads SDK to your app module build.gradle file:
1
compile 'com.google.android.gms:play-services-ads:11.0.2'
If you are going to integrate Firebase into your app, then you should use the SDK which is part of Firebase:
1
compile 'com.google.firebase:firebase-ads:11.0.2'
Check out the some of our Firebase tutorials here on Envato Tuts+ if you need help getting started with Firebase:

You need to initialize the Mobile Ads SDK before you can load ads on your Android app, so do this as early as possible. We create a class which extends the Application class, and then we initialize the MobileAds SDK in the onCreate() method of that class, because this method is called only once when the app is launched. 
01
02
03
04
05
06
07
08
09
10
11
import com.google.android.gms.ads.MobileAds;
import android.app.Application;
public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        MobileAds.initialize(this, "ca-app-pub-3940256099942544/6300978111");
    }
}
The second argument supplied to the static method initialize() of the MobileAds class should be your AdMob application ID you got when you signed up for AdMob. In this case, we are using the public application ID provided by Google for demo purposes. 
We need to add the application class we created to the application tag name attribute in our AndroidManifest.xml file. 
1
2
3
<application
    android:name=".App">
</application>
While in this file, also make sure to include the INTERNET permission so that Google mobile ads can run. 
1
2
3
<!-- Include required permissions for Google Mobile Ads to run. -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
In the code snippet below, we added the AdActivity to our AndroidManifest.xml file within the application tag. 
1
2
3
<activity android:name="com.google.android.gms.ads.AdActivity"
          android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
          android:theme="@android:style/Theme.Translucent" />
This activity is provided by the SDK. It is useful in banner ads to fire up the ad to be viewed when the user clicks on the ad, while for an interstitial ad, it is used for displaying the ad when the user clicks on it.
Banner ads cover a part of the currently visible screen. In other words, any content in your app and the ad are displayed together on the screen. This improves the user experience because your users can keep on using your app while the ad is showing, unlike an interstitial ad (just hang on, we'll get to that shortly). Note that a banner ad can be text or an image. 
Let's look at how to implement a banner ad. 
AdView is a custom ViewGroup that will contain the banner ad, so we need to edit our activity_banner_ad.xml layout file to include this view. 
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:ads="http://schemas.android.com/apk/res-auto"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context="com.chikeandroid.tutsplus_admob.MainActivity">
    <TextView android:text="Hello World"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" />
    <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="ca-app-pub-3940256099942544/6300978111">
    </com.google.android.gms.ads.AdView>
</RelativeLayout>
We are defining the AdView size by using the attribute ads:adSize and setting it to BANNER. Other alternatives available are LARGE_BANNERFULL_BANNERSMART_BANNER, etc. 
The ads:adUnitId AdView attribute is set to a sample ad unit provided by Google. You'll have to update this with an ad unit associated with your account if you want to actually make money from your ads! 
The ad unit ID identifies an ad placement, and you can find it in the AdMob admin console. This ID will tell AdMob the kind of ad to be displayed on your app and also the display format (image, text, or video).
For us to finally show the ad, we need to make a request and then show it in the AdView we created above in the BannerAdActivity class. 
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class BannerAdActivity extends AppCompatActivity {
    private AdView mAdView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .build();
        mAdView.loadAd(adRequest);
    }
}
We made an ad request by creating an instance of AdRequest using the builder. Then we used the method addTestDevice(), passing in a device id as an argument to receive test ads to the device, which in our case is the emulator. We then finally called the AdView method loadAd() that takes in this AdRequest instance and then loads the ad on a background thread (so as not to block the UI/main thread).

Let's now explore the events or callbacks we can observe in an ad. These are the events available: 
  • onAdLoaded(): this method is fired when the ad is retrieved. 
  • onAdOpened(): this method is invoked when the ad is opened. 
  • onAdClosed(): this method is fired when the ad is closed.
  • onAdLeftApplication(): this method is invoked when the user has left the application.
  • onAdFailedToLoad(int errorCode): this is fired when a request for the ad fails. The code can be one of ERROR_CODE_NETWORK_ERRORERROR_CODE_INVALID_REQUESTERROR_CODE_NO_FILL, or ERROR_CODE_INTERNAL_ERROR.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// ...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // ...
        mAdView.setAdListener(new AdListener() {
         
                    @Override
                    public void onAdLoaded() {
                        super.onAdLoaded();
                        Toast.makeText(MainActivity.this, "onAdLoaded()", Toast.LENGTH_SHORT).show();
                    }
         
                    @Override
                    public void onAdOpened() {
                        super.onAdOpened();
                        Toast.makeText(MainActivity.this, "onAdOpened()", Toast.LENGTH_SHORT).show();
                    }
         
                    @Override
                    public void onAdClosed() {
                        super.onAdClosed();
                        Toast.makeText(MainActivity.this, "onAdClosed()", Toast.LENGTH_SHORT).show();
                    }
         
                    @Override
                    public void onAdFailedToLoad(int i) {
                        super.onAdFailedToLoad(i);
                        Toast.makeText(MainActivity.this, "onAdFailedToLoad()", Toast.LENGTH_SHORT).show();
                    }
         
                    @Override
                    public void onAdLeftApplication() {
                        super.onAdLeftApplication();
                        Toast.makeText(MainActivity.this, "onAdLeftApplication()", Toast.LENGTH_SHORT).show();
                    }
            });
    }
 
    @Override
    public void onPause() {
        // This method should be called in the parent Activity's onPause() method.
        if (mAdView != null) {
            mAdView.pause();
        }
        super.onPause();
    }
     
    @Override
    public void onResume() {
        super.onResume();
        // This method should be called in the parent Activity's onResume() method.
        if (mAdView != null) {
            mAdView.resume();
        }
    }
     
    @Override
    public void onDestroy() {
        // This method should be called in the parent Activity's onDestroy() method.
        if (mAdView != null) {
            mAdView.destroy();
        }
        super.onDestroy();
    }
}
After adding the listener, run the project again and interact with the ad. Observe the events being invoked by watching the toasts we created. 
We have seen how easy it is to display a banner ad. Now let's look at how to create interstitial ads. 
Interstitial ads are ads that cover the whole screen of your application, giving no room for other views of your app to show (as we'll see shortly). Since this takes over the entire screen and also takes some time to load if the network is slow, you need to be careful not to irritate your users. So ideally these interstitial ads should be shown during natural breaks in your app, e.g. between levels in a game, and not when users are in the middle of some other task.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
 
public class InterstitialAdActivity extends AppCompatActivity {
    private InterstitialAd mInterstitialAd;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        loadInterstitialAd();
    }
 
    private void loadInterstitialAd() {
        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
        mInterstitialAd.setAdListener(new AdListener() {
 
            @Override
            public void onAdLoaded() {
                super.onAdLoaded();
                Toast.makeText(MainActivity.this, "onAdLoaded()", Toast.LENGTH_SHORT).show();
                if(mInterstitialAd.isLoaded()) {
                    mInterstitialAd.show();
                }
            }
 
            @Override
            public void onAdFailedToLoad(int i) {
                super.onAdFailedToLoad(i);
                Toast.makeText(MainActivity.this, "onAdFailedToLoad()", Toast.LENGTH_SHORT).show();
            }
        });
 
        AdRequest adRequest = new AdRequest.Builder().build();
        mInterstitialAd.loadAd(adRequest);
    }
}
In the code above, we declared and initialized an instance of the class InterstitialAd in the InterstitialAdActivity class. We set the add unit id by passing the Google-provided one as the only argument into the method setAdUnitId()
Just like what we did for the banner ad, we want to listen for events on the ad, so we set a listener to fire the overloaded methods onAdLoaded() and onAdFailedToLoad(int i). We make an ad request by creating an instance of the AdRequest class using its builder and then call the method loadAd(), passing this request as an argument to the method. We use the method isLoaded() to determine when the ad has been loaded and then call the method show() to finally display it. 
You can also add an AdListener just like we did for the banner ad.

Now that we have learned about the different types of ads, you can go ahead and integrate them into your application. To begin showing real ads and making money, you'll need an AdMob account—with real ad unit ids that are linked to real ads from advertisers. Just visit the AdMob website to sign up!

In this tutorial, you learned about AdMob and how to integrate different AdMob ad formats such as banner, interstitial, and native express ads on Android.
My AdMob Screenshot:


For  More  Blogs subscribe my website and Troll Videos Click here 

No comments:

Post a Comment