Admob integration in android studio | Admob android tutorial | Admob interstitial ads android tutorial | Admob banner ads tutorial




Step 1

First add below dependency in build.gradle (Module:app)
after
dependencies {

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



and sync Project




Step 2 

then Open AndroidManifest.xml

go to last of Code and Place below code before last two lines in Manifest , 
last two lines are also given below

<meta-data    
android:name="com.google.android.gms.ads.APPLICATION_ID"    
android:value="ca-app-pub-3940256099942544~3347511713" />

</application>

</manifest>




ca-app-pub-3940256099942544~3347511713 is test app id 
You will get your app id when you register your app on admob replace this id with your
app application id






Step 3 

Admob banner ads tutorial


First go To MainActivity.xml or your desired xml file and place below code in layout


<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>
 



You have to replace 
ca-app-pub-3940256099942544/6300978111   
with your own admob banner ad code



Then go to MainActivity.Java file

After below line Place Main code


setContentView(R.layout.activity_main);



AdView adView = findViewById(R.id.adView);
MobileAds.initialize(this , "ca-app-pub-3940256099942544~3347511713");
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest); 
 

you have to replace ca-app-pub-3940256099942544~3347511713
with your application id you get from admob  





Step 4

Admob interstitial ads android tutorial

Place this code in .Java File or MainActivity.Java File
MobileAds.initialize(this , "ca-app-pub-3940256099942544~3347511713");
final InterstitialAd interstitialAd1 = new InterstitialAd(this);
interstitialAd1.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
interstitialAd1.loadAd(new AdRequest.Builder().build());
Button button7 = findViewById(R.id.button7);
button7.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (interstitialAd1.isLoaded()) {

            interstitialAd1.show();
        } else {

            startActivity(new Intent(MainActivity.this, NextActivity.class));

        }
    }
});
interstitialAd1.setAdListener(new AdListener() {

    @Override
    public void onAdClosed() {
        interstitialAd1.loadAd(new AdRequest.Builder().build());
        startActivity(new Intent(MainActivity.this, NextActivity.class));

    }

});
 
Replace ca-app-pub-3940256099942544~3347511713 in first line with your Application id

Replace ca-app-pub-3940256099942544/1033173712 in third line with your Admob interstitial ad code

Replace button7 in 5 and 6 lines,with your button id on which you want to apply ad

Replace NextActivity.class in 14 and 24 lines with your own activity which should be 
open when user close interstitial ad in lines 2,3,4 ,9,11,19,23 


Don't place interstitial ad in every activity , Place one interstitial ad after 4-5 activities
If You want to apply interstitial ad on more than one button then
Copy Paste Same Code and Replace interstitialAd1 with interstitialAd2 or some other text of your choice in whole code
Happy Earning
Comment Below If you face any problem

Post a Comment

4 Comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. Thank you for sharing this post. Looking forward to read more.
    Android App Development Services India

    ReplyDelete
  3. Nice article, thanks for such a nice information mobile application development

    ReplyDelete
  4. Excellent information in this Post. Keep it up. Thanks for sharing Love to read it, Waiting For More new Update and I Already Read your Recent Post its Great Thanks,
    Android Application Development
    Android App Development

    ReplyDelete