A complete guide to integrating AdMob in your Android App

6 min


0

So you will have developed an android software and now you need to make some cash from your software. You have come to the suitable place. In this text I’ll train you ways to combine AdMob in your software. By the tip of this text you’ll understand how to combine three various kinds of advertisements in your software, Banners advertisements, Interstitial advertisements and Rewarded Video advertisements. We will combine AdMob utilizing Firebase.

Setting Up AdMob

Make positive you will have Firebase arrange in your software. Here are the steps to add Firebase in your venture.

Next you want to join an AdMob account. If you don’t have an AdSense account, you have to to create it earlier than creating an AdMob account. are all of the attainable circumstances to create AdSense and AdMob account.

Remember to signup for AdMob with the identical account that you’ll be utilizing with Firebase.

Once you will have created an account, login into AdMob and open your dashboard. Now go to Apps > Add App > Fill in the data required by AdMob > Click on Add.

Once you achieved establishing Firebase and AdMob and creating your software, you have to to hyperlink your software in AdMob to Firebase.

Select the newly added app, go to App Settings and there beneath App Info you will notice an choice to hyperlink software with Firebase.

Click on ‘LINK TO FIREBASE’, a pop up will seem asking you the bundle title of your software, enter the right bundle title of you software. If you face any drawback linking software to Firebase, is the official assist web page from Google.

In you software’s construct.gradle add the Mobile Ads SDK dependency:

compile 'com.google.firebase:firebase-ads:12.0.0'

You want to initialise the Mobile Ads SDK, do it in one in all your actions when your software begins. This must be achieved earlier than exhibiting any sort of advert.

public class ForemostActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        tremendous.onCreate(savedInstanceState);
        setContentView(R.structure.activity_main);
        // Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713
        MobileAds.initialize(this, "YOUR_ADMOB_APP_ID");
    }
}

Right now for testing functions you need to use the pattern AdMob app ID supplied by Google:

ca-app-pub-3940256099942544~3347511713

So the initialisation code might be:

MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");

Now that we’re achieved with the arrange lets create some advertisements!

Creating Banner Ads

To add a Banner Ad to any of your exercise/fragment you should have to use

AdView

 . In your structure file add the next code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:instruments="http://schemas.android.com/tools"
        android:layout_height="match_parent"
        android:layout_width="match_parent">

        <com.google.android.gms.advertisements.AdView
            xmlns:advertisements="http://schemas.android.com/apk/res-auto"
            android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"
            advertisements:adSize="BANNER"
            advertisements:adUnitId="ca-app-pub-3940256099942544/6300978111">
        </com.google.android.gms.advertisements.AdView>

</RelativeLayout>

There are two completely different parameters that it’s best to discover right here,

adUnitId

  and

adSize

 .

adUnitId

 is the distinctive ID that identifies your single advert unit. Here the ID that we now have used is a take a look at ID supplied by Google. Always use take a look at ID when your are growing/testing an app. A actual ID ought to solely be used in manufacturing. In the tip of this tutorial I’ll present you ways to create authentic ID’s for your advert items.

ca-app-pub-3940256099942544/6300978111
adSize

 defines the scale of your banner, there are numerous banner sizes accessible which should take a look at .

You can even add an

AdView

  dynamically from your code.

AdView advertView = new AdView(this);
advertView.setAdDimension(AdDimension.BANNER);
advertView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
viewGroup.addChild(advertView);    //Or another method your like

Loading Ad

After including the AdView, we’ll now load an advert into it.

public class ForemostActivity extends AppCompatActivity {
    non-public AdView mAdView;

    protected void onCreate(Bundle savedInstanceState) {
        tremendous.onCreate(savedInstanceState);
        setContentView(R.structure.activity_main);
        MobileAds.initialize(this,"ca-app-pub-3940256099942544~3347511713");

        mAdView = discoverViewById(R.id.advertView);
        AdRequest adRequest = new AdRequest.Builder().construct();
        mAdView.loadAd(adRequest);
    }
}

