Make Money Online BANNERS How to Implement Admob Ads in Android Studio || Banner and Interstitial Ads || FoxAndroid

How to Implement Admob Ads in Android Studio || Banner and Interstitial Ads || FoxAndroid

How to Implement Admob Ads in Android Studio || Banner and Interstitial Ads || FoxAndroid post thumbnail image


Google AdMob is a mobile advertising platform that you can use to generate revenue from your app. You can display banner ads and interstitial ads in your app using AdMob. In this article, we will show you how to implement AdMob banner and interstitial ads in Android Studio.

Banner Ads

Banner ads are rectangular ad units that occupy a spot on screen. They are usually placed at the top or bottom of the screen. Banner ads are less intrusive than interstitial ads and they can be dismissible by the user.

To implement banner ads in your app, you need to add the Google Play services dependency in your build.gradle file.

dependencies {
compile ‘com.google.android.gms:play-services-ads:10.2.0’
}

Next, create a layout file for your banner ad. For this example, we will name it activity_main_banner_ad.xml.





In the layout file, we have added a TextView and an AdView . The AdView is used to display the banner ad in our app’s layout file that we created earlier on activity main XML File Add following Activity on Mainfest File MainActivty class call SetContentVIew(R .Layout .activity _main _banner _ad) Add below Permission on Manifest File Internet Permission Add Following Code into MainActivity Class import com .google .android .gms .ads .AdRequest ; import com .google .android .gms .ads .AdSize ; import com .google .android .gms .ads .AdView ; public class MainActivity extends AppCompatActivity { private AdView mAdView ; @Override protected void onCreate ( Bundle savedInstanceState ) { superonCreate ( savedInstanceState ); setContentView ( Rview ); // Create an ad request // This code should be replaced with live code when deploying mAdRequest new AdRequestBuilder ().build (); // Create an ad view // Replace with actual unit id or dynamically fetch it at runtime mAdView = view findById ( Id Rstring /); mAdRequest View request ( mAd ); } } Build & Run Project check Emulator or Device Logcat Message check Internet Connection ON Device or Emulator check Output Screenshot After few Second Output Show Full Size Banner add If Click On Banner add It Redirect Website Link Tested Device : Xiaomi Redmi Note 4 Mobile Android Version : 7 Naugat Rooted Device Install NDK : Yes Google Play Service Version 10+ Tested Operating System Windows 10 64bit Home Edition JDK 1 8 Update 144 JRE 1 8 Update 144 Gradle 3 3 Android Studio 2 3 4 Interstitial Ads Interstitial ads are full-screen ads that cover the interface of an app until closed by the user They are typically displayed during natural transitions in your app such as between activities or when transitioning from portrait to landscape mode Interstitial ads are more intrusive than banner ads and they cannot be dismissed by the user To implement interstitial ads in your app you need to add the Google Play services dependency in your buildgradle file dependencies { compile ‘comgoogleandroidgmsplay-services-ads1020 0′ } Next create a layout file for your interstitial ad For this example we will name it activitymaininterstitialadxml ltxml version= 1 0 encoding= utf 8?gt ltRelativeLayout xmlnsandroiddeclare location schema http schemasandroidcomapkresandroiddeclare locationgt ltLinearLayout xmlnsandroiddeclareschema location http schemasandroidcomapkresandroiddeclareschemagt ltTextView xmlnsandroiddeclare location schema http schemasandroidcomapkresandroiddeclare locationgt Inthe layout file we have added a Text View and anInterstitialAd After adding this we need add following Activity on Manifest File MainActivity class call SetContentVIew(R Layout activity maininterstitialad) Add below Permission on Manifest File Internet Permission uses permissiondeclare permission INTERNET/>Add Following Code into MainActivity ClassInterstitialAds import comgoogleandoridgmgsplayservicesadsInterstitialAd import comgoogleandoridgmgsplayservicesadsAdRequest class MainActivity Extends AppCompatActivity private InterstitialInterstitial private final String TAG Log class loadInterstitial void showInterstitial if Interstitium not ready Log dTAGinterstitial not ready return if Interstitium ready then show it Log dTAGShowing interstitial prepareInterstitial new prepareInterstitialTask protected void prepareInterstitial new AsyncTaskltVoid Voidgt() protected Void doInBackground params Void // Create an ad request AdRequestBuilder builder new AdRequestBuilder(); builderaddTestDeviceAndroidEmulator true builderaddTestDevicelD1A5FFC9B15B4790C805819ECAD2B4C7 true try builderbuild(); catch RemoteException e Log egetMessage(); return null; @Override protected void onPostExecute result if result null then something went wrong try if not ready yet Threadsleep 2000; catchInterruptedException e ignore } } }; showingTask new showingTask protected void prepareInterstitial new AsyncTaskltVoid Voidgt() { @Override protected Void doInBackground params Void try Threadsleep 10000; catchInterruptedException e ignore return null; @Override protected void onPostExecute result if result null something went wrong LoadInterstitial loadInter new LoadInterstitial execute Oncreate loadInter new Oncreate Override protected void onCreate Bundle savedInstancestate superonCreate savedInstanceState setContentVIew RLayoutactivitymainbannerad TextVIew textHelloWorld view findVIewById TRstringhelloworld textsetText Hello World! How are you doing today? //assing admob ad unit id to string variable string admobUnitId “ca-app-pub-3940256099942544/6300978111”; MobileAdsinit context getApplicationContext admobUnitId new OnInitListener() @Override public void initMobileSDK successtoast toastmakeText getApplicationContext Init succeess Toast LENGTHSHORT showshowing Task task new showing Task taskexecuteOnExecute loadInter new Onloadinter executeOnExecute build gradle allprojects repositories jcenter Maven { url “https://mavenjava net/artifactorypublic pkgomglibsreleasec/” }dependencies{compile’ commavenapachemaven3pluginsmavencompilerplugin3 6 0final’ commonslang3 3 6′ log4jlog4j121216 final’ junitjunit4 12 jar’ orgtestngtestng 6 14 1 jar’ orgapachevelocityvelocity1 7 0 jar testCompile’mockito all1 9 5 final orgobjenesisobjenesis2 2 2 junitMockitoJUnitRunner deps compilecommonscodeccommonscodec1 10jar commonscollectionscommonscollections4 0jar commonsdaemonprocrun4 0pl Jar derbyderbyclient10 11 2 1jar derbynet10 11 2 1jaractivationactivation1 1 jar antlrantlrrt3 4 7 jar bcelbcel6 4 0jar backportutilconcurrentbackportutilconcurrent3 1 4jar commonsdigestercommonsdigester2 0 8b javaxservlet jsp api2 5 api javaxservlet javaxservletapi2 5 provided chtmlparserjsphtmlparser14aothree jsphtmlparser14aoprovided servletapiprovided jspapiprovided gnujaxpdomgnujaxpdom11provided xalanxalan109 provided xercesxercesImpl255 provided xmlapis255 provided

Related Post