Journal for Developers

How to Integrate AdMob in Android App

0 74

So you want to monetize your App by adding AdMob to your Android Application.

Here we are gonna share the steps for integration of AdMob to Android Application.

AdMob can be integrated as a Standalone or with the help of Firebase.

In this journal entry, we will be taking the first point, that is, integrating the AdMob as a Standalone.

Things that will be required to implement the AdMob is:

  • Your Android App
  • AdMob Account
  • App having the permission to access the INTERNET.

Lets get started with Admob account creation, it is pretty straight forward. You have to visit the Google AdMob official page and create your account.

Step #1:

After creating the account on AdMob, click the button “Monetize New App” on Dashboard.

Step #2:

The next step is to add your app. You can either provide your app via play store or add it manually (for manually entering the application, you need to provide the package name of your application).

 

Step #3:

Once your app is added you need to set the type of ad unit you need to display on your app. Currently, Banner Ads, Interstitial Ads, Reward Videos, and Native Ads are supported. We will be concentrating only on Banner Ads.

Step #4:

After adding you will get your app-Id and ad-unit id.

 

It is not recommended to use the actual ad unit while developing and testing your app. In the testing phase, you are suggested to use the demo ad unit.

Once you have your ad-unit, it’s the time to integrate AdMob to your app.

Step #1:

Add the AdMob library to your build.gradle [add it to your app build file (module:app)]. Do remember to “Sync the Project” after adding this line.

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

Step #2:

Initialize the AdMob in the MainActivity.

public class MainActivity extends AppCompatActivity {
    ...
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Sample AdMob app ID: your app ad id
        MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");    }
    ...
}

Step #3:

Open your activity layout file and add the Banner Ad-View to this layout file.

<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"
        android:background="@color/blue">
    </com.google.android.gms.ads.AdView>

Step #4:

Now the main part, you need to initialize the adView so that ads start coming in your activity.

public class MainActivity extends AppCompatActivity {
    private AdView mAdView;

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

// init the ads
        mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
    }
    ...
}

Step #5:

Time to run the app, you will see the demo banner ad on the device running your app, either emulator or actual device.

 

Courtesy: Android Learner: Android How to integrate Admob

Subscribe to our Journal

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!