Get a reference to the advertView, create a

AdRequest

  object, load the advert by calling

loadAd

 on the

AdView

 reference and passing adRequest into it.

Handling advert occasions

Ad is loaded utilizing web so it is extremely possible that the consumer may not have correct web connection or any kind of error happens. Also you may want to know when the advert is totally loaded. AdMob supplies a set of callbacks for all kind of advertisements.

You can use an

AdListener

 to hear to numerous occasions.

mAdView.setAdListener(new AdListener() {
    @Override
    public void onAdLoaded() {
        // Code to be executed when an advert finishes loading.
    }

    @Override
    public void onAdFailedToLoad(int errorCode) {
        // Code to be executed when an advert request fails.
    }

    @Override
    public void onAdOpened() {
        // Code to be executed when an advert opens an overlay that
        // covers the display screen.
    }

    @Override
    public void onAdLeftApplication() {
        // Code to be executed when the consumer has left the app.
    }

    @Override
    public void onAdClosed() {
        // Code to be executed when when the consumer is about to return
        // to the app after tapping on an advert.
    }
});

Note: Always use take a look at Ad ID if you are testing your software, it’s best to by no means use authentic Ad ID in your software when in testing/growing section. If you achieve this you would possibly get blocked from AdMob.

Once the advert is loaded, you will notice one thing like this:

Creating Interstitial Ads

When creating interstitial advertisements, we don’t want to outline any view in your structure file, these can solely be created programatically.

Make a world variable of

InterstitialAd

 , initialise the the variable in onCreate and set the AdUnitId.

public class ForemostActivity extends Activity {

    non-public InterstitialAd mInterstitialAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        tremendous.onCreate(savedInstanceState);
        setContentView(R.structure.activity_main);

        MobileAds.initialize(this,"ca-app-pub-3940256099942544~3347511713");

        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
    }
}

Google supplies a testing id for interstitial advert, make sure that to use precisely this ID when testing/debugging your software.

ca-app-pub-3940256099942544/1033173712

Now we’d like to load the advert.

mInterstitialAd.loadAd(new AdRequest.Builder().construct());

Lets present the advert. Always examine if the advert is loaded earlier than exhibiting it.

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.present();
        }
    }
});

You can set numerous callbacks to hear to numerous occasions relating to the advert.

mInterstitialAd.setAdListener(new AdListener() {
    @Override
    public void onAdLoaded() {
        // Code to be executed when an advert finishes loading.
    }

    @Override
    public void onAdFailedToLoad(int errorCode) {
        // Code to be executed when an advert request fails.
    }

    @Override
    public void onAdOpened() {
        // Code to be executed when the advert is displayed.
    }

    @Override
    public void onAdLeftApplication() {
        // Code to be executed when the consumer has left the app.
    }

    @Override
    public void onAdClosed() {
        // Code to be executed when when the interstitial advert is closed.
    }
});

Watch this  to get to know one of the best practices when implementing interstitial advertisements. This is how the advert will look as soon as loaded.

Rewarded Video Ads

Now we’ll see how to implement video advertisements which can be utilized to give rewards to the consumer.

Just as in interstitial advertisements, we don’t want to add something to our structure, these advertisements can solely be carried out programatically.

Create a world variable of RewardedVideoAd, initialise the variable in onCreate and set the AdUnitId.

public class ForemostActivity extends AppCompatActivity{
    non-public RewardedVideoAd mRewardedVideoAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        tremendous.onCreate(savedInstanceState);
        setContentView(R.structure.activity_main);

        // Use an exercise context to get the rewarded video occasion.
        mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
    }
}

We create a brand new occasion of RewardedVideoAd in another way in contrast to different advert varieties through the use of 

MobileAds.getRewardedVideoAdInstance(this);

To load RewardedVideoAd you want to name the

loadAd(adUnitId, AdRequest)

 on the advert object.

