Making apps are always hard work. This includes days/months of testing and feature iterations. Implementing ads can be one solution where your work finally gets paid off. Google's Admob is an efficient SDK when it comes to app monetisation. Let us see how to implement Admob ads into our android application.
Let us break this into two modules :
1. Setting up ad units in the admob dashboard.
2. Implementing the created ad units in our app.
You should add the app id(the one with "~") here as well. For testing purpose, above id is fine(admob will serve test ads for this value)
Now go to layouts>> activity_main.xml and add the following code
In the above declared adview, you need to place the ad unit id from the admob dashboard when going live or you can use the above test value for testing (the one with "/" in between). We have created this earlier along with app id
Now go to your MainActivity.java file and then initialise and make an ad request like this following
Now run your app and you could see test ads being displayed like below
Now change the test credentials to the ids mentioned in admob dashboard and see if live ads are appearing. Live ads will be similar to the test ads but it wont be having a test ad label like the one above. Don't worry if the ads not appearing when you added your live credentials, admob might take a few hours after ad unit to setup the ads for you. It will start showing ads from then on.
Download full source code here
Let us break this into two modules :
1. Setting up ad units in the admob dashboard.
2. Implementing the created ad units in our app.
1. Setting up ad units in the admob dashboard.
- Step 1 : head over to https://apps.admob.com/ and sign in. After proper login, you would see an option named Apps in the left sidebar, click it. Click 'add app' option on the popup that follows.
- Step 2 : After setting up the app as instructed in previous step, select the created app from the above menu and click on Ad Units from the left sidebar menu as shown below
- Now click on "ADD AD UNIT" from the next screen. This will take you to the screen where you can select the ad type : banner, interstetial or rewarded ad type. In this tutorial we will be covering the banner ad type. Banner ads are rectangular strip ads that looks like the following
- Select banner type and complete the ad unit creation by entering the details. You will reach this screen finally
- In the above image, the id given in the first step with a "~" somewhere in between the id is called the app ID and the id given in second step with a "/" somewhere in between the id is the ad unit id. Now make note of these two ids. We will require them while we setup the code.
2. Implementing the created ad units in our app.
Now create a new Android Studio Project and follow these steps :
- Goto your project level build.gradle file and add the maven repo inside allprojects>> repositories like this
allprojects {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}
}
- Goto your app level build.gradle file and add the dependency like this
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.google.android.gms:play-services-ads:17.0.0'
}
- Now we need to add the app id that we have obtained from our admob dashboard previously(one with the ~ in between) to our AndroidManifest.xml like shown below.Here I'm adding test app id provided by admob(you may use this for testing but change it to your app id from dashboard while going live)
<manifest>
<application>
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713"/>
</application>
</manifest>
adding the above meta tag is a mandatory step from play-services-ads version 17.0.0 onwards. Also the meta tag should be added within your <application> tag as shown above
- Now you may perform a gradle sync.
You are Now all set to implement the ads inside your app. Now go to your MainActivity.java add the following code to initialise the admob ads sdk
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713 MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713"); } }
You should add the app id(the one with "~") here as well. For testing purpose, above id is fine(admob will serve test ads for this value)
Now go to layouts>> activity_main.xml and add the following code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_height="match_parent" android:layout_width="match_parent" tools:context=".MainActivity"> <TextView android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto" 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> </RelativeLayout>
In the above declared adview, you need to place the ad unit id from the admob dashboard when going live or you can use the above test value for testing (the one with "/" in between). We have created this earlier along with app id
Note : Never use your production values of ad unit id, app id for testing purposes(doing so may get your account deactivated, although doing so few times for checking is not a problem). It is recommended that you use these test values : ca-app-pub-3940256099942544~3347511713 (test app id), ca-app-pub-3940256099942544/6300978111(test ad unit id). You may change this to the production values(the one you created in admob dashboard) while going live
Now go to your MainActivity.java file and then initialise and make an ad request like this following
public class MainActivity extends AppCompatActivity { private AdView mAdView; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713"); mAdView = findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder().build(); mAdView.loadAd(adRequest); } }
Now run your app and you could see test ads being displayed like below
Now change the test credentials to the ids mentioned in admob dashboard and see if live ads are appearing. Live ads will be similar to the test ads but it wont be having a test ad label like the one above. Don't worry if the ads not appearing when you added your live credentials, admob might take a few hours after ad unit to setup the ads for you. It will start showing ads from then on.
You can download the full source code hereIn the downloaded code replace the test credentials with the one received from admob dashboard to see live ads or otherwise it will be serving test ads
hi can u help me to put the admod ads on my app. I have the source code already but i dun know how to do it.
ReplyDeleteHelp me with ur source code bro pls
DeleteGuy please help me with ur source code. I can help u putthe admob ads in ur app
Delete