How to Integrate Google AdMob in your Android App
AdMob is a multi-platform
mobile ad network that allows you to monetize your android app. By integrating
AdMob you can start earning right away. It is very useful particularly when you
are publishing a free app and want to earn some money from it.
Integrating
AdMob is such an easy task that it takes not more than 5mins. In this article
we’ll build a simple app with two screen to show the different types of ads
that AdMob supports.
1. Type of Ads
– Banner and Interstitial
AdMob currently support two kinds
of ad units. One is Banner ad
which occupies a portion of the screen. Other is Interstitial ad which occupies
device full screen.
Interstitial completely blocks your app UI and places the ad on top it.
2. Creating Ad
Units
1. Sign into your AdMob account.
2. Click on Monetize tab.
3. Select or Create the app and choose the platform.
4. Select the ad
format either Banner or Interstitial and give the ad unit a
name.
5. Once the ad unit is created, you can notice the Ad unit ID on the dashboard. An
example of ad unit id look like ca-app-pub-066457076332243242/3326342124
Create
as many ad units required for your app.
Banner Ad
Banner ads occupies only a portion of the screen.
I am adding a banner ad in my main activity aligning to bottom of the screen
Interstitial Ad (Fullscreen Ad)
Interstitial ads occupies full screen of the app.
Adding interstitial ad doesn’t require an AdView element to be added in the xml
layout. Rather we load the ad programatically from the activity. Normally these
ads will be populated when user is moving between activities or moving to next
level when playing a game.
We’ll
test this ad by creating a second activity and popup the full screen ad when
the second activity is launched.
Creating
New Project
1. Create a new project in Android Studio
from File ⇒ New Project.
When it prompts you to select the default activity, select Empty Activity and proceed.
2. Open build.gradle and add play services dependency as
AdMob requires it.
compile
‘com.google.android.gms:play-services-ads:9.0.0’
Open AndroidManifest.xml and
add the below mentioned permissions and other properties.
> Add INTERNET permissions.
<uses-permission
android:name="android.permission.INTERNET"
/>
Add
Linear Layout in main activity.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/ad"
android:layout_alignParentBottom="true"
android:background="@android:color/transparent"
android:orientation="horizontal"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/ad"
android:layout_alignParentBottom="true"
android:background="@android:color/transparent"
android:orientation="horizontal"
/>
Now
open and edit main activity java class
public class MainActivity extends AppCompatActivity { AdView adView; LinearLayout linearLayout; InterstitialAd interstitialAd;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_main); linearLayout = (LinearLayout) findViewById(R.id.ad); adView = new AdView(this); interstitialAd = new InterstitialAd(this); initAd(); new Handler().postDelayed(new Runnable() { @Override public void run() { displayInterstialAd(); } },2000);
} public void initAd(){ adView.setAdUnitId("ca-app-pub-2049596536820731/9402866166"); adView.setAdSize(AdSize.SMART_BANNER); AdRequest adRequest=new AdRequest.Builder().build(); if(adView.getAdSize()!= null || adView.getAdUnitId()!= null){ Log.e("","Banner Requested"); adView.loadAd(adRequest); }else{ Log.e(""," AdSize: "+adView.getAdSize()+" AdUnitID: "+adView.getAdUnitId()); } linearLayout.addView(adView); } public void showInterstetial(){ interstitialAd.setAdUnitId("ca-app-pub-2049596536820731/2995926838"); interstitialAd.setAdListener(new AdListener() { @Override public void onAdLoaded() { super.onAdLoaded(); displayInterstialAd(); } }); } public void displayInterstialAd(){ if(interstitialAd.isLoaded()) { Toast.makeText(this,"showing ads..",Toast.LENGTH_LONG).show(); interstitialAd.show(); }else{ Log.e("","Ads not loaded"); } } }
No comments:
Post a Comment