non-public void loadRewardedVideoAd() {
    mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917",
            new AdRequest.Builder().construct());
}

Google supplies a testing id for video advert, make sure that to use precisely this ID when testing/debugging your software.

ca-app-pub-3940256099942544/5224354917

As with different sorts of advertisements you may hear to numerous advert occasions by setting a

RewardedVideoAdListener

  utilizing

mRewardedVideoAd.setRewardedVideoAdListener(RewardedVideoAdListener);

 .

@Override
public void onRewarded(RewardMerchandise reward) {
    Toast.makeText(this, "onRewarded! currency: " + reward.getType() + "  amount: " +
            reward.getAmount(), Toast.LENGTH_SHORT).present();
    // Reward the consumer.
}

@Override
public void onRewardedVideoAdLeftApplication() {
    Toast.makeText(this, "onRewardedVideoAdLeftApplication",
            Toast.LENGTH_SHORT).present();
}

@Override
public void onRewardedVideoAdClosed() {
    Toast.makeText(this, "onRewardedVideoAdClosed", Toast.LENGTH_SHORT).present();
}

@Override
public void onRewardedVideoAdFailedToLoad(int errorCode) {
    Toast.makeText(this, "onRewardedVideoAdFailedToLoad", Toast.LENGTH_SHORT).present();
}

@Override
public void onRewardedVideoAdLoaded() {
    Toast.makeText(this, "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).present();
}

@Override
public void onRewardedVideoAdOpened() {
    Toast.makeText(this, "onRewardedVideoAdOpened", Toast.LENGTH_SHORT).present();
}

@Override
public void onRewardedVideoStarted() {
    Toast.makeText(this, "onRewardedVideoStarted", Toast.LENGTH_SHORT).present();
}

@Override
public void onRewardedVideoAccomplished() {
    Toast.makeText(this, "onRewardedVideoCompleted", Toast.LENGTH_SHORT).present();
}

Always examine if the advert is loaded earlier than exhibiting the advert.

if (mRewardedVideoAd.isLoaded()) {
    mRewardedVideoAd.present();
}

With

RewardedVideoAd

 ‘s you will have the deal with the lifecycle occasions as proven under.

@Override
public void onResume() {
    mRewardedVideoAd.resume(this);
    tremendous.onResume();
}

@Override
public void onPause() {
    mRewardedVideoAd.pause(this);
    tremendous.onPause();
}

@Override
public void onDestroy() {
    mRewardedVideoAd.destroy(this);
    tremendous.onDestroy();
}

Once loaded the advert will look one thing like this.

Getting actual advert unit ID’s

When your software is in manufacturing, it should use actual advert unit ID’s created in the admob dashboard. Don’t go away take a look at advert unit ID’s in your manufacturing software, else you gained’t earn any cash :P.

Go to AdMob dashboard > Select your software consumer Apps> Click on Ad Units > Click Get Started. Now it ask you what sort of AdUnit would you like to create.

Select the kind of AdUnit you need create. Here I’ve created a banner advert unit.

Once the advert unit is created efficiently it’ll present you the AdUnitId which you’ll use as an alternative of the take a look at ID that you simply used beforehand.

Some useful hyperlinks

The publish A complete guide to integrating AdMob in your Android App appeared first on TheTechnoCafe.


What's Your Reaction?

hate hate
0
hate
confused confused
0
confused
fail fail
0
fail
fun fun
0
fun
geeky geeky
0
geeky
love love
0
love
lol lol
0
lol
omg omg
0
omg
win win
0
win

0 Comments

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

Choose A Format
Trivia quiz
Series of questions with right and wrong answers that intends to check knowledge
Poll
Voting to make decisions or determine opinions
Story
Formatted Text with Embeds and Visuals
List
The Classic Internet Listicles
Meme
Upload your own images to make custom memes
Video
Youtube, Vimeo or Vine Embeds
Audio
Soundcloud or Mixcloud Embeds
Image
Photo or GIF
Gif
GIF format