A complete guide to integrating AdMob in your Android App

So you’ll have developed an android software program and now you want to make some money from your software program. You have come to the acceptable place. In this textual content I’ll practice you methods to mix AdMob in your software program. By the tip of this textual content you may perceive how to mix three varied sorts of ads in your software program, Banners ads, Interstitial ads and Rewarded Video ads. We will mix AdMob using Firebase.

Setting Up AdMob

Make optimistic you’ll have Firebase organize in your software program. Here are the steps to add Firebase in your enterprise.

Next you need to be a part of an AdMob account. If you don’t have an AdSense account, you’ve gotten to to create it sooner than creating an AdMob account. are the entire attainable circumstances to create AdSense and AdMob account.

Remember to signup for AdMob with the equivalent account that you’re going to be using with Firebase.

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

Once you achieved establishing Firebase and AdMob and creating your software program, you’ve gotten to to hyperlink your software program in AdMob to Firebase.

Select the newly added app, go to App Settings and there beneath App Info you’ll discover an alternative to hyperlink software program with Firebase.

Click on ‘LINK TO FIREBASE’, a pop up will appear asking you the bundle title of your software program, enter the appropriate bundle title of you software program. If you face any downside linking software program to Firebase, is the official help internet web page from Google.

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

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

You need to initialise the Mobile Ads SDK, do it in one in all your actions when your software program begins. This have to be achieved sooner than exhibiting any form of advert.

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

Right now for testing capabilities you want to use the sample AdMob app ID provided by Google:

ca-app-pub-3940256099942544~3347511713

So the initialisation code may be:

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

Now that we’re achieved with the organize lets create some ads!

Creating Banner Ads

To add a Banner Ad to any of your train/fragment it’s best to have to use

AdView

 . In your construction file add the following code:

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

        <com.google.android.gms.ads.AdView
            xmlns:ads="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"
            ads:adSize="BANNER"
            ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
        </com.google.android.gms.ads.AdView>

</RelativeLayout>

There are two utterly totally different parameters that it is best to uncover proper 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 check out ID provided by Google. Always use check out ID when your are rising/testing an app. A precise ID ought to solely be used in manufacturing. In the tip of this tutorial I’ll current you methods to create genuine ID’s for your advert objects.

ca-app-pub-3940256099942544/6300978111
adSize

 defines the size of your banner, there are quite a few banner sizes accessible which ought to check out .

You may 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 one other technique your like

Loading Ad

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

public class ForemostActivity extends AppCompatActivity {
    personal AdView mAdView;

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

        mAdView = uncoverViewById(R.id.advertView);
        AdRequest adRequest = new AdRequest.Builder().assemble();
        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 events

Ad is loaded using internet so this can be very potential that the patron might not have appropriate internet connection or any type of error occurs. Also it’s your decision to know when the advert is completely loaded. AdMob provides a set of callbacks for all type of ads.

You can use an

AdListener

 to hear to quite a few events.

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 show display screen.
    }

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

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

Note: Always use check out Ad ID in case you are testing your software program, it is best to not at all use genuine Ad ID in your software program when in testing/rising part. If you obtain this you may get blocked from AdMob.

Once the advert is loaded, you’ll discover one factor like this:

Creating Interstitial Ads

When creating interstitial ads, we don’t need to define any view in your construction 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 {

    personal InterstitialAd mInterstitialAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        great.onCreate(savedInstanceState);
        setContentView(R.construction.activity_main);

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

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

Google provides a testing id for interstitial advert, make it possible for to use exactly this ID when testing/debugging your software program.

ca-app-pub-3940256099942544/1033173712

Now we might like to load the advert.

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

Lets current the advert. Always look at if the advert is loaded sooner than exhibiting it.

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

You can set quite a few callbacks to hear to quite a few events 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 patron 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 the most effective practices when implementing interstitial ads. This is how the advert will look as quickly as loaded.

Rewarded Video Ads

Now we’ll see how to implement video ads which might be utilized to give rewards to the patron.

Just as in interstitial ads, we don’t need to add one thing to our construction, these ads 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{
    personal RewardedVideoAd mRewardedVideoAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        great.onCreate(savedInstanceState);
        setContentView(R.construction.activity_main);

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

We create a model new event of RewardedVideoAd in one other manner in distinction to totally different advert varieties via the usage of 

MobileAds.getRewardedVideoAdInstance(this);

To load RewardedVideoAd you need to identify the

loadAd(adUnitId, AdRequest)

 on the advert object.

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

Google provides a testing id for video advert, make it possible for to use exactly this ID when testing/debugging your software program.

ca-app-pub-3940256099942544/5224354917

As with different types of ads it’s possible you’ll hear to quite a few advert events by setting a

RewardedVideoAdListener

  using

mRewardedVideoAd.setRewardedVideoAdListener(RewardedVideoAdListener);

 .

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

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

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

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

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

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

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

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

Always look at if the advert is loaded sooner than exhibiting the advert.

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

With

RewardedVideoAd

 ‘s you’ll have the cope with the lifecycle events as confirmed below.

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

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

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

Once loaded the advert will look one factor like this.

Getting precise advert unit ID’s

When your software program is in manufacturing, it ought to use precise advert unit ID’s created in the admob dashboard. Don’t go away check out advert unit ID’s in your manufacturing software program, else you gained’t earn any money :P.

Go to AdMob dashboard > Select your software program client Apps> Click on Ad Units > Click Get Started. Now it ask you what kind of AdUnit would you want to create.

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

Once the advert unit is created effectively it’s going to current you the AdUnitId which you may use as an alternative of the check out ID that you just used beforehand.

Some helpful hyperlinks

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

https://www.adsensemarket.com/

#1 Platform Where You Get Tips & Tricks About Google AdSense, AdSense Alternatives & Google Admob.

Leave a Reply

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