Banner And Interstitial ADS Add in app
Aslam o Alikum friends. Today we can discuss about Admob ads. A lots of people have same question that is “Why my ads are not showing in app “. There are several reason but main reason is not choosing right dependency so kindly read my article carefully if you miss any step ads will not shown.
Steps
There are several steps and all steps are very important so kindly don’t miss any step other wise ads will not show on your app.
- First step is to add dependency to your android project location is Build.Gradule.
- For banner we can use xml code with ID and that id we can access in Java class.
- For interstitial Ads we cannot need xml code.
Code
Dependency Buidl.gradule
Important Note!! If you are using firebase dependency v17.0 then admob v12.0 dependency cannot work so if you use firebase with admob then you can add admob v15.0 dependency.
implementation 'com.google.android.gms:play-services-ads:12.0.0'
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"> </uses-permission>
Java Activity Imports for banner ads
import com.google.android.gms.ads.MobileAds; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdView;
XML Code For Banner Ads
<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>
Banner Ads for java code
AdView mAdView;
MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713"); mAdView = findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder().build(); mAdView.loadAd(adRequest);
Java Activity Import for interstitial ads
import com.google.android.gms.ads.InterstitialAd;
Banner Ads for java code
private InterstitialAd mInterstitialAd;
mInterstitialAd = new InterstitialAd(this); mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712"); mInterstitialAd.loadAd(new AdRequest.Builder().build()); mInterstitialAd.setAdListener(new AdListener() { @Override public void onAdClosed() { mInterstitialAd.loadAd(new AdRequest.Builder().build()); } });
Function code
public void Intertialshow() { if (mInterstitialAd.isLoaded()) { mInterstitialAd.show(); } else { finish(); } }
On Back Pressed Button Code
@Override public void onBackPressed() { Intertialshow(); }
So friends this is the code of banner ads and interstitial ads. In next articles we well discuss the reward video ads and native ads. Banner ads cpc is less then interstitial ads so use interstitial ads. Video reward ads cpc is greater than interstitial ads. So next article we will discuss reward and native ads.
If you have any problem about admob ads kindly mention in comment section.
bro thank you so much but how to add timer for this
i have app contain 20 button, everytime you click button show ad, for that this violate admob policies
so how to add timer , and is that code work for buttons ?