AdMob ads is a multi platform mobile ad network that provide developers
to monetize their android app(s) by displaying in ­app ads from
other advertisers  . By integrating AdMob ads you can start
earning right away.

You can set filters to select the ads being displayed are
relevant to your app users. You also have control over the types
of ad formats you allow.
Choose from banner ads, full pages interstitial, video ads, and native ads.

Create an AdMob ads new Project and select Empty Activity and go through it.

1 . Import the Mobile Ads SDK :


At first here we have to add the Google Mobile Ads SDK in our app’s
project­level build.gradle file .

implementation 'com.google.android.gms:play­services-ads:17.1.1'

Try to add the latest version of the Mobile Admob Ads SDK. Once that’s done, save the file and perform a Gradle sync.

2. Add Meta ­data to AndroidManifest.xml :

After this add the Meta ­Data with AdMob App ID to your app’s AndroidManifest.xml, we can find out our App ID in the AdMob UI Account.

<meta­-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="[ADMOB_APP_ID]"/>

Note : Meta­data is required as of Google Mobile Ads SDK version 17.0.0. If you haven’t created an AdMob account then you can use Google Test App ID to initialize the SDK: ca­-app-pub-­3940256099942544~3347511713 .

3 . Initialize Mobile AdmobAds :

Initialize MobileAds to onCreate method of our Main_Activity.java file

MobileAds.initialize(this, "YOUR_ADMOB_APP_ID");

4. Implement AdView to the XML Layout :

Implement AdView to the XML Layout file i.e. activity_main.xml.

Now open your activity_main.xml file and add a banner adview in the XML layout of activity or fragment where we want to display it for better user experience.

<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"
android:layout_marginBottom="5dp"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca­-app-­pub­-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>

Note : If you don’t have an adUnitId then you can use Google test ad unit ID for Android banners i.e. ca­-app-­pub­-3940256099942544/6300978111

5. Implement adView, Adrequest, loadAd and Ad Listener in Main_Activty.java :

Now Add the following code to implement Admob ads in Main_Activty.java

package net.technxtcodelabs.admob;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

public class MainActivity extends AppCompatActivity {
private AdView adView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, getString(R.string.app_id));
adView = (AdView) findViewById(R.id.adView);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId(getString(R.string.add_unit_id));
AdRequest mAdRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
// To get your test device ID find it from logcat details
.addTestDevice("device_id")
.build();

//Admob listener
adView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {

// Code to be executed when an ad finishes loading.

}
@Override
public void onAdClosed() {
Toast.makeText(getApplicationContext(), "Ad is closed!", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdFailedToLoad(int errorCode) {
Toast.makeText(getApplicationContext(), "Ad failed to load! error code: " + errorCode, Toast.LENGTH_SHORT).show();
}
@Override
public void onAdLeftApplication() {
Toast.makeText(getApplicationContext(), "Ad left application!", Toast.LENGTH_SHORT).show();
}
@Override

public void onAdOpened() {
super.onAdOpened();
}
});
adView.loadAd(mAdRequest);
}
@Override
public void onPause() {
if (adView != null) {
adView.pause();
}
super.onPause();
}
@Override
public void onResume() {
super.onResume();
if (adView != null) {
adView.resume();
}
}
@Override
public void onDestroy() {
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}
}

Now run the project or app in android device or any Emulator .

%d bloggers like this: