AdMob provides various ads for mobile development. These are 3 categories.
- Banner
- Interstitial
- Rewarded
Now,Here i will explain Code integration about Banner and Interstitial,It's very simple and easy.
dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.android.support:support-v4:26.1.0' implementation 'com.google.android.gms:play-services-ads:8.4.0'}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <com.google.android.gms.ads.AdView
android:id="@+id/my_addnews"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"ads:adUnitId="put here your add banner unit Id" />
</LinearLayout>
MainActivity.java
import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; 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 MainActivity extends AppCompatActivity { private AdView mAdView; private InterstitialAd mAdMobInterstitialAd; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mAdMobInterstitialAd = new InterstitialAd(this); mAdMobInterstitialAd.setAdUnitId("Your Interstitial Unit Id");AdRequest adRequest = new AdRequest.Builder() .build(); mAdMobInterstitialAd.loadAd(adRequest); mAdMobInterstitialAd.setAdListener(new AdListener() { public void onAdLoaded() { showInterstitialAd(); } }); mAdView = (AdView) findViewById(R.id.my_addnews); AdRequest adRequest2 = new AdRequest.Builder() .build(); mAdView.loadAd(adRequest2); mAdView.setAdListener(new AdListener() { @Override public void onAdLoaded() { } @Override public void onAdClosed() { // Toast.makeText(getActivity(), "Ad is closed!", Toast.LENGTH_SHORT).show(); } @Override public void onAdFailedToLoad(int errorCode) { //Toast.makeText(getActivity(), "Ad failed to load! error code: " + errorCode, Toast.LENGTH_SHORT).show(); } @Override public void onAdLeftApplication() { //Toast.makeText(getActivity(), "Ad left application!", Toast.LENGTH_SHORT).show(); } @Override public void onAdOpened() { super.onAdOpened(); } }); } @Override public void onPause() { if (mAdView != null) { mAdView.pause(); } super.onPause(); } @Override public void onResume() { super.onResume(); if (mAdView != null) { mAdView.resume(); } } @Override public void onDestroy() { if (mAdView != null) { mAdView.destroy(); } super.onDestroy(); } }
Comments
Post a Comment