Header Ads

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

No comments

Powered by Blogger.