How to add Admob ads to your Android App in 2020

0
157
1. Add in dependency this
Example project-level build.gradle (excerpt)
allprojects {
    repositories {
        google() // check for this
    }
}
Next, open the app-level build.gradle file for your app, and look for a “dependencies” section.
Example app-level build.gradle (excerpt)
dependencies {
    implementation fileTree(dir: ‘libs’, include: [‘*.jar’])
    implementation ‘androidx.appcompat:appcompat:1.0.2’
    implementation ‘com.google.android.gms:play-services-ads:19.2.0’
}
2. Add in AndroidManifest file this below code
<manifest>
    <application>
        <!– Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 –>
        <meta-data
            android:name=”com.google.android.gms.ads.APPLICATION_ID”
            android:value=”ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy”/>
//Where xxxxxxxxxxxxxxxx~yyyyyyyyyy is your admob id.
    </application>
</manifest>
3. Import these dependencies codes in any activity you want display admob incase you don’t want to show ads in all your activity
          import com.google.android.gms.ads.MobileAds;
          import com.google.android.gms.ads.initialization.InitializationStatus;
          import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
4. Initialize these codes under onCreate method in your java activity.
            MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
            }
        });
You have successfully inform you app that admob has been introduced.
Lets add Banner and interstitial
5. Go to any xml interface you want banner to show and add this below codes. Remember any XML file you add the code the complimentary Java file must contain banner codes too.
<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>
Ensure to make it fit the UI design.
6. Import the ads banner request and adView by adding below codes.
    import com.google.android.gms.ads.AdRequest;
    import com.google.android.gms.ads.AdView;  
7. Intialise this below code under publc class Activity name.
         private AdView mAdView;
 
8. Initialize these under onCreate method in your java activity by adding below codes.
        mAdView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
9. Import the ads interstitial request and adView by adding below codes.
    import com.google.android.gms.ads.InterstitialAd;  
10. Intialise this below code under publc class Activity name.
          private InterstitialAd mInterstitialAd;
  
11. Initialize these under onCreate method in your java activity by adding below codes.
        mInterstitialAd.loadAd(new AdRequest.Builder().build());
12. Carefully add below code in any onClickListener you want ads to dispay
    mMyButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (mInterstitialAd.isLoaded()) { //add from here 
            mInterstitialAd.show();
        } else {
            Log.d(“TAG”, “The interstitial wasn’t loaded yet.”);
        }  //To here
    }
});
Watch the video below
There are other customization check here for more.