interstitial ads example android studio
hello android developer. today I will tell you how to add interstitial ads in the android studio. in the last article, we already learned about how to place banner ads form AdMob in your android application. the banner ads cover only a small portion of your application but the interstitial ads cover the entire area of the application. must you have to take extra care while placing interstitial ads. don’t place a large number of interstitial ads in your application that will affect the AdMob policy and also affect the user experience of the application user experience. it may lead to the uninstall of the application.
now I’m going to show you how to place interstitial ads in your android application. the interstitial ads give you more revenue than simple banner ads.
XML (activity_main.xml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" xmlns:ads="http://schemas.android.com/apk/res-auto" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" > <com.google.android.gms.ads.AdView 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> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start Interstitial Ads" android:id="@+id/newgame_button" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="58dp" /> </RelativeLayout> |
JAVA (MainActivity.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import com.google.android.gms.ads.AdListener; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdView; import com.google.android.gms.ads.InterstitialAd; public class AdMobInterstitialAds extends AppCompatActivity { InterstitialAd ads; Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ad_mob_interstitial_ads); button = (Button) findViewById(R.id.newgame_button); ads = new InterstitialAd(this); ads.setAdUnitId("ca-app-pub-3940256099942544/1033173712"); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (ads.isLoaded()) { ads.show(); } else { } } }); ads.setAdListener(new AdListener() { @Override public void onAdClosed() { } }); AdView mAdView = (AdView) findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder().build(); mAdView.loadAd(adRequest); ads.loadAd(adRequest); } } |