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 3Admob 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 replaceca-app-pub-3940256099942544/6300978111
with your own admob banner ad code
Then go to MainActivity.Java fileAfter below line Place Main codesetContentView(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~3347511713with your application id you get from admobStep 4Admob interstitial ads android tutorial
Place this code in .Java File or MainActivity.Java FileMobileAds.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 idReplace 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 beopen when user close interstitial ad in lines 2,3,4 ,9,11,19,23Don't place interstitial ad in every activity , Place one interstitial ad after 4-5 activitiesIf You want to apply interstitial ad on more than one button thenCopy Paste Same Code and Replace interstitialAd1 with interstitialAd2 or some other text of your choice in whole codeHappy EarningComment Below If you face any problem
4 Comments
This comment has been removed by the author.
ReplyDeleteThank you for sharing this post. Looking forward to read more.
ReplyDeleteAndroid App Development Services India
Nice article, thanks for such a nice information mobile application development
ReplyDeleteExcellent 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,
ReplyDeleteAndroid Application Development
Android App Development