Your experience on this site will be improved by allowing cookies
In the below example, we are displaying Google AdMob full-screen Interstitial Ads on its layout
build.gradle:
In the build.gradle file, we will add the below google ads dependencies.
compile ‘com.google.android.gms:play-services-ads:8.4.0’
apply plugin: 'com.android.application' android { compileSdkVersion 28 defaultConfig { applicationId "com.example.radioapp" minSdkVersion 23 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } sourceSets { main { assets { srcDirs 'src/main/assets', 'src/main/res/assets/' } } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' implementation 'com.android.support:design:28.0.0' implementation 'com.android.support:support-v4:28.0.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' implementation 'com.google.android.gms:play-services-ads:8.4.0' } |
AndroidManifest.xml:
In the AndroidMenifest.xml file, we will add the below user permissions:
File: AndroidManifest.xml:
activity_main.xml:
In the activity_main.xml file, we will write the code to create google ads AdView View.
File: MainActivity.java:
package com.example.radioapp; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button=(Button)findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, InterstitialAdsActivity.class); startActivity(intent); } }); } } |
activity_interstitial_ads.xml:
In the activity_interstitial_ads.xml file, created inside the layout, we will write the code to display the interstitial ad.
File: InterstitialAdsActivity.java:
File: InterstitialAdsActivity.java: package com.example.radioapp; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import com.google.android.gms.ads.AdListener; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.InterstitialAd; public class InterstitialAdsActivity extends AppCompatActivity { InterstitialAd mInterstitialAd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_interstitial_ads); mInterstitialAd = new InterstitialAd(this); // set the ad unit ID mInterstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen)); AdRequest adRequest = new AdRequest.Builder().build(); // Load ads into Interstitial Ads mInterstitialAd.loadAd(adRequest); mInterstitialAd.setAdListener(new AdListener() { public void onAdLoaded() { showInterstitial(); } }); } private void showInterstitial() { if (mInterstitialAd.isLoaded()) { mInterstitialAd.show(); } } } |
0 comments