Menu

Adding Google Admob Ads to Android App

0 Comments

Main Objective of this blog post is to explain how to integrate Admob Ads in Android Application using  Android Studio.Integrating the Google Mobile Ads SDK into an app is the first step toward displaying ads and earning revenue.It has a very flexible ad control and allows you to choose between a variety of types and sizes of the apps which can help to increase your earning through app advertising.

Select an ad format

1.Banner Ads

Banner ads are rectangular image or text ads that occupy a spot within an app’s layout. They stay on screen while users are interacting with the app, and can refresh automatically after a certain period of time. The former type of banner advertisement is called a leaderboard, while the latter is called a skyscraper. Banner ads are image-based rather than text-based and are a popular form of website advertising. The purpose of banner advertising is to promote a brand and/or to get visitors from the host website to go to the advertiser’s website.

 

2.Interstitial Ads

Interstitials are full-screen ads that cover the interface of an app until closed by the user.Interstitials Ads are bigger full-screen ads. They block the screen for some time until the user presses a “x” button to close the Ad. By blocking the complete screen interstitial Ads compel the user to interact with the ad, giving high user engagement and conversion rates. A prestitial ad (splash ad, welcome ad) displays before the homepage and an interstitial ad displays between two content pages. Both types work the same way.

 

 

3.Native Ads

Native is a component-based ad format that gives publishers the freedom to customize how ad assets like headlines and calls to action are presented in their apps. By choosing fonts, colors, and other details for themselves, publishers are able to create natural, unobtrusive ad presentations that can add to a rich user experience.

AdMob offers two different ways to implement native ads: Native Ads Express and Native Ads Advanced.

 

4.Rewarded Video

 

Rewarded video ads are full screen video ads that users have the option of watching in full in exchange for in-app rewards.

 

Create your Ad Units

  1. Go to Google Admob Page and Sign-Up if you don’t have an existing account. Just Sign-In in case you already have a Google Admob Account.
  2.  Click on Apps tab and select or create new app.
  3. If you’ve already published your app to the play store, you can search app or developer from the search for your app tab.

 

 

4. If you want to add your app manually (not published yet) click on add your app manually tab and give your app name and choose    the platform.

 

 

 

5.Once the ad unit is created, you can notice the Ad unit ID on the dashboard. An example of ad unit id look like ca-app-pub- 0******7076*******42***2674

 

 

 

 

Creating New Project

1. Create a new project in Android Studio from File ⇒ New Project.

 

2. Open build.gradle and add play services dependency as AdMob requires it.

compile 'com.google.android.gms:play-services-ads:9.0.0'

3. Open AndroidManifest.xml and add the below-mentioned permissions and other properties.

Add INTERNET & ACCESS_NETWORK_STATE permissions.

  <uses-permission android:name="android.permission.INTERNET" />

Add google play services version meta-data.

<activity
    android:name="com.google.android.gms.ads.AdActivity"
    android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
    android:screenOrientation="portrait" />


4.Open the layout file of your main activity (activity_main.xml) and add the AdView widget.
<com.google.android.gms.ads.AdView
    android:id="@+id/mAdView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:adSize="SMART_BANNER"
    app:adUnitId="ADS_UNIT_ID"></com.google.android.gms.ads.AdView>

5.Open MainActivity.java and modify the code as shown.

In onCreate() write below Code for banner Ads .mAdView is the id of layout AdView widget.

mAdView.setAdListener(new AdListener() {
          private void showToast(String message) {
               Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
           }

            @Override
            public void onAdLoaded() {
              showToast("Ad loaded.");
                if (mAdView.getVisibility() == View.GONE) {
                    mAdView.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
             showToast(String.format("Ad failed to load with error code %d.", errorCode));
            }

            @Override
            public void onAdOpened() {
             showToast("Ad opened.");
            }

            @Override
            public void onAdClosed() {
                showToast("Ad closed.");
            }

            @Override
            public void onAdLeftApplication() {
               showToast("Ad left application.");
            }
        });


        AdRequest adRequest = new AdRequest.Builder()
                .build();
        mAdView.loadAd(adRequest);





@Override
protected void onDestroy() {
    super.onDestroy();
    
    mAdView.destroy();
    if (mAdView != null) {

        mAdView = (AdView) findViewById(R.id.adView);
        mAdView.destroy();
    }


}

@Override
protected void onPause() {
    super.onPause();
    System.out.println("pause");
    if (mAdView != null) {
        mAdView.pause();
    }
    super.onPause();
}

@Override
protected void onResume() {
    super.onResume();
    System.out.println("resume");
    if (mAdView != null) {
        mAdView.resume();
    }
}

 

AdListener

public abstract class AdListener extends Object
A listener for receiving notifications during the lifecycle of an ad.

onAdClosed()

Called when the user is about to return to the application after clicking on an ad.

onAdLoaded()

Called when an ad is received.

onAdOpened()

Called when an adopens an overlay that covers the screen.

onAdFailedToLoad(int errorCode)

Called when an ad request failed.

Interstitial Ads

In onCreate() write below Code for Interstitial Ads .mInterstitialAd is the id of layout AdView widget.

mInterstitialAd = new InterstitialAd(this);

// set the ad unit ID
mInterstitialAd.setAdUnitId(getString(R.string.interstitial_home_footer));

AdRequest adRequest = new AdRequest.Builder()
 .build();

// Load ads into Interstitial Ads
mInterstitialAd.loadAd(adRequest);

mInterstitialAd.setAdListener(new AdListener() {
 public void onAdLoaded() {
 showInterstitial();
 }

@Override
 public void onAdClosed() {
 super.onAdClosed();
 
 finish();
 }
});

private void showInterstitial() {
 if (mInterstitialAd.isLoaded()) {
 mInterstitialAd.show();
 }
}

 

Test Ads

If you click on too many ads without being in test mode, you risk your account being flagged for invalid activity.So, use the
Test Ads Unit id provided by google API Developers.

https://developers.google.com/admob/android/test-ads

 

 

Happy Coding!!!!

 

 

Please follow and like us:
0
0
Tags: , , ,

1 thought on “Adding Google Admob Ads to Android App”

  1. admob says:

    Many thanks for every other helpful weblog. The place different could I receive that type of knowledge written in this type of perfect method? For sale goal that I’m right now taking care of, and I’ve also been in the be aware of similarly info.

    0

Leave a Reply

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

www.000webhost